Twelves Monkeys (multiset解法 141 - ZOJ Monthly, July 2015 - H)
Twelves Monkeys
Time Limit: 5 Seconds
Memory Limit: 32768 KB
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.
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
Hint
6 can go back to 1 for two route. One is 6-1, the other is 6-7-8-9-1.6 can go back to 2 for two route. One is 6-1-2, the other is 6-7-8-9-1-2.
题意:n个时刻点。m次时光穿梭,告诉每次穿梭的起点和终点,q次询问,每次询问t时刻t之前有多少时刻点是能够通过两种不同的路径到达的。
思路:对于询问的时刻t能够顺时间向后推移到t+1,t+2。t+3.。。
。。。那么t时刻及以后的时刻的穿梭都是可能的,把他们能穿梭到的时刻插入multiset,假设multiset里有至少两个元素的值大于等于t,则该时刻t存在解。
另外注意的是,询问的时刻点靠前的都能够到达靠后的,所以我们得从后往前求解。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std; #define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 1e5+10; struct Edge
{
int u,v;
}edge[MAXN]; struct Node
{
int d,id;
}node[MAXN]; int cmp1(Edge e1,Edge e2)
{
return e1.u<e2.u;
} int cmp2(Node node1,Node node2)
{
return node1.d<node2.d;
} multiset<int> S; int n,m,q;
int ans[MAXN],a[maxn]; void solve()
{
int i;
S.clear();
sort(edge,edge+m,cmp1); //两个都依照日期从小到大排序
sort(node,node+q,cmp2);
int pos=m-1;
for (i=q-1;i>=0;i--) //从后往前扫,由于前面的时刻能够顺时间到达后面的
{
int day=node[i].d;
int cnt=0;
while (pos>=0&&edge[pos].u>=day)
{
S.insert(edge[pos].v);
pos--;
}
for (multiset<int>::iterator it=S.begin();it!=S.end();it++)
{
a[cnt++]=*(it);
if (cnt>=2) break;
}
if (cnt>=2&&a[1]<=day)
ans[node[i].id]=day-a[1];
else
ans[node[i].id]=0;
}
for (i=0;i<q;i++)
pf("%d\n",ans[i]);
return ;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
int i,j,t;
while (~sfff(n,m,q))
{
for (i=0;i<m;i++)
sff(edge[i].u,edge[i].v);
for (i=0;i<q;i++)
{
sf(node[i].d);
node[i].id=i;
}
solve();
}
return 0;
}
Twelves Monkeys (multiset解法 141 - ZOJ Monthly, July 2015 - H)的更多相关文章
- 思维+multiset ZOJ Monthly, July 2015 - H Twelves Monkeys
题目传送门 /* 题意:n个时刻点,m次时光穿梭,告诉的起点和终点,q次询问,每次询问t时刻t之前有多少时刻点是可以通过两种不同的路径到达 思维:对于当前p时间,从现在到未来穿越到过去的是有效的值,排 ...
- ZOJ Monthly, July 2015
B http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5552 输入n,表示有n个数1到n.A先拿,B后拿,依次拿,每次可以拿任意一 ...
- ZOJ 3913 Bob wants to pour water ZOJ Monthly, October 2015 - H
Bob wants to pour water Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge There i ...
- ZOJ 3910 Market ZOJ Monthly, October 2015 - H
Market Time Limit: 2 Seconds Memory Limit: 65536 KB There's a fruit market in Byteland. The sal ...
- ZOJ 3908 Number Game ZOJ Monthly, October 2015 - F
Number Game Time Limit: 2 Seconds Memory Limit: 65536 KB The bored Bob is playing a number game ...
- ZOJ 3903 Ant ZOJ Monthly, October 2015 - A
Ant Time Limit: 1 Second Memory Limit: 32768 KB There is an ant named Alice. Alice likes going ...
- ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- ZOJ 3905 Cake ZOJ Monthly, October 2015 - C
Cake Time Limit: 4 Seconds Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...
- matrix_2015_1 138 - ZOJ Monthly, January 2015
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3844 第一个,n个数,每次操作最大数和最小数都变成他们的差值,最后n个数相 ...
随机推荐
- JavaScript中的Array数组详解
ECMAScript中的数组与其他多数语言中的数组有着相当大的区别,虽然数组都是数据的有序列表,但是与其他语言不同的是,ECMAScript数组的每一项可以保存任何类型的数据.也就是说,可以用数组的第 ...
- 【图论】Popular Cows
[POJ2186]Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34752 Accepted: ...
- bzoj 2468: [中山市选2010]三核苷酸
2468: [中山市选2010]三核苷酸 Description 三核苷酸是组成DNA序列的基本片段.具体来说,核苷酸一共有4种,分别用’A’,’G’,’C’,’T’来表示.而三核苷酸就是由3个核苷酸 ...
- 20162318 实验一《Java开发环境的熟悉》实验报告
北京电子科技学院(BESTI) 实 验 报 告 课程:程序设计与数据结构 班级:1623班 姓名:张泰毓 成绩:2分 指导老师:娄老师.王老师 实验日期:2017年3月17日 实验密级:非密级 实验器 ...
- Java NIO:Buffer、Channel 和 Selector
Buffer 一个 Buffer 本质上是内存中的一块,我们可以将数据写入这块内存,之后从这块内存获取数据. java.nio 定义了以下几个 Buffer 的实现,这个图读者应该也在不少地方见过了吧 ...
- Problem E: 零起点学算法84——数组中删数II
#include<stdio.h> int main() { ],b[],i,flag=; while(scanf("%d",&n)!=EOF) { ;i< ...
- [转] FileSystemXmlApplicationContext、ClassPathXmlApplicationContext和XmlWebApplicationContext简介
今天在用Spring时遇到一个问题,提示找不到applicationContext.xml文件.原来是在加载这个文件时调用的方法不太合适,所以造成了程序找不到项目下的xml配置文件. 我们常用的加载c ...
- 搭建MySQL的主从、半同步、主主复制架构
复制其最终目的是让一台服务器的数据和另外的服务器的数据保持同步,已达到数据冗余或者服务的负载均衡.一台主服务器可以连接多台从服务器,并且从服务器也可以反过来作为主服务器.主从服务器可以位于不同的网络拓 ...
- 一种计算MD5的实现方法
1.在需要用到加密的地方可以使用.net中的md5相关的类生成md5给文件加密. 2.基本思路: 将文件也好,字符串也好,转成字节数组,再利用.net的md5相关类生成md5相关字符串,再将字符串转成 ...
- android源码包下载
http://rgruet.free.fr/public/ 其他下载地址:http://cid-b50f9d5897331c44.office.live.com/browse.aspx/Android ...