0x07 贪心

【例题】POJ3614 Sunscreen

直接贴代码。。。

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 using namespace std;
 6 const int maxn=25000+10;
 7 int c, l, ans=0;
 8 pair<int,int> cow[maxn], lotion[maxn];
 9 
10 priority_queue<int> q;
11 void solve() {
12     int i, j;
13     sort(cow+1, cow+c+1);
14     sort(lotion+1, lotion+l+1);
15     for (i=j=1; i<=l; ++i) {
16         while ((j<=c)&&(cow[j].first<=lotion[i].first))
17             q.push(-cow[j++].second);
18         while ((!q.empty())&&(-(q.top())<lotion[i].first))
19             q.pop();
20         while ((!q.empty())&&(lotion[i].second--)) {
21             ++ans;
22             q.pop();
23         }
24     }
25 }
26 
27 int main() {
28     //freopen("tanning.4.in", "r", stdin);
29     scanf("%d%d", &c, &l);
30     for (int i=1; i<=c; ++i)
31         scanf("%d%d", &cow[i].first, &cow[i].second);
32     for (int i=1; i<=l; ++i)
33         scanf("%d%d", &lotion[i].first, &lotion[i].second);
34     solve();
35     printf("%d\n",ans);
36     return 0;
37 } 

【例题】POJ3190 Stall Reservations

0分,还没AC。。。

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 using namespace std;
 6 const int maxn=50000+10;
 7 struct node {
 8     int n, l, r;
 9 } cow[maxn];
10 int n, s[maxn];
11 
12 bool cmp(node a, node b) {
13     return a.l<b.l;
14 }
15 
16 priority_queueint,int>, vectorint,int> >, greaterint,int> > > q;
17 
18 int main() {
19     freopen("a.txt", "r", stdin);
20     scanf("%d", &n);
21     for (int i=1; i<=n; ++i) {
22         scanf("%d%d", &cow[i].l, &cow[i].r);
23         cow[i].n=i;
24     }
25     sort(cow+1, cow+n+1, cmp);
26     /**/
27 //    for (int i=1; i<=n; ++i)
28 //        printf("%d %d %d\n", cow[i].n, cow[i].l, cow[i].r);
29     /**/
30     int ans=0;
31     for (int i=1; i<=n; ++i) {
32         if (q.empty()) {
33             ++ans;
34             q.push(make_pair(cow[i].r, ans));
35             s[cow[i].n]=ans;
36             continue;
37         }
38         pair<int,int> tmp=q.top();
39         /**/
40         //printf("%d %d\n", tmp.first, tmp.second);
41         /**/
42         if (tmp.first<cow[i].l) {
43             int a=q.top().second;
44             //printf("%d %d\n", cow[i].n, a);
45             q.pop();
46             q.push(make_pair(cow[i].r,a));
47             s[cow[i].n]=a;
48             /**/
49             //printf("%d:%d %d\n", i, q.top().first, q.top().second);
50             /**/
51         }
52         else {
53             ++ans;
54             q.push(make_pair(cow[i].r, ans));
55             s[cow[i].n]=ans;
56         }
57         
58     }
59     printf("%d\n", ans);
60     for (int i=1; i<=n; ++i)
61         printf("%d\n", s[i]);
62     return 0;
63 } 

【例题】POJ1328 Radar Installation

贴代码

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 #include 
 6 #include 
 7 using namespace std;
 8 const int maxn=1000+10;
 9 int n, d;
10 struct node {
11     double l, r;
12 } a[maxn];
13 
14 bool cmp(node x, node y) {
15     return x.r==y.r ? x.ly.r;
16 }
17 
18 inline bool check(double a, double b) {
19     if (b>d) return false;
20     else if (b<0) return false;
21     return true;
22 }
23 
24 int main() {
25     //freopen("New7.in", "r", stdin);
26     int k=0;
27     double x, y;
28     while (cin>>n>>d) {
29         if (!n||!d) break;
30         memset(a, 0, sizeof(a));
31         int flag=0;
32         for (int i=1; i<=n; ++i) {
33             scanf("%lf%lf", &x, &y);
34             if (!check(x,y)) flag=1;
35             a[i].l=x-sqrt(d*d-y*y);
36             a[i].r=x+sqrt(d*d-y*y);
37         }
38         if (flag) {
39             printf("Case %d: -1\n", ++k);
40             continue;
41         }
42         
43         sort(a+1, a+n+1, cmp);
44         double pos=a[1].r;
45         int ans=1;
46         for (int i=2; i<=n; ++i) {
47             if (a[i].l>pos) {
48                 ++ans; pos=a[i].r;
49             } 
50             else {
51                 pos=min(a[i].r, pos);
52             }
53         }
54         printf("Case %d: %d\n", ++k, ans);
55     }
56     return 0;
57 } 

【例题】NOIP2012/CH0701 国王游戏

50分代码,100分代码需要高精度

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 #include 
 6 #include 
 7 using namespace std;
 8 const int maxn=1000+10;
 9 int n;
10 struct node {
11     int a, b;
12 } a[maxn];
13 
14 bool cmp(node x, node y) {
15     return x.a*x.by.b;
16 }
17 
18 int main() {
19     scanf("%d", &n);
20     for (int i=1; i<=n+1; ++i)
21         scanf("%d%d", &a[i].a, &a[i].b);
22     sort(a+2, a+n+2, cmp);
23     int ans=1;
24     for (int i=1; i<=n; ++i)
25         ans*=a[i].a;
26     ans/=a[n+1].b;
27     printf("%d\n", ans);
28     return 0;
29 }

 

你可能感兴趣的:(0x07 贪心)