Title Link: http://poj.org/problem?id=1456
Test instructions is an existing n items, each item has a shelf life and a profit, now can only sell a commodity every day, ask the maximum profit is how much, if the product expires, it can not be sold;
Methods of Violence:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<map>#include<vector>using namespaceStd;typedefLong LongLL;#definePI 4*atan (1.0)#defineN 10550#defineMet (A, b) memset (A, B, sizeof (a))structnode{intp, D;} A[n];intCMP (node p, node Q) {if(P.P! =Q.P)returnP.P >Q.P; returnP.D >q.d;}intVis[n];intMain () {intN; while(SCANF ("%d", &n)! =EOF) {Met (Vis,0); Met (A,0); for(intI=1; i<=n; i++) scanf ("%d%d", &A[I].P, &A[I].D); Sort (a+1, a+n+1, CMP); intAns =0; for(intI=1; i<=n; i++) { for(intJ=A[I].D; j>=1; j--) { if(!Vis[j]) {ans+=A[I].P; VIS[J]=1; Break; }}} printf ("%d\n", ans); } return 0;}
View Code
And check the Set optimization:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<map>#include<vector>using namespaceStd;typedefLong LongLL;#definePI 4*atan (1.0)#defineN 10500#defineMet (A, b) memset (A, B, sizeof (a))structnode{intp, D;} A[n];intCMP (node p, node Q) {if(P.P! =Q.P)returnP.P >Q.P; returnP.D >q.d;}intF[n];intFind (intx) { if(f[x]!=x) f[x]=Find (f[x]); returnf[x];}intMain () {intN; while(SCANF ("%d", &n)! =EOF) { for(intI=0; i<n; i++) F[i]=i; Met (A,0); for(intI=1; i<=n; i++) scanf ("%d%d", &A[I].P, &A[I].D); Sort (a+1, a+n+1, CMP); intAns =0; for(intI=1; i<=n; i++) { intt =Find (A[I].D); if(T >0) {ans+=A[I].P; F[t]= T1; }} printf ("%d\n", ans); } return 0;}
View Code
Supermarket---poj456 (greedy and check-set optimization)