洛谷P2827 蚯蚓 题解
洛谷P2827 蚯蚓 题解
题目描述
本题中,我们将用符号 ⌊c⌋ 表示对 c 向下取整。
蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓。
蛐蛐国里现在共有 n 只蚯蚓(nn 为正整数)。每只蚯蚓拥有长度,我们设第 ii 只蚯蚓的长度为 a_iai (i=1,2,\dots,ni=1,2,…,n),并保证所有的长度都是非负整数(即:可能存在长度为 00 的蚯蚓)。
每一秒,神刀手会在所有的蚯蚓中,准确地找到最长的那一只(如有多个则任选一个)将其切成两半。神刀手切开蚯蚓的位置由常数 pp(是满足 0 < p < 10<p<1 的有理数)决定,设这只蚯蚓长度为 xx,神刀手会将其切成两只长度分别为 \lfloor px \rfloor⌊px⌋ 和 x - \lfloor px \rfloorx−⌊px⌋ 的蚯蚓。特殊地,如果这两个数的其中一个等于 00,则这个长度为 00 的蚯蚓也会被保留。此外,除了刚刚产生的两只新蚯蚓,其余蚯蚓的长度都会增加 qq(是一个非负整常数)。
蛐蛐国王知道这样不是长久之计,因为蚯蚓不仅会越来越多,还会越来越长。蛐蛐国王决定求助于一位有着洪荒之力的神秘人物,但是救兵还需要 mm 秒才能到来……(mm 为非负整数)
蛐蛐国王希望知道这 mm 秒内的战况。具体来说,他希望知道:
- mm 秒内,每一秒被切断的蚯蚓被切断前的长度(有 mm 个数);
- mm 秒后,所有蚯蚓的长度(有 n + mn+m 个数)。
蛐蛐国王当然知道怎么做啦!但是他想考考你……
输入输出格式
输入格式:
第一行包含六个整数 n,m,q,u,v,tn,m,q,u,v,t,其中:n,m,qn,m,q 的意义见【问题描述】;u,v,tu,v,t 均为正整数;你需要自己计算 p=u / vp=u/v(保证 0 < u < v0<u<v);tt 是输出参数,其含义将会在【输出格式】中解释。
第二行包含 nn 个非负整数,为 a_1, a_2, \dots, a_na1,a2,…,an,即初始时 nn 只蚯蚓的长度。
同一行中相邻的两个数之间,恰好用一个空格隔开。
输出格式:
第一行输出 \left \lfloor \frac{m}{t} \right \rfloor⌊tm⌋ 个整数,按时间顺序,依次输出第 tt 秒,第 2t2t 秒,第 3t3t 秒,……被切断蚯蚓(在被切断前)的长度。
第二行输出 \left \lfloor \frac{n+m}{t} \right \rfloor⌊tn+m⌋ 个整数,输出 mm 秒后蚯蚓的长度;需要按从大到小的顺序,依次输出排名第 tt,第 2t2t,第 3t3t,……的长度。
同一行中相邻的两个数之间,恰好用一个空格隔开。即使某一行没有任何数需要输出,你也应输出一个空行。
请阅读样例来更好地理解这个格式。
输入输出样例
3 7 1 1 3 1
3 3 2
3 4 4 4 5 5 6
6 6 6 5 5 4 4 3 2 2
3 7 1 1 3 2
3 3 2
4 4 5
6 5 4 3 2
3 7 1 1 3 9
3 3 2
//空行
2
说明
【样例解释1】
在神刀手到来前:33只蚯蚓的长度为3,3,23,3,2。
11秒后:一只长度为33的蚯蚓被切成了两只长度分别为11和22的蚯蚓,其余蚯蚓的长度增加了11。最终44只蚯蚓的长度分别为(1,2),4,3(1,2),4,3。括号表示这个位置刚刚有一只蚯蚓被切断
22秒后:一只长度为44的蚯蚓被切成了11和33。55只蚯蚓的长度分别为:2,3,(1,3),42,3,(1,3),4。
3秒后:一只长度为44的蚯蚓被切断。66只蚯蚓的长度分别为:3,4,2,4,(1,3)3,4,2,4,(1,3)。
44秒后:一只长度为44的蚯蚓被切断。77只蚯蚓的长度分别为:4,(1,3),3,5,2,44,(1,3),3,5,2,4。
55秒后:一只长度为55的蚯蚓被切断。88只蚯蚓的长度分别为:5,2,4,4,(1,4),3,55,2,4,4,(1,4),3,5。
66秒后:一只长度为55的蚯蚓被切断。99只蚯蚓的长度分别为:(1,4),3,5,5,2,5,4,6(1,4),3,5,5,2,5,4,6。
77秒后:一只长度为66的蚯蚓被切断。1010只蚯蚓的长度分别为:2,5,4,6,6,3,6,5,(2,4)2,5,4,6,6,3,6,5,(2,4)。所以,77秒内被切断的蚯蚓的长度依次为3,4,4,4,5,5,63,4,4,4,5,5,6。77秒后,所有蚯蚓长度从大到小排序为6,6,6,5,5,4,4,3,2,26,6,6,5,5,4,4,3,2,2
【样例解释2】
这个数据中只有t=2t=2与上个数据不同。只需在每行都改为每两个数输出一个数即可。
虽然第一行最后有一个66没有被输出,但是第二行仍然要重新从第二个数再开始输出。
【样例解释3】
这个数据中只有t=9t=9与上个数据不同。
注意第一行没有数要输出,但也要输出一个空行。
【数据范围】
洛谷丑恶的标志↑
题目将就着看吧,本人太弱不想改。
思路:
首先,展示一下35分的代码。
#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;
inline int read(){
int ret=,f=;char ch=getchar();
while (ch<''||ch>'') {if (ch=='-') f=-f;ch=getchar();}
while (ch>=''&&ch<='') ret=ret*+ch-'',ch=getchar();
return ret*f;
}
inline void write(int zx){
if(zx<){zx=-zx;putchar('-');}
if(zx<) putchar(zx+'');
else{
write(zx/);
putchar(zx%+'');
}
}
int n,m,q,u,v,t,a[];
bool cmp(int x,int y){
return x>y;
}
int main(){
freopen("earthworm.in","r",stdin);freopen("earthworm.out","w",stdout);
n=read();m=read();q=read();u=read();v=read();t=read();
double p=(double)u/(double) v;
for(int i=;i<=n;i++) a[i]=read();
int l=n;
int v=;
for(int i=;i<=m;i++){
sort(a+,a+n+,cmp);
int tot=n;
if(i==t*v){
cout<<a[]<<" ";
++v;
}
int Nxt=p*((double)a[]);
++tot;
a[tot]=Nxt;
a[]=a[]-Nxt;
for(int j=;j<=tot-;j++) a[j]+=q;
n=tot;
}
cout<<endl;
sort(a+,a+n+,cmp);
for(int i=;i<=(l+m)/t;i++){
cout<<a[t*i]<<" ";
}
cout<<endl;
return ;
}
好了,100分的思路是什么呢?
如果要有满分,要发现隐含的单调性:先切的对应长度一定比后切的长。
也就是说,一开始,q2、q3是空的,q1放置原始长度。q2可以放p*最大值,q3可以放最大值-p*最大值。
那么最大值怎么求呢?
当然是求三个队列的顶端的最大值(=@__@=)。
啦啦。。。展示代码:↓
#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;//长长的头文件,为了卡常(逃:
inline int read(){
int ret=,f=;char ch=getchar();
while (ch<''||ch>'') {if (ch=='-') f=-f;ch=getchar();}
while (ch>=''&&ch<='') ret=ret*+ch-'',ch=getchar();
return ret*f;
}//读入优化
inline void write(int zx){
if(zx<){zx=-zx;putchar('-');}
if(zx<) putchar(zx+'');
else{
write(zx/);
putchar(zx%+'');
}
}//输出优化
int n,m,q,u,v,t,a[];
struct node{
int x,Time;
};
queue<node> q1,q2,q3;
bool jhjgay(int x,int y){return x>y;}
int main(){
freopen("earthworm.in","r",stdin);freopen("earthworm.out","w",stdout);//freopen文件就不用多说了吧
n=read();m=read();q=read();u=read();v=read();t=read();
double p=(double)u/(double)v;
for(int i=;i<=n;i++) a[i]=read();
sort(a+,a+n+,jhjgay);
for(int i=;i<=n;i++) q1.push((node){a[i],});
for(int i=;i<m;i++){
int X=-2e9;
if(!q1.empty()) X=max(X,q1.front().x+q*(i-q1.front().Time));
if(!q2.empty()) X=max(X,q2.front().x+q*(i-q2.front().Time));
if(!q3.empty()) X=max(X,q3.front().x+q*(i-q3.front().Time));
//------------------------------分割线-----------------------------------------
if(!q1.empty()&&X==(q1.front().x+q*(i-q1.front().Time))) q1.pop();else
if(!q2.empty()&&X==(q2.front().x+q*(i-q2.front().Time))) q2.pop();else
if(!q3.empty()&&X==(q3.front().x+q*(i-q3.front().Time))) q3.pop();else ;//求三个队列的队顶的最大值,然后在减去(注意有可能队顶是相同的,所以一定要写else)
if((i+)%t==) write(X),putchar(' ');(输出,具体看题面)
int bc=p*(double)X;
q2.push((node){bc,i+});q3.push((node){X-bc,i+}); //将取得的最大值切割
}
puts("");//换行不要忘记哦
int Num=;
while((!q1.empty())||(!q2.empty())||(!q3.empty())){
int X=-2e9;++Num;
if(!q1.empty()) X=max(X,q1.front().x+q*(m-q1.front().Time));
if(!q2.empty()) X=max(X,q2.front().x+q*(m-q2.front().Time));
if(!q3.empty()) X=max(X,q3.front().x+q*(m-q3.front().Time));
//------------------------------分割线-----------------------------------------
if(!q1.empty()&&X==(q1.front().x+q*(m-q1.front().Time))) q1.pop();else
if(!q2.empty()&&X==(q2.front().x+q*(m-q2.front().Time))) q2.pop();else
if(!q3.empty()&&X==(q3.front().x+q*(m-q3.front().Time))) q3.pop();else ;//同上
if(Num==t) write(X),putchar(' '),Num=;//输出
}
puts("");//换行不要忘记哦
return ;
}
洛谷P2827 蚯蚓 题解的更多相关文章
- NOIP 2016 洛谷 P2827 蚯蚓 题解
题目传送门 展开 题目描述 本题中,我们将用符号[c]表示对c向下取整,例如:[3.0」= [3.1」=[3.9」=3.蛐蛐国最近蚯蚓成灾了!隔壁跳 蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手 ...
- 洛谷p2827蚯蚓题解
题目 算法标签里的算法什么的都不会啊 什么二叉堆?? qbxt出去学习的时候讲的,一段时间之前做的,现在才写到博客上的 维护3个队列,队列1表示最开始的蚯蚓,队列2表示每一次被切的蚯蚓被分开的较长的那 ...
- 洛谷 P2827 蚯蚓 题解
每日一题 day32 打卡 Analysis 我们可以想一下,对于每一秒除了被切的哪一个所有的蚯蚓都增长Q米,我们来维护3个队列,队列1表示最开始的蚯蚓,队列2表示每一次被切的蚯蚓被分开的较长的那一部 ...
- 洛谷 P2827 蚯蚓 解题报告
P2827 蚯蚓 题目描述 本题中,我们将用符号 \(\lfloor c \rfloor\) 表示对 \(c\) 向下取整,例如:\(\lfloor 3.0 \rfloor = \lfloor 3.1 ...
- 洛谷——P2827 蚯蚓
P2827 蚯蚓 题目描述 本题中,我们将用符号 \lfloor c \rfloor⌊c⌋ 表示对 cc 向下取整,例如:\lfloor 3.0 \rfloor = \lfloor 3.1 \rflo ...
- 洛谷P2827 蚯蚓——思路题
题目:https://www.luogu.org/problemnew/show/P2827 思路... 用优先队列模拟做的话,时间主要消耗在每次的排序上: 能不能不要每次排序呢? 关注先后被砍的两条 ...
- 洛谷 P2827 蚯蚓
题目描述 本题中,我们将用符号\lfloor c \rfloor⌊c⌋表示对c向下取整,例如:\lfloor 3.0 \rfloor= \lfloor 3.1 \rfloor=\lfloor 3.9 ...
- 洛谷P2827 蚯蚓(单调队列)
题意 初始时有$n$个蚯蚓,每个长度为$a[i]$ 有$m$个时间,每个时间点找出长度最大的蚯蚓,把它切成两段,分别为$a[i] * p$和$a[i] - a[i] * p$,除这两段外其他的长度都加 ...
- 洛谷P2827蚯蚓
题目 堆+模拟,还有一个小优化(优化后跟堆关系不大,而是类似于贪心). 如果不加优化的话,卡常可以卡到85. 思路是对于对每一秒进行模拟,用堆来维护动态的最大值,然后对于每个长度都加q的情况可以用一个 ...
随机推荐
- C++11——引入的新关键字
1.auto auto是旧关键字,在C++11之前,auto用来声明自动变量,表明变量存储在栈,很少使用.在C++11中被赋予了新的含义和作用,用于类型推断. auto关键字主要有两种用途:一是在变量 ...
- Linux之Makefile20160707
说一下LINUX下的Makefile,直接根据实际碰到的Makefile进行解读: 当make的目标为all时,-C $(KDIR) 指明跳转到内核源码目录下读取那里的Makefile:M=$(PWD ...
- js实现数组排序
1. JavaScript的sort()方法 var array = [1,4,-8,-3,6,12,9,8]; function compare(val1,val2){ return val1-va ...
- CSS属性的私有前缀
在CSS属性能中,我们常常能看到-webkit-,-moz-之类的前缀,这种就叫做浏览器私有前缀,是浏览器对于新CSS属性的一个提前支持.-webkit-是webkit内核的,-moz-是Firefo ...
- PhotoSwipe 图片浏览插件使用方法
一.简介 PhotoSwipe 是专为移动触摸设备设计的相册/画廊.兼容所有iPhone.iPad.黑莓6+,以及桌面浏览器.底层实现基于HTML/CSS/JavaScript,是一款免费开源的相册产 ...
- 【BZOJ】1087: [SCOI2005]互不侵犯King
[算法]状态压缩型DP [题解]http://www.cnblogs.com/xtx1999/p/4620227.html (orz) https://www.cnblogs.com/zbtrs/p/ ...
- 用Vue来实现图片上传多种方式
没有业务场景的功能都是耍流氓,那么我们先来模拟一个需要实现的业务场景.假设我们要做一个后台系统添加商品的页面,有一些商品名称.信息等字段,还有需要上传商品轮播图的需求. 我们就以Vue.Element ...
- H5调试工具 - weinre远程调试工具
weinre 简介 weinre 是一款类似于firebug 和Web Inspector的网页调试工具, 它的不同之处在于可以用于进行远程调试,比如调试手机上面的网页. 安装 weinre(运行在n ...
- [006] largest_common_substring
[Description] Given two different strings, find the largest successive common substring. e.g. str1[] ...
- python基础===利用unittest进行测试用例执行的几种方式
利用python进行测试时,测试用例的加载方式有2种: 一种是通过unittest.main()来启动所需测试的测试模块: 一种是添加到testsuite集合中再加载所有的被测试对象,而tests ...