NOIP模拟 run - 双向链表
题目大意:
企鹅国正在举办全面运动会,第一项比赛就是跑步。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 - 双向链表的更多相关文章
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
- NOIP模拟赛 by hzwer
2015年10月04日NOIP模拟赛 by hzwer (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
- CH Round #52 - Thinking Bear #1 (NOIP模拟赛)
A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...
随机推荐
- 第二遍回顾--①前端flex布局
1.flex: 弯曲,收缩 2.概念 2条主轴,main axis,cross axis; 每个单元为flex item,主轴空间main size,交叉轴空间cross size; 3.容器 .co ...
- “焦点图/幻灯片”“Tab标签切换”“图片滚动”“无缝滚动”仅需一个SuperSlidev2.1
官网:http://www.superslide2.com/index.html 1. 标签切换 / 书签切换 / 默认效果 2. 焦点图 / 幻灯片 3. 图片滚动-左 4. 图片滚动-上 5. 图 ...
- How to Rotate Tomcat catalina.out
If catalina.out becomes 2GB in size, tomcat crashes and fails to start without any error message. To ...
- PHP从数组中删除元素的四种方法实例
PHP从数组中删除元素的四种方法实例 一.总结 一句话总结:unset(),array_splice(),array_diff(),array_diff_key() 二.PHP从数组中删除元素的四种方 ...
- Javascript和jquery事件--双击事件
在js中和jq中对应的命名都为dblclick,ondblclick,但是ondblclick和dom元素的属性相似,可以在行内设置,也可以使用attr设置. 同时,双击事件需要关注一个问题,那就是双 ...
- GCJ 2008 Round 1A Minimum Scalar Product
https://code.google.com/codejam/contest/32016/dashboard 题目大意: GCJ(google code jam)上的水题.下周二有比赛,来熟悉熟悉. ...
- common daemon
http://zdsyouxiang.iteye.com/blog/1940202 http://commons.apache.org/proper/commons-daemon/procrun.ht ...
- UVA 11280 - Flying to Fredericton SPFA变形
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...
- 【Codeforces Round #437 (Div. 2) A】Between the Offices
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 在这里写题解 [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h> using n ...
- java回调函数这样说,应该明确了吧!
有哥们问我回调怎么用,回调怎么理解? 怎么说好呢,仅仅可意会不可言传呐,非也,回调在实际开发中使用频率事实上是非常高的,恰好我小时候也被回调函数欺负过,居然问了,那么肯定要好好分享一下我的一些经验. ...