题目大意:

企鹅国正在举办全面运动会,第一项比赛就是跑步。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. win7桌面有个无法删除的IE图标

    平台:win7 症状:安装软件时没仔细看,结果装上了一大堆,挨个卸载后桌面残留了一个IE无法删除.在该图标上点右键只有“打开”“属性”“创建快捷方式”三个选项,主页默认为www.2345.com. 解 ...

  2. Makefile的问题

    注意包含路径的变量,可能会带有空格而使得运行失败 直接定义:DEBUG  = false后面引用需要使用 $(DEBUG) 或者set  DEBUG  $(DEBUG)之后就可以直接引用了

  3. 浅析C#组件编程中的一些小细节

    控件与组件的区别(Control&Component的区别) 作者:作者不详  发布日期:2011-06-30 12:08:41 控件与组件的区别(Control&Component的 ...

  4. Java对ad操作

    转载:http://blog.csdn.net/binyao02123202/article/details/18697953

  5. [Angular] Protect The Session Id with https and http only

    For the whole signup process. we need to Hash the password to create a password digest Store the use ...

  6. [Angular] HttpParams

    It is possible to use HttpParams to set http params. For example we have this url to make: https://a ...

  7. amazeui学习笔记--css(常用组件5)--评论列表Comment

    amazeui学习笔记--css(常用组件5)--评论列表Comment 一.总结 1.am-comment:使用am-comment来声明评论对象,这个是放在article里面的,虽然article ...

  8. 主定理(Master Theorem)与时间复杂度

    1. 问题 Karatsuba 大整数的快速乘积算法的运行时间(时间复杂度的递推关系式)为 T(n)=O(n)+4⋅T(n/2),求其最终的时间复杂度. 2. 主定理的内容 3. 分析 所以根据主定理 ...

  9. 关于JS面向对象继承问题

    1.原型继承(是JS中很常用的一种继承方式) 子类children想要继承父类father中的所有的属性和方法(私有+公有),只需要让children.prototype=new father;即可. ...

  10. 【Codeforces Round #445 (Div. 2) D】Restoration of string

    [链接] 我是链接,点我呀:) [题意] 给你n个字符串. 让你构造一个字符串s. 使得这n个字符串. 每个字符串都是s的子串. 且都是出现次数最多的子串. 要求s的长度最短,且s的字典序最小. [题 ...