Asia Regional Contest, Tokyo,Problem C Shopping

贪心。

不难证明,如果两个区间有交集,那么最好的方法是走完这些区间的并再掉头。  也就是说,处理出所有相交的区间组成一个更大的区间,然后对于这些区间,答案要加上两倍的区间长度。  然后最后再加上整条街的长度即可。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 1000 + 5;
int T,n,m,u,v;
struct node {
    int l, r;
    node(int ll=0, int rr=0):l(ll), r(rr) {}
    bool operator < (const node& rhs) const {
        return l < rhs.l || (l == rhs.l && r < rhs.r);
    }
}b[maxn],a[maxn];
int main() {
    scanf("%d%d",&n,&m);
    int cnt = 0, cnt2 = 0;
    for(int i=0;i<m;i++) {
        scanf("%d%d",&u,&v);
        if(u > v) continue;
        a[cnt++] = node(u,v);
    }
    sort(a,a+cnt);
    int l = a[0].l, r = a[0].r, ans = 0;
    for(int i=1;i<cnt;i++) {
        if(r >= a[i].l) r = max(r, a[i].r);
        else {
            b[cnt2++] = node(l,r);
            l = a[i].l;
            r = a[i].r;
        }
        if(i == cnt-1) b[cnt2++] = node(l,r);
    }
    for(int i=0;i<cnt2;i++) 
        ans += (b[i].r - b[i].l) * 2;
    ans += n+1;
    printf("%d\n",ans);

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值