题目大意:

企鹅国正在举办全面运动会,第一项比赛就是跑步。N 个人在圆形跑道上跑步,他们都有各自的速度和起点。但这个跑步规则很奇怪,当两个人相遇的时候编号较小的就会出局,当场上剩下最后一个人的时候跑步就结束了。豆豆想知道多长时间游戏会结束?

输入格式

第一行一个整数 T 表示数据组数;

每组数据的第一行是两个整数 N 和 L ,表示参赛人数以及跑道长度。

接下来一行有 N 个不同的整数 Di,表示每个人的起点。

接下来一行有 N 个不同的整数 Vi,表示每个人的跑步速度,如果速度为负数,就是在反着跑。

输出格式

对于每组数据,以最简分数形式表示游戏结束的时间。

数据范围

对于 30% 的数据,2≤n≤100, 1≤L≤200;

对于 60% 的数据,2≤n≤103。

对于 100% 的数据,2≤n≤105,T≤5, 1≤L≤109, 0≤Di<L, 0≤|Vi|≤109;

题目分析:

双向链表的运用:首先要知道最先相遇的肯定是相邻的两个人(因为任何一方都不能越过另一个人),那么将每个人按照起点排序,将相邻两个人的相遇时间放入优先队列,每次取出时间最短的两个人,将编号较小的那个从双向链表中删除,直到队列中只剩下两个人,最后输出答案。

code

#include<bits/stdc++.h>
using namespace std; const int N = 1e5 + 5;
int T, n, cnt;
typedef long long ll;
ll L;
bool dele[N];
struct node{
int id, last, nxt, speed, start;
inline bool operator < (const node &b) const{
return start < b.start;
}
}bdList[N];
struct node2{
int id1, id2;
double t;
friend bool operator < (const node2 &a, const node2 &b){
return a.t > b.t;
}
};
priority_queue<node2> que; inline double calcTime(int u, int v){
if(bdList[u].speed < bdList[v].speed) swap(u, v);
ll dis = (bdList[v].start - bdList[u].start + L) % L, deltaV = (bdList[u].speed - bdList[v].speed);
return 1.0 * dis / deltaV;
} inline void del(int k){
bdList[bdList[k].nxt].last = bdList[k].last;
bdList[bdList[k].last].nxt = bdList[k].nxt;
} inline void init(){
memset(dele, 0, sizeof dele);
while(!que.empty()) que.pop();
memset(bdList, 0, sizeof bdList);
cnt = 0;
} inline ll GCD(ll a, ll b){return b == 0 ? a : GCD(b, a % b);} int debug;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
cin >> T;
while(T--){
cin >> n >> L;
init();
for(int i = 1; i <= n; i++){
bdList[i].id = i;
cin >> bdList[i].start;
}
for(int i = 1; i <= n; i++) cin >> bdList[i].speed;
sort(bdList + 1, bdList + n + 1);
for(int i = 1; i <= n; i++) bdList[i].last = i - 1, bdList[i].nxt = i + 1;
bdList[1].last = n, bdList[n].nxt = 1; for(int i = 1; i <= n; i++){
double t = calcTime(i, bdList[i].nxt);
que.push((node2){i, bdList[i].nxt, t});
}
// while(!que.empty()) cout<<que.top().id1<<" "<<que.top().id2<<" "<<que.top().t<<endl, que.pop();return 0;
while(!que.empty()){
// cout<<++debug<<endl;
while(dele[que.top().id1] || dele[que.top().id2]) que.pop();
int u = que.top().id1, v = que.top().id2;
que.pop(); cnt++;
if(cnt == n - 1){
if(bdList[u].speed < bdList[v].speed) swap(u, v);
ll dis = (bdList[v].start - bdList[u].start + L) % L;
ll deltaV = bdList[u].speed - bdList[v].speed;
ll gcd = GCD(dis, deltaV);
cout << (dis / gcd) << "/" << (deltaV / gcd) << endl;
break;
}
node2 temp;
double tt;
if(bdList[u].id < bdList[v].id)
temp.id1 = bdList[u].last, temp.id2 = v, temp.t = calcTime(bdList[u].last, v), del(u), dele[u] = true;
else temp.id1 = u, temp.id2 = bdList[v].nxt, temp.t = calcTime(u, bdList[v].nxt), del(v), dele[v] = true;
que.push(temp);
}
}
}

NOIP模拟 run - 双向链表的更多相关文章

  1. NOIP模拟赛20161022

    NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...

  2. contesthunter暑假NOIP模拟赛第一场题解

    contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...

  3. NOIP模拟赛 by hzwer

    2015年10月04日NOIP模拟赛 by hzwer    (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...

  4. 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程

    数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...

  5. 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...

  6. 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...

  7. 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...

  8. CH Round #58 - OrzCC杯noip模拟赛day2

    A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...

  9. CH Round #52 - Thinking Bear #1 (NOIP模拟赛)

    A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...

随机推荐

  1. ECMALL功能拓扑图以及模式分析

    ECMALL  VS  常规的B2C产品(以ECSHOP做对比)的区别: 1.支持多用户在同一个域名下开店. 2.开店的卖家各自结算,直接收钱.平台只是提供了一个类似传统行业的摊位.平台不过手金钱 3 ...

  2. PatentTips - Method, apparatus and system for instructing a virtual device from a virtual machine

    BACKGROUND OF THE INVENTION A virtual machine (VM) may be or include a framework or environment crea ...

  3. Transact-SQL语法速查手册

    第1章 Transact-SQL基础 1.1 标识符 一.常规标识符 1. 命名规则: l 第一个字母必须是Unicode2.0标准定义的字母.下划线.at符号(@)和数字符号(#): l 后续字符可 ...

  4. springboot集成shiro 实现权限控制(转)

    shiro apache shiro 是一个轻量级的身份验证与授权框架,与spring security 相比较,简单易用,灵活性高,springboot本身是提供了对security的支持,毕竟是自 ...

  5. MongoDbHelper 帮助类(上)

    在网上搜索mongodbHelper的帮助类时,出来的东西都大同小异,再此摘录一下. 这些代码也看了一遍,总是感觉重复的代码太多了,在后续的文章中又整合了一下,请看下篇,欢迎指正! using Sys ...

  6. express 学习笔记(一)路由

    先导入express: var express = require('express'); var app = express(); 1.路由方法: get, post, put, head, del ...

  7. python3中sum

    摘自https://blog.csdn.net/ikerpeng/article/details/17026011 其实python中sum有两种 一种是python自己的sum 另一种是python ...

  8. [Nuxt] Add Arrays of Data to the Vuex Store and Display Them in Vue.js Templates

    You add array of todos to the store simply by adding them to the state defined in your store/index.j ...

  9. [Angular] Change component default template (ng-content, ng-template, ngTemplateOutlet, TemplateRef)

    Here is the defulat tab header template: <ng-template #defaultTabHeader let-tabs="tabsX" ...

  10. php面试题5

    php面试题5 一.总结 二.php面试题5 1. 什么事面向对象?主要特征是什么?1) 面向对象是程序的一种设计方式,它利于提高程序的重用性,是程序结构更加清晰.2) 主要特征:封装.继承.多态 2 ...