csp20141203 集合竞价 解题报告


Solution:
对股票出价进行排序,然后按照价格递增的次序依次设定p的价格并求成交量。
1.
 //prove that the result of price(maximum--maxprice) is info[k].price:
 //If not,the nearest data that is bigger than price
 //result=maxresult & price>maxprice , conflict

2.
 //po assending as times by
 //so if maxamount is the same, use the newest

3.buy
先比较并设置maxamount值
再 买单 减

4.
sell
先 卖单 加
再比较并设置maxamount值

 

注意
1.出价价格的重复性
2.buy sell出价价格相同
3.数据2^64内的范围,用long long 或__int64。
而c注意用%I64d或%lld。而与购买股数有关的变量全部用long long 或__int64,不要遗漏,如min和max函数要用到long long 或__int64。
当你的程序为80分时,就是第三点没考虑。

建议:
把buy,sell合并或分开都可以,把相同价格的值分开或合并都可以,虽然后者时间效率会高一点,
但是无疑考试期间把把buy,sell合并,不把相同价格的值合并更容易写,更容易理解,也不容易写错。

 

  1 #include <iostream>
  2 #include <stdlib.h>
  3 #include <cstring>
  4 #include <algorithm>
  5 using namespace std;
  6 #define maxn 8000
  7 
  8 //The softest way to write a right code in a competition
  9 //Just time limite, we don't need to write the best code(in time aspect)
 10 
 11 struct node
 12 {
 13     double price;
 14     //mode: 1:sell 0:buy
 15     //why put sell ahead of buy?
 16     //sell:add  &  buy:delete    --- we need maximum
 17     long amount,mode;
 18 }info[maxn+1];
 19 
 20 bool cmp(struct node a,struct node b)
 21 {
 22     if (a.price<b.price)
 23         return true;
 24     else if (a.price>b.price)
 25         return false;
 26     else if (a.mode>b.mode)
 27         return true;
 28     else
 29         return false;
 30 }
 31 
 32 __int64 min(__int64 a,__int64 b)
 33 {
 34     if (a>b)
 35         return b;
 36     else
 37         return a;
 38 }
 39 
 40 int main()
 41 {
 42     long g,pos,ans,i,b[maxn+1],c[maxn+1];
 43     __int64 x,y,z,maxamount;
 44     double a[maxn+1],maxprice;
 45     char s[10];
 46     bool vis[maxn+1];
 47     //use stdlib.h faster!
 48     g=0;
 49     while (scanf("%s",s)!=EOF)
 50     {
 51         g++;
 52         if (strcmp(s,"cancel")==0)
 53         {
 54             scanf("%ld",&pos);
 55             vis[g]=false;
 56             vis[pos]=false;
 57         }
 58         else
 59         {
 60             //pay attention:%lf        double a[]
 61             scanf("%lf%ld",&a[g],&b[g]);
 62             if (strcmp(s,"sell")==0)
 63                 c[g]=1;
 64             else
 65                 c[g]=0;
 66             vis[g]=true;
 67         }
 68     }
 69     ans=0;
 70     for (i=1;i<=g;i++)
 71         if (vis[i])
 72         {
 73             info[ans].price=a[i];
 74             info[ans].amount=b[i];
 75             info[ans].mode=c[i];
 76             ans++;
 77         }
 78     sort(info,info+ans,cmp);
 79     //prove that the result of price(maximum--maxprice) is info[k].price:
 80     //If not,the nearest data that is bigger than price
 81     //result=maxresult & price>maxprice , conflict
 82     
 83     //choose
 84     //in >= po
 85     //out <= po
 86     
 87     //po assending as times by
 88     //so if maxamount is the same, use the newest
 89     
 90     //buy
 91     x=0;
 92     for (i=0;i<ans;i++)
 93         if (info[i].mode==0)
 94             x+=info[i].amount;
 95     //sell
 96     y=0;
 97     maxamount=0;
 98     for (i=0;i<ans;i++)
 99     //when po is info[i].price
100         //buy
101         if (info[i].mode==0)
102         {
103             z=min(x,y);
104             if (z>=maxamount)
105             {
106                 maxamount=z;
107                 maxprice=info[i].price;
108             }
109             x-=info[i].amount;
110         }
111         //sell
112         else
113         {
114             y+=info[i].amount;
115             z=min(x,y);
116             if (z>=maxamount)
117             {
118                 maxamount=z;
119                 maxprice=info[i].price;
120             }
121         } 
122     //数据默认maxamount>0 
123     printf("%.2lf %I64d",maxprice,maxamount);
124     return 0;
125 }

 

posted @ 2017-04-13 23:58  congmingyige  阅读(326)  评论(0编辑  收藏  举报