ZOJ 3888 Twelves Monkeys
2015-07-27 23:54
274 查看
James Cole is a convicted criminal living beneath a post-apocalyptic Philadelphia. Many years ago, the Earth's surface had been contaminated by a virus so deadly that it forced
the survivors to move underground. In the years that followed, scientists had engineered an imprecise form of time travel. To earn a pardon, Cole allows scientists to send him on dangerous missions to the past to collect information on the virus,
thought to have been released by a terrorist organization known as the Army of the Twelve Monkeys.
The time travel is powerful so that sicentists can send Cole from year x[i] back to year y[i]. Eventually, Cole finds that Goines is the founder
of the Army of the Twelve Monkeys, and set out in search of him. When they find and confront him, however, Goines denies any involvement with the viruscan. After that, Cole goes back and tells scientists what he knew. He wants to quit the mission
to enjoy life. He wants to go back to the any year before current year, but scientists only allow him to use time travel once. In case of failure,Cole will find at least one route for backup. Please help him to calculate how many years he can go with
at least two routes.
The first line contains three integers n,m,q(1≤ n ≤ 50000, 1≤ m ≤ 50000, 1≤ q ≤ 50000), indicating the maximum year,
the number of time travel path and the number of queries.
The following m lines contains two integers x,y(1≤ y ≤ x ≤ 50000) indicating Cole can travel from year x to
year y.
The following q lines contains one integers p(1≤ p ≤ n) indicating the year Cole is at now
the survivors to move underground. In the years that followed, scientists had engineered an imprecise form of time travel. To earn a pardon, Cole allows scientists to send him on dangerous missions to the past to collect information on the virus,
thought to have been released by a terrorist organization known as the Army of the Twelve Monkeys.
The time travel is powerful so that sicentists can send Cole from year x[i] back to year y[i]. Eventually, Cole finds that Goines is the founder
of the Army of the Twelve Monkeys, and set out in search of him. When they find and confront him, however, Goines denies any involvement with the viruscan. After that, Cole goes back and tells scientists what he knew. He wants to quit the mission
to enjoy life. He wants to go back to the any year before current year, but scientists only allow him to use time travel once. In case of failure,Cole will find at least one route for backup. Please help him to calculate how many years he can go with
at least two routes.
Input
The input file contains multiple test cases.The first line contains three integers n,m,q(1≤ n ≤ 50000, 1≤ m ≤ 50000, 1≤ q ≤ 50000), indicating the maximum year,
the number of time travel path and the number of queries.
The following m lines contains two integers x,y(1≤ y ≤ x ≤ 50000) indicating Cole can travel from year x to
year y.
The following q lines contains one integers p(1≤ p ≤ n) indicating the year Cole is at now
Output
For each test case, you should output one line, contain a number which is the total number of the year Cole can go.Sample Input
9 3 3 9 1 6 1 4 1 6 7 2
Sample Output
5 0 1 事实上就是求从x到n的次小值,直接倒推预处理出全部位置答案即可#include<cstdio> #include<cmath> #include<queue> #include<vector> #include<stack> #include<map> #include<string> #include<cstring> #include<algorithm> #include<iostream> using namespace std; typedef long long ll; const ll maxn = 100005; vector<int> t[maxn]; int f[maxn][2]; int n, m, q, x, y; int main() { while (scanf("%d%d%d", &n, &m, &q) == 3) { for (int i = 1; i <= n; ++i) t[i].clear(); while (m--) { scanf("%d%d", &x, &y); t[x].push_back(y); } f[n + 1][0] = f[n + 1][1] = n + 1; for (int i = n; i; --i) { f[i][0] = min(i, f[i + 1][0]); f[i][1] = min(i, f[i + 1][1]); for (int j = 0; j < t[i].size(); ++j) { if (f[i][0] >= t[i][j]) { f[i][1] = f[i][0]; f[i][0] = t[i][j]; } else f[i][1] = min(f[i][1], t[i][j]); } } while (q--) { scanf("%d", &x); printf("%d\n", x - f[x][1]); } } return 0; }
相关文章推荐
- KMP算法模板
- @Autowired与@Resource的区别
- 一个菜鸟写的入门级for循环函数 请多多指教
- 1、我们第一个(极小的)机器学习应用【准备数据】
- Dom元素中的元素应用
- JS应用,表单上的一些东西
- 初识html
- Fragment的生命周期(4)
- symfony路由组件(The Routing Component)
- Linux系统管理之十二---内核编译升级
- poj_1877
- Hibernate One2Many映射关系
- xss和csrf攻击
- mongodb(四)-文档
- 如何编写Xcode 插件
- ZOJ 3882 Help Bob
- UVa 311 - Packets
- android面试题总结02 在android中本地写入一个xml文件
- 添加朋友, 数据库设计 与 实现
- Neutron - flat模式