题目: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.