您的位置:首页 > 运维架构

Asia Regional Contest, Tokyo,Problem C Shopping

2015-11-16 22:32 323 查看
贪心。

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

细节参见代码:

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm-icpc 贪心