SuperHyperMarket(优先队列+重载)

SuperHyperMarket(优先队列+重载)

具体见代码注释

 1 /* */
 2 #include <iostream>
 3 #include <cstring>
 4 #include <queue>
 5 #include <cstdio>
 6 #include <string>
 7 #include <cmath>
 8 #include <utility>
 9 #include <algorithm>
10 using namespace std;
11 typedef long long ll;
12 const int maxn=1e5+10;
13 const int inf=0x3f3f3f3f;
14 const double pi=acos(-1.0);
15 
16 int arr[maxn];
17 struct node
18 {
19     int mem;//该队的人数
20     int num;//该队的编号
21     int b1,b2;//该队的最后两个人
22     int pi;//下一个人计算的花费
23     bool operator<(const node &a)const//重载,从小到达排,先出队小的
24     {
25         if( a.pi==pi ) return num>a.num;//编号小的
26         return pi>a.pi;//花费小的
27     }
28 } p[maxn];
29 priority_queue<node> q;
30 int loc[maxn];//标记所要去的队
31 
32 int main()
33 {
34     int n,k;
35     cin>>n>>k;
36     for(int i=1; i<=n; i++) scanf("%d",&arr[i]);
37     for(int i=1; i<=k; i++) //初始化
38     {
39         p[i].b1=p[i].b2=0;//the last purchase people
40         p[i].mem=p[i].pi=0;
41         p[i].num=i;//编号
42         q.push(p[i]);
43     }
44 
45     for(int i=1; i<=n; i++)
46     {
47         node x=q.top();
48         loc[i]=x.num;
49         q.pop();
50         x.mem++;
51         x.b2=x.b1;
52         x.b1=arr[i];
53         if( x.mem==1 )
54         {
55             x.pi=2*arr[i];//化除为乘防止精度缺失
56         }
57         else
58         {
59             x.pi=x.mem*(x.b1+x.b2);//化除为乘防止精度缺失,所以不除2
60         }
61         q.push(x);
62     }
63     for(int i=1; i<=n; i++)
64     {
65         if(i==n) printf("%d\n",loc[i]);
66         else printf("%d ",loc[i]);
67     }
68     return 0;
69 }

猜你喜欢

转载自www.cnblogs.com/wsy107316/p/11656328.html