题目1159:坠落的蚂蚁

题目1159:坠落的蚂蚁_第1张图片
#include 
#include 
#include 
#include 
using namespace std;
 
struct Ant
{
    int pos;
    int dir;
};
Ant ant[101];
int pos[101];
 
int cmp(const void *a, const void *b) {
    Ant at = *(Ant *)a;
    Ant bt = *(Ant *)b;
    return at.pos - bt.pos;
}
 
int main(int argc, char const *argv[])
{
    int n;
    //freopen("input.txt","r",stdin);
    while(scanf("%d",&n) != EOF) {
        for(int i = 0; i < n; i++) {
            scanf("%d %d",&ant[i].pos, &ant[i].dir);    
        }
        qsort(ant, n, sizeof(Ant), cmp);
        int left = 0;
        int right = 0;
        int Apos;
        int state = 0;
        int j = 0;
        for(int i = 0; i < n; i++) {
            if(state == 0 && ant[i].dir == 1) {
                pos[j++] = ant[i].pos;
                left++;
            }
            else if(state == 0 && ant[i].dir == 0) {
                state = 1;
                Apos = j;
                pos[j++] = ant[i].pos;
                 
            }
            else if(state == 1 && ant[i].dir == -1) {
                pos[j++] = ant[i].pos;
                right++;
            }
        }
        if(left == right) {
            puts("Cannot fall!");
            continue;
        }
        else if(left > right) {
            int tmp = left - right;
            int ans = 100 - pos[tmp-1];
            printf("%d\n",ans);
        }
        else {
            int tmp = right - left;
            int ans = pos[Apos + left+1];
            printf("%d\n",ans);
        }
    }
    return 0;
}
/**************************************************************
    Problem: 1159
    User: cust123
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1520 kb
****************************************************************/

你可能感兴趣的:(九度OJ)