Monday, February 24, 2020

POJ.2674 Linear world

1.Problem
http://poj.org/problem?id=2674

2.Idea
When 2 animal meets, they exchanges the name and pass thru each other.

3.Source
 #define maxn 32005  
 int dir[maxn];  
 double pos;  
 char name[maxn][300];  
 int n;  
 double len, v;  
 int main()  
 {  
      while (~scanf("%d", &n)) {  
           if (!n) break;  
           scanf("%lf %lf", &len, &v);  
           char ch;  
           int id;  
           double t = -1;  
           for (int i = 0; i < n; i++) {  
                ch = getchar();  
                while (ch == '\n' || ch == ' ') ch = getchar();  
                if (ch == 'p' || ch == 'P')  
                     dir[i] = 1;  
                else dir[i] = -1;  
                scanf("%lf", &pos);  
                scanf("%s", name[i]);  
                double tmp;  
                if (dir[i] == 1)  
                     tmp = (len - pos);  
                else  
                     tmp = pos;  
                if (tmp > t) {  
                     t = tmp;  
                     id = i;  
                }  
           }  
           int cnt = 0;  
           if (dir[id]>0) {  
                for (int i = id + 1; i < n; i++)  
                     if (dir[i]<0)  
                          cnt++;  
           }  
           else {  
                for (int i = id - 1; i >= 0; i--)  
                     if (dir[i]>0)  
                          cnt++;  
           }  
           if (dir[id]>0)  
                id += cnt;  
           else  
                id -= cnt;  
           t = t / v;  
           printf("%13.2lf %s\n", (int)(t * 100) / 100.0, name[id]);  
      }  
      return 0;  
 }  

No comments:

Post a Comment