题目链接

problem

火车上的一列人要去排队接水。每个人都会在某个特定的时刻口渴。口渴之后他要去排队接水,如果他前面的座位有人已经在排队或者正在接水,那么他就不会去排队。否则他就会去排队。每个人接水都为一个相同的时间P。问每个人接完水的时间。

solution

其实模拟即可。注意题目的要求。如果一个人口渴的时候已经在排队的人中最靠前的位置也在他后面,那么他就要去排队。否则就把他扔到一个按位置从小到大排序的优先队列里面。然后模拟就行了。

code

#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<ctime>
#include<set>
using namespace std;
typedef long long ll;
const int N = 100010;
ll read() {
ll x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;c = getchar();
}
while(c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
struct node {
int pos,w;
}a[N];
bool operator < (const node &A,const node &B) {
return A.pos > B.pos;
}
priority_queue<node>q;
bool cmp(const node &A,const node &B) {
return A.w == B.w ? A.pos < B.pos : A.w < B.w;
}
set<int>s;
ll ans[N];
queue<node>tq;
int main() {
int n = read(),P = read();
for(int i = 1;i <= n;++i) {
a[i].pos = i;a[i].w = read();
} sort(a + 1,a + n + 1,cmp); int p = 1;
ll now = a[1].w;
s.insert(n + 1);
while(p <= n || !tq.empty() || !q.empty()) {
if(!tq.empty()) {
node k = tq.front();tq.pop();
now += P;
ans[k.pos] = now; while(p <= n && a[p].w < now) {
if(a[p].pos < *s.begin()) {
s.insert(a[p].pos);
tq.push(a[p++]);
}
else {
q.push(a[p++]);
}
}
s.erase(k.pos);
} if(tq.empty() && q.empty())
now = max(now,(ll)a[p].w); while(p <= n && a[p].w <= now) q.push(a[p++]);
int t = *s.begin();
if(!q.empty() && q.top().pos <= t) { tq.push(q.top());
s.insert(q.top().pos);
q.pop();
}
} for(int i = 1;i <= n;++i) printf("%I64d ",ans[i]);
return 0;
}

CF1248E Queue in the Train的更多相关文章

  1. Codeforces Round #594 (Div. 1) C. Queue in the Train 模拟

    C. Queue in the Train There are

  2. Codeforces 1239C. Queue in the Train

    传送门 事实上就是模拟 搞一个优先队列维护一下事件结构体:时间,人的编号,入队还是出队 再维护两个 $set$ ,队列内的人 $inQueue$ ,想要进入队列内的人 $want$ 然后模拟模拟模拟! ...

  3. 并不对劲的CF1239B&C&D Programming Task in the Train to Catowice City

    CF1239B The World Is Just a Programming Task 题目描述 定义一个括号序列s是优秀的,当且仅当它是以下几种情况的一种: 1.|s|=0 2.s='('+t+' ...

  4. tf.train.string_input_producer()

    处理从文件中读数据 官方说明 简单使用 示例中读取的是csv文件,如果要读tfrecord的文件,需要换成 tf.TFRecordReader import tensorflow as tf file ...

  5. Tensorflow的CNN教程解析

    之前的博客我们已经对RNN模型有了个粗略的了解.作为一个时序性模型,RNN的强大不需要我在这里重复了.今天,让我们来看看除了RNN外另一个特殊的,同时也是广为人知的强大的神经网络模型,即CNN模型.今 ...

  6. TensorFlow实践笔记(一):数据读取

    本文整理了TensorFlow中的数据读取方法,在TensorFlow中主要有三种方法读取数据: Feeding:由Python提供数据. Preloaded data:预加载数据. Reading ...

  7. TensorFlow入门学习(让机器/算法帮助我们作出选择)

    catalogue . 个人理解 . 基本使用 . MNIST(multiclass classification)入门 . 深入MNIST . 卷积神经网络:CIFAR- 数据集分类 . 单词的向量 ...

  8. 学习笔记TF040:多GPU并行

    TensorFlow并行,模型并行,数据并行.模型并行根据不同模型设计不同并行方式,模型不同计算节点放在不同硬伯上资源运算.数据并行,比较通用简便实现大规模并行方式,同时使用多个硬件资源计算不同bat ...

  9. TensorFlow读取CSV数据(批量)

    直接上代码: # -*- coding:utf-8 -*- import tensorflow as tf def read_data(file_queue): reader = tf.TextLin ...

随机推荐

  1. JVM学习分享-练习题

    package org.fenixsoft.clazz; public class TestClass { private int m; public int inc() { return m + 1 ...

  2. swift冒泡排序,swift快速排序,swift归并排序,swift插入排序,swift基数排序

    import UIKit /// 冒泡 /// ///时O(n2),空O(1) 稳定排序 func Mysort(arr:[Int]) -> [Int]{ var transArr = arr ...

  3. InnoDB On-Disk Structures(二)--Indexes (转载)

    转载.节选于 https://dev.mysql.com/doc/refman/8.0/en/innodb-indexes.html This section covers topics relate ...

  4. 在 Cocos2d-x 中添加自己的微博链接

    配置:OS X 10.10 + Xcode 6.0 + Cocos2d-x-3.2 一.Android 端代码 1.在 Cocos2dxActivity.java 中添加openUrl函数并导入响应包 ...

  5. 构造函数new运算符进行了哪些操作

    new 运算符 1,实例化一个对象 2,将构造函数prototype对象赋值给对象__proto__属性 3,将对象作为函数this传进去,函数有return 并且是对象的话,就直接返回return的 ...

  6. Metasploit从文件中读取目标地址

    本文简单介绍如何使用Metasploit从文件中读取目标地址,来执行检测. 以检测MS17-010漏洞为例,在设定RHOSTS参数时,可设定目标地址范围和CIDR地址块,设定单个IP的目标也是可以的. ...

  7. SpringBoot2.0 整合 Shiro 框架,实现用户权限管理

    本文源码:GitHub·点这里 || GitEE·点这里 一.Shiro简介 1.基础概念 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.作为一款安全 ...

  8. 指定节点滚动到屏幕中间的js

    父节点的class是slimScrollDiv 子节点的class是fa-warning 执行这个js document.getElementsByClassName("slimScroll ...

  9. In .net 4.8,calculate the time cost of serialization in BinaryFormatter,NewtonSoft.json,and System.Text.Json.JsonSerializer.Serialize

    using ConsoleApp390.Model; using Newtonsoft.Json; using System; using System.Collections.Generic; us ...

  10. 利用Python开发智能阅卷系统

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 机器学习与统计学 PS:如有需要Python学习资料的小伙伴可以加 ...