poj 1456贪心

xiaoxiao2021-02-27  350

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int pre[10100]; struct node { int v,l; }p[10100]; bool cmp(struct node a,struct node b) { return a.v>b.v; } int find(int x) { if(pre[x]==x) return x; else return find(pre[x]); } int main() { int n; while(~scanf("%d",&n)) { for(int i=0;i<n;i++) scanf("%d%d",&p[i].v,&p[i].l); for(int i=0;i<10010;i++) pre[i]=i; sort(p,p+n,cmp); int res=0; for(int i=0;i<n;i++) { int tem=find(p[i].l); if(tem>0) { pre[tem]=tem-1; res+=p[i].v; } } printf("%d\n",res); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-5937.html

最新回复(0)