题目大意:

企鹅国正在举办全面运动会,第一项比赛就是跑步。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. 洛谷 P1205 [USACO1.2]方块转换 Transformations

    P1205 [USACO1.2]方块转换 Transformations 题目描述 一块N x N(1<=N<=10)正方形的黑白瓦片的图案要被转换成新的正方形图案.写一个程序来找出将原始 ...

  2. [D3] Build an Area Chart with D3 v4

    Similar to line charts, area charts are great for displaying temporal data. Whether you’re displayin ...

  3. solrcloud集群搭建

    solrcloud 集群搭建 初始条件: 1. 三台服务器 IP 地址分别为 192.168.1.133 192.168.1.134 192.168.1.135 2. 使用 solr-5.3.1,zo ...

  4. Socket 长连接与短连接,心跳

    长连接与短连接 所谓长连接,指在一个TCP连接上可以连续发送多个数据包,在TCP连接保持期间,如果没有数据包发送,需要双方发检测包以维持此连接,一般需要自己做在线维持. 短连接是指通信双方有数据交互时 ...

  5. python获取序列中最大值

    test =[ [1, 2, 3], [4, 5, 6], [7, 8, 9]]   #这个就可以看做是二维数组了,直接创建print(test)print(test[:][1])           ...

  6. ejs模板引擎的使用

    引入ejs.min.js 创建模板,以<%=jsCode%>包裹起来其余的html和html结构一样 focusTemplateData是模板使用的数据,使用$.each()方法遍历绑定数 ...

  7. 4、python基本知识点及字符串常用方法

    查看变量内存地址   id(变量名) ni = 123 n2 = 123 ni和n2肯定是用的两份内存,但是python对于数字在-5~257之间的数字共用一份地址,范围可以修改 name = ‘李璐 ...

  8. html练习(3)

    1.这个小练习用到了css的四种选择器id选择器,类选择器,html选择器,通配符选择器. (1)假设一个元素中用到了各种选择器,而且选择器中的属性发生了冲突,则 优先级为id选择器>类选择器& ...

  9. ping 本地端口

    C:\Users\Administrator>netstat -ano | findstr 8001

  10. .dmp文件导出使用示例

    exp导出的几种用例,先睹为快: 1 将数据库SampleDB完全导出,用户名system 密码manager 导出到E:/SampleDB.dmp中 exp system/manager@TestD ...