题意:
n 只蚯蚓,m 秒后,每秒递增 q,p=u/v 切的系数,t 询问系数
思路:
每次取最长的蚯蚓,优先队列很好想。
但本题一个麻烦之处在于如何更新队列中蚯蚓的状态,也就是不断递增。
因为当我们取要切的蚯蚓时,它在这一秒是不增的,而队列中是递增的。我们其实可以只记录一下总共增的,因为大家都在增,相对的,大家都不变。
关键是处理切的这个蚯蚓:
队列里的其实都在增,只是相对不变,而“我”在这一秒切完后是不增的,相对的,“我”就是在减【最后会加回来】
然后就有85分啦:
#include<stdio.h> #include<algorithm> #include<queue> #include<math.h> #define db double using namespace std; const int mxn=1e7; int n,m,t,d; double u,v; int tot,tim[mxn],a[mxn]; priority_queue<int> q; void file() { freopen("earthworm.in","r",stdin); freopen("earthworm.out","w",stdout); } int main() { // file(); scanf("%d%d%d%lf%lf%d",&n,&m,&d,&u,&v,&t); double p=u/v; for(int i=1;i<=n;++i) { int x;scanf("%d",&x); q.push(x); } for(int i=0;i<m;++i) { int x=q.top()+tot;q.pop(); tim[i+1]=x; int s1=floor((db)x*p); int s2=x-s1; s1=s1-tot-d;s2=s2-tot-d; q.push(s1);q.push(s2); tot+=d; } for(int i=t;i<=m;i+=t) printf("%d ",tim[i]); printf(" "); for(int i=1;i<=n+m;++i) { if(i%t==0) { printf("%d ",q.top()+tot); } q.pop(); } return 0; }
什么?这还不是正解?
因为我们调用的是STL的优先队列,就要考虑是否超时的问题。
原来的基础上加上一个显然的结论,就能避免多次使用优先队列导致TLE
后切的蚯蚓一定比先切的短
对于切过的蚯蚓,只需要用普通的队列就行啦
100分代码
#include<stdio.h> #include<algorithm> #include<queue> #include<math.h> #define db double using namespace std; const int mxn=1e7; int n,m,t,d,h=1,h1=1,h2=1,t1,t2; double u,v; int tot,tim[mxn],a[mxn],c1[mxn],c2[mxn]; priority_queue<int> q; bool cmp(int x,int y) { return x>y; } void file() { freopen("earthworm.in","r",stdin); freopen("earthworm.out","w",stdout); } int main() { // file(); scanf("%d%d%d%lf%lf%d",&n,&m,&d,&u,&v,&t); double p=u/v; for(int i=1;i<=n;++i) scanf("%d",&a[i]); sort(a+1,a+1+n,cmp); int x; for(int i=0;i<m;++i) { if(h>n) { if(c1[h1]>c2[h2]) x=c1[h1++]+tot; else x=c2[h2++]+tot; } else if(a[h]>=c1[h1] && a[h]>=c2[h2]) x=a[h++]+tot; else if(c1[h1]>c2[h2]) x=c1[h1++]+tot; else x=c2[h2++]+tot; tim[i+1]=x; int s1=(db)x*p; int s2=x-s1; s1=s1-tot-d;s2=s2-tot-d; c1[++t1]=s1,c2[++t2]=s2; tot+=d; } for(int i=t;i<=m;i+=t) printf("%d ",tim[i]); printf(" "); for(int i=h1;i<=t1;++i) q.push(c1[i]); for(int i=h2;i<=t2;++i) q.push(c2[i]); for(int i=h;i<=n;++i) q.push(a[i]); for(int i=1;i<=n+m;++i) { if(i%t==0) { printf("%d ",q.top()+tot); } q.pop(); } return 0; }