蚯蚓

我的做法:开一个优先队列

用一个变量记录x队列里的蚯蚓增长了多少

被切割完的蚯蚓在放进去时长度减去x

取出队首时蚯蚓的长度加上x

得分:80

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 #define DB double
 4 using namespace std;
 5 int n,m,qq,t,x,y;
 6 DB p;
 7 struct node{
 8     int o;
 9     bool operator <(const node &x) const{
10      return o<x.o;
11     }
12 };
13 priority_queue<node>q;
14 int main()
15 {
16     scanf("%d%d%d%d%d%d",&n,&m,&qq,&x,&y,&t);
17     p=(DB)x/y;x=0;
18     for(int i=1;i<=n;++i)
19     {
20         scanf("%d",&x);
21         q.push((node){x});
22     }x=0;
23     for(int k=1,L,R;k<=m;++k)
24     {
25         y=q.top().o+x;q.pop();
26         if(k%t==0) printf("%d ",y);
27         L=y*p;R=y-L;x+=qq;
28         q.push((node){L-x});
29         q.push((node){R-x});
30     }
31     putchar('\n');
32     for(int k=1;k<=n+m;++k)
33     {
34         y=q.top().o+x;q.pop();
35         if(k%t==0) printf("%d ",y);
36     }
37     return 0;
38 }
代码

优化:

我们可以发现以下性质:先切割的一定比后切割的要长

我们开三个队列

q1[]存放原来蚯蚓(只拿不放) q2[]存放切割后的较长的蚯蚓

q3[]存放切割后的较短的蚯蚓

每次从三个队列中取出队首较大的进行操作即可

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 #define DB double
 4 using namespace std;
 5 const int N=1e7+10;
 6 int n,m,qq,t,x,y,a[N];
 7 DB p;
 8 int q1[N],q2[N],q3[N];
 9 int hh1,tt1,hh2,tt2,hh3,tt3;
10 int main()
11 {
12     scanf("%d%d%d%d%d%d",&n,&m,&qq,&x,&y,&t);
13     memset(q1,128,sizeof(q1));
14     memset(q2,128,sizeof(q2));
15     memset(q3,128,sizeof(q3));
16     p=(DB)x/y;
17     hh1=hh2=hh3=1;
18     for(int i=1;i<=n;++i) scanf("%d",&a[i]);
19     sort(a+1,a+n+1);
20     for(int i=n;i>=1;--i) q1[++tt1]=a[i];
21     x=0;y=0;
22     for(int k=1,L,R;k<=m;++k)
23     {
24         if(q1[hh1]>=q2[hh2] && q1[hh1]>=q3[hh3])y=q1[hh1]+x,hh1++;
25         else if(q2[hh2]>=q1[hh1] && q2[hh2]>=q3[hh3])y=q2[hh2]+x,hh2++;
26         else if(q3[hh3]>=q2[hh2] && q3[hh3]>=q1[hh1])y=q3[hh3]+x,hh3++;
27         x+=qq;
28         if(k%t==0) printf("%d ",y);
29         L=p*y;R=y-L;if(L<R) swap(L,R);
30         q2[++tt2]=L-x;q3[++tt3]=R-x;
31     }
32     putchar('\n');y=0;
33     for(int k=1;k<=n+m;++k)
34     {
35         if(q1[hh1]>=q2[hh2] && q1[hh1]>=q3[hh3])y=q1[hh1]+x,hh1++;
36         else if(q2[hh2]>=q1[hh1] && q2[hh2]>=q3[hh3])y=q2[hh2]+x,hh2++;
37         else if(q3[hh3]>=q2[hh2] && q3[hh3]>=q1[hh1])y=q3[hh3]+x,hh3++;
38         if(k%t==0) printf("%d ",y);
39     }
40     return 0;
41 }
代码
posted @ 2018-04-04 09:26  月亮茶  阅读(136)  评论(0编辑  收藏  举报