洛谷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的情况可以用一个 ...
随机推荐
- centos下 安装python相关
centos 安装python相关 python3 https://blog.csdn.net/tanxiaob/article/details/78725301 yum -y install zli ...
- 如何将指定文件或文件夹直接提交到svn指定目录?
如何将指定文件或文件夹直接提交到svn指定目录? 一般我们都是按以下步骤操作的: 1.先将那个目录checkout下来 2.将要添加的文件或者文件夹放到这个目录中 3.右击文件执行svn菜单中的add ...
- JDK9新特性实战:简化流关闭新姿势。
做Java开发的都知道,每个资源的打开都需要对应的关闭操作,不然就会使资源一直占用而造成资源浪费,从而降低系统性能. 关于资源的关闭操作,从JDK7-JDK9有了不少的提升及简化. JDK6 在JDK ...
- 12-5-上下文this
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python类的静态方法和类方法区别
先看语法,python 类语法中有三种方法,实例方法,静态方法,类方法. # coding:utf-8 class Foo(object): """类三种方法语法形式&q ...
- 【JZOJ3423】Vani和Cl2捉迷藏&【BZOJ1143】祭祀river
description vani和cl2在一片树林里捉迷藏-- 这片树林里有N座房子,M条有向道路,组成了一张有向无环图. 树林里的树非常茂密,足以遮挡视线,但是沿着道路望去,却是视野开阔.如果从房子 ...
- MySQL系列(十二)--如何设计一个关系型数据库(基本思路)
设计一个关系型数据库,也就是设计RDBMS(Relational Database Management System),这个问题考验的是对RDBMS各个模块的划分, 以及对数据库结构的了解.只要讲述 ...
- LUOGU P1313 计算系数 (组合数学)
解题思路 比较简单的题,用二项式定理即可. #include<iostream> #include<cstdio> #include<cstring> #inclu ...
- jquery.cookie.js时间设置
var expiresDate= new Date(); expiresDate.setTime(expiresDate.getTime() + (120*60*1000)); $.cookie('u ...
- LinkedList集合 实现栈和队列
LinkedList集合的底层是链表结构实现的,所以可以模拟栈(先进后出)和队列(先进先出). 方法: addFirst() //添加元素到列表的起始位置 addLast() //添加元素到列表的结束 ...