POJ 3164 Sunscreen (挑战程序设计竞赛的练习题)

题目:https://vjudge.net/problem/POJ-3614

 

思路参考这个:https://blog.csdn.net/qq_25576697/article/details/76577536

我有时间再补吧。

思路很清晰, 不过我最开始做的第一个思路是:对奶牛的maxSPF从大到小优先队列储存, 然后看lotion的SPF是否在奶牛的SPF区间内。

 

代码中的不用专门写个cmp对比吗?

还是sort就是直接对比P->first?

 

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <queue>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int MAX_N = 2500+10;
 8 typedef pair<int, int> P;
 9 priority_queue<int, vector<int>, greater<int> > q;
10 P cows[MAX_N], bottle[MAX_N];
11 int C, L;
12 
13 bool cmp (pair<int, int>a, pair<int, int>b)
14 {
15     return a.first<b.first;//根据fisrt的值升序排序
16    
17 }
18 
19 int main()
20 {
21     cin>>C>>L;
22     for(int i=1; i<=C; i++)
23         scanf("%d%d", &cows[i].first, &cows[i].second);
24     for(int i=1; i<=L; i++)
25         scanf("%d%d", &bottle[i].first, &bottle[i].second);
26     sort(cows, cows+C+1, cmp);
27     sort(bottle, bottle+L+1, cmp);
28 //    cout<<"&&"<<cows[1].first<<cows[2].first<<cows[3].first<<endl;
29 //    cout<<"&"<<bottle[1].first<<bottle[2].first<<endl;
30     
31     int ans=0, j=1;
32     for(int i=1; i<=L; i++)
33     {
34         while(j<=C && cows[j].first<=bottle[i].first)
35         {
36             q.push(cows[j].second);
37             j++;
38         }
39         int x;
40         while(!q.empty() && bottle[i].second)
41         {
42             x=q.top();//奶牛i的最大SPF 
43 //            cout<<"$"<<bottle[i].first<<endl;
44 //            cout<<"#"<<x<<endl;
45             q.pop();
46             if(x<bottle[i].first)    continue;
47             
48             bottle[i].second--;
49 //            cout<<"*"<<bottle[i].second<<endl;
50             ans++;
51         }
52     }
53     cout<<ans<<endl;
54     return 0;
55 }

这题打完代码后一直WA, 卡了我半个多小时。

第二天才发现是因为我对sort()不够理解。

参考题解是从下标0开始储存的, 然后sort(a,a+c);

而我是从下标1开始存的, 应该要sort(a, a+c+1);才对, 少加了个1.

转载于:https://www.cnblogs.com/watsoncao/p/10658029.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值