标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 9452 | Accepted: 4067 |
Description
Input
Output
Sample Input
4 50 2 10 1 20 2 30 1 7 20 1 2 1 10 3 100 2 8 2 5 20 50 10
Sample Output
80 185
Hint
Source
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<cstdlib> #include<algorithm> using namespace std; int n,vis[10010],ans; struct node { int x,y; }e[10010]; bool cmp(node a,node b) { return a.x>b.x; } int main() { while(scanf("%d",&n)!=EOF) { ans=0; memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) scanf("%d%d",&e[i].x,&e[i].y); sort(e+1,e+1+n,cmp); for(int i=1;i<=n;i++) { if(!vis[e[i].y]) { vis[e[i].y]=true; ans+=e[i].x; } else { for(int j=e[i].y-1;j>=1;j--) { if(!vis[j]) { vis[j]=1; ans+=e[i].x; break; } } } } printf("%d\n",ans); } return 0; }
2.使用并查集优化 速度快了一倍多
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> using namespace std; int n,fa[200010]; struct node { int x,y; }e[10010]; bool cmp(node a,node b) { return a.x>b.x; } int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); } int main() { while(scanf("%d",&n)!=EOF) { int ans=0,maxx=0; for(int i=1;i<=n;i++) { scanf("%d %d",&e[i].x,&e[i].y); maxx=max(maxx,e[i].y); } for(int i=0;i<=maxx;i++) fa[i]=i; sort(e+1,e+1+n,cmp); for(int i=1;i<=n;i++) { int fx,fy; fx=find(e[i].y); if(fx==0) continue; fy=find(fx-1); fa[fx]=fy; ans+=e[i].x; } printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4264441.html