CodeForce-810B Summer sell-off (结构体排序)

http://codeforces.com/problemset/problem/810/B

已知n天里,已知第i天的供货量和需求量,给定一个f,可以在n天之中选f天促销使得供货量翻倍。

问选择其中f天的供货量翻倍之后,n天之后总的销量是多少。

思路:先对于每一天都先算出正常的销量 x1 = min(供货量,需求量) ,然后再算每一天促销之后的销量 x2 = min(供货量*2,需求量)  。

对 x2 - x1 进行排序,找出促销之后销量增加最大的 f 天,正常模拟输出即可。

结构体:

struct node

{

   long long 供货量,销售量,正常销量,促销销量;

   正常销量 = min(供货量,需求量);

   促销销量 = min(供货量*2,需求量);

}货物[100005];

/*这题在CF上的测试数据有一百多组。*/

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct node
 4 {
 5     int k,l,x1,x2;
 6 }s[100050];
 7 int cmp(node a,node b)
 8 {
 9     return a.x2>b.x2;
10 }
11 int main()
12 {
13     int n,f;
14     cin>>n>>f;
15     for(int i=0;i<n;i++)
16     {
17         cin>>s[i].k>>s[i].l;
18         s[i].x1=s[i].k>=s[i].l?s[i].l:s[i].k;
19         s[i].x2=s[i].k*2>=s[i].l?s[i].l:s[i].k*2;
20         s[i].x2-=s[i].x1;
21     }
22     sort(s,s+n,cmp);
23     long long ans=0;
24     for(int i=0;i<n;i++)
25     {
26         if(i<f) ans+=s[i].k*2>=s[i].l?s[i].l:s[i].k*2;
27         else ans+=s[i].x1;
28     }
29     cout<<ans<<endl;
30     return 0;
31 }

 

posted @ 2017-05-25 20:53  饼饼饼饼饼  阅读(241)  评论(0编辑  收藏  举报