码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 342B Xenia and Spies (水题模拟,贪心)

时间:2016-07-08 01:27:44      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

题意:给定 n 个间谍,m个区间,一个 s,一个f,然后从 s开始传纸条,然后传到 f,然后在每个 t 时间在区间内的不能传,问你最少的时间传过去。

析:这个题,就模拟一下就好,贪心策略,能传就传,找好方向,一直传就行,传到 f 就结束。

代码如下:

#include <bits/stdc++.h>

using namespace std;
struct node{
    int l, r, t;
    bool operator < (const node &p) const{
        return t < p.t;
    }
};
node a[100005];

int main(){
    int s, t, m, n, f, l, r;
    while(cin >> n >> m >> s >> f){
        bool ok = false;
        int x = f > s ? 1 : -1;
        int pos = s;
        for(int i = 0; i < m; ++i){
            scanf("%d %d %d", &a[i].t, &a[i].l, &a[i].r);
        }
//        sort(a, a+m);
        int cnt = 0;
        for(int i = 1; ; ++i){
            if(a[cnt].t == i){
                if((pos + x < a[cnt].l && pos < a[cnt].l) || (pos + x > a[cnt].r && pos > a[cnt].r)){  printf("%c", x < 0 ? ‘L‘ : ‘R‘); pos += x; }
                else  printf("X");
                if(pos == f)  break;
                ++cnt;
            }else{
                pos += x;
                printf("%c", x < 0 ? ‘L‘ : ‘R‘);
                if(pos == f)  break;
            }
        }
        printf("\n");
    }
    return 0;
}

 

CodeForces 342B Xenia and Spies (水题模拟,贪心)

标签:

原文地址:http://www.cnblogs.com/dwtfukgv/p/5652004.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!