洛谷P2827 蚯蚓
pts85/90(90应该是个意外,第一次交是90之后都是85了):
优先队列模拟题意
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int n,m,q,u,v,t,tim;
double p;
priority_queue<int>qq;
int main()
{
scanf("%d%d%d%d%d%d",&n,&m,&q,&u,&v,&t);
p=1.0*u/(1.0*v);
for(int i=,x;i<=n;i++){
scanf("%d",&x);
qq.push(x);
}
while(m--){
tim++;
int x=qq.top();
qq.pop();
if(tim%t==)printf("%d ",x+(tim-)*q);
int y=(int)(p*(double)(x+(tim-)*q));
x=(x+(tim-)*q)-y;
qq.push(x-tim*q),qq.push(y-tim*q);
}
printf("\n");
int now=;
while(qq.size()){
now++;
if(now%t==){
printf("%d ",qq.top()+tim*q);
}
qq.pop();
}
return ;
}
优先队列
顺手练一下手写二叉堆
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,q,u,v,t,tim,a[*],num;
double p;
bool cmp(int x,int y){
return x>y;
}
int main()
{
scanf("%d%d%d%d%d%d",&n,&m,&q,&u,&v,&t);
p=1.0*u/(1.0*v);
for(int i=,x;i<=n;i++){
scanf("%d",&a[i]);
x=i;
while(x>&&a[x]>a[x>>]){
swap(a[x],a[x>>]);
x>>=;
}
}
while(m--){
tim++;
int x=a[];
if(tim%t==)printf("%d ",x+(tim-)*q);
int y=(int)(p*(double)(x+(tim-)*q));
x=(x+(tim-)*q)-y;
a[]=x-tim*q;
int now=;
while((a[now]<a[now<<]||a[now]<a[now<<|])&&(now<<|)<=n){
if(a[now<<]<a[now<<|]){
swap(a[now],a[now<<|]);
now<<=;
now|=;
}
else{
swap(a[now],a[now<<]);
now<<=;
}
}
if(a[now]<a[now<<]&&(now<<)<=n){
swap(a[now],a[now<<]);
}
a[++n]=y-tim*q;
now=n;
while(now>&&a[now]>a[now>>]){
swap(a[now],a[now>>]);
now>>=;
}
}
printf("\n");
sort(a+,a+n+,cmp);
for(int i=;i<=n;i++){
if(i%t==){
printf("%d ",a[i]+tim*q);
}
}
return ;
}
手写二叉堆
正解:
发现先切的蚯蚓产生的长段永远不小于后切的蚯蚓产生的长段,短段也是。因为从准备切先切的开始,后切的生长t个单位长度的同时,先前切好的两段都生长了t-1个单位长度。
切好的长段集合和短段集合本身就具有单调性。
用三个队列分别储存未切的,切好的长段,切好的短段,每次从三个队头寻找最大的进行操作。
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,q,u,v,t,tim,maxx,pos,a[*],inf=;
double p;
queue<int>qq[];
bool cmp(int x,int y){
return x>y;
}
int main()
{
scanf("%d%d%d%d%d%d",&n,&m,&q,&u,&v,&t);
p=1.0*u/(1.0*v);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
sort(a+,a+n+);
for(int i=n;i>=;i--)qq[].push(a[i]);
while(m--){
tim++;
maxx=pos=-inf;
if(qq[].front()>maxx&&qq[].size())maxx=qq[].front(),pos=;
if(qq[].front()>maxx&&qq[].size())maxx=qq[].front(),pos=;
if(qq[].front()>maxx&&qq[].size())maxx=qq[].front(),pos=;
qq[pos].pop();
if(tim%t==)printf("%d ",maxx+(tim-)*q);
int y=(int)(p*(double)(maxx+(tim-)*q));
maxx=(maxx+(tim-)*q)-y;
qq[].push(max(maxx,y)-tim*q),qq[].push(min(maxx,y)-tim*q);
}
printf("\n");
n=;
while(qq[].size()){
a[++n]=qq[].front();
qq[].pop();
}
while(qq[].size()){
a[++n]=qq[].front();
qq[].pop();
}
while(qq[].size()){
a[++n]=qq[].front();
qq[].pop();
}
sort(a+,a+n+,cmp);
for(int i=;i<=n;i++){
if(i%t==){
printf("%d ",a[i]+tim*q);
}
}
return ;
}
洛谷P2827 蚯蚓的更多相关文章
- 洛谷P2827 蚯蚓 题解
洛谷P2827 蚯蚓 题解 题目描述 本题中,我们将用符号 ⌊c⌋ 表示对 c 向下取整. 蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓. 蛐蛐国里现 ...
- NOIP 2016 洛谷 P2827 蚯蚓 题解
题目传送门 展开 题目描述 本题中,我们将用符号[c]表示对c向下取整,例如:[3.0」= [3.1」=[3.9」=3.蛐蛐国最近蚯蚓成灾了!隔壁跳 蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手 ...
- 洛谷 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蚯蚓题解
题目 算法标签里的算法什么的都不会啊 什么二叉堆?? qbxt出去学习的时候讲的,一段时间之前做的,现在才写到博客上的 维护3个队列,队列1表示最开始的蚯蚓,队列2表示每一次被切的蚯蚓被分开的较长的那 ...
- 洛谷 P2827 蚯蚓 题解
每日一题 day32 打卡 Analysis 我们可以想一下,对于每一秒除了被切的哪一个所有的蚯蚓都增长Q米,我们来维护3个队列,队列1表示最开始的蚯蚓,队列2表示每一次被切的蚯蚓被分开的较长的那一部 ...
- 洛谷P2827蚯蚓
题目 堆+模拟,还有一个小优化(优化后跟堆关系不大,而是类似于贪心). 如果不加优化的话,卡常可以卡到85. 思路是对于对每一秒进行模拟,用堆来维护动态的最大值,然后对于每个长度都加q的情况可以用一个 ...
随机推荐
- Git log和reflog
1.log log命令可以显示所有提交过的版本信息.显示信息如下: Administrator@USER-20171026MG MINGW64 ~/Desktop/lyf (master) $ gi ...
- appium + python 自动化调试手机时 UiAutomator exited unexpectedly with code 0, signal null
放上appium报错图,appium在手机里安装了appium setting 和unlock 软件,输入法也被变成了appium input ,但是就是点不到目标软件,手机也可以被cmd adb ...
- 「APIO 2019」奇怪装置
题目 考虑推柿子 最开始的想法是如果两个\(t\)在\(mod\ B\)意义下相等,那么只需要比较一下\((t+\left \lfloor \frac{t}{B}\rfloor \right)mod\ ...
- Mapped Statements collection does not contain value for xxx.xxx 错误原因&解决方案
先贴出详细的报错信息 2019-11-05 10:10:00 [executor-1] ERROR [org.quartz.core.JobRunShell:225] - Job DEFAULT.ef ...
- mysqldump使用记录
#导出单个数据库:结构 无数据 [root@localhost ~]#mysqldump -h127.0.0.1 -uroot -p --opt --no-data db_name >~/db_ ...
- 解决Mysql因内存不足启动失败的问题
参考:https://www.jb51.net/article/136432.htm 一.查看内存 free -h free -m 二.解决方法: 1.增加swap交换空间解决问题: dd if=/d ...
- 筛法求欧拉函数(poj2478
求1-n的欧拉函数的值 #include <iostream> #include <cstdio> #include <queue> #include <al ...
- JSM 基础
JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信 ...
- 前端基础之BOM与DOM操作
目录 BOM操作 navigator对象 screen对象 history对象 localtion对象 弹出框 计时 setTimeout() clearTimeout() setInterval() ...
- PageRank算法R语言实现
PageRank算法R语言实现 Google搜索,早已成为我每天必用的工具,无数次惊叹它搜索结果的准确性.同时,我也在做Google的SEO,推广自己的博客.经过几个月尝试,我的博客PR到2了,外链也 ...