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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-14 00:57:55

Twelves Monkeys (multiset解法 141 - ZOJ Monthly, July 2015 - H)的相关文章

Help Bob (141 - ZOJ Monthly, July 2015 )

题意: 现在有1-n个数,每次从中取出一个数,同时把这个数的所有因子取出.最后一个不能取数的人输 分析: 1是所有数的因子,所有第一次任取一个数1都会被取走:下面分两种情况: 我们先把1拿出来,对于其他的数 1.如果先手必败,那么先手第一次取1,然后把这种必败的状态留给对手,则先手必胜 2.如果先手必胜,则按照必胜的策越即可(1作为附带的被取出) #include <cstdio> int main() { int n; while(scanf("%d",&n)==

The Exchange of Items (141 - ZOJ Monthly, July 2015 - E 最小费用最大流)

The Exchange of Items Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob lives in an ancient village, where transactions are done by one item exchange with another. Bob is very clever and he knows what items will become more valuable later on. So,

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 salesmen there only sell apples. There are n salesmen in the fruit market and the i-th salesman will sell at most wi apples. Every salesman has an immedi

ZOJ Monthly, July 2012

zoj 3622.Magic Number   水题 先找规律生成区间[1,1<<32-1]内的所有Magic Number,计算出来只有40多个,然后就随便YY了. void init() { int a[5] = { 1,2,5,25,125 }; ll Max = (1ll<<32)-1; for(ll tmp =1; tmp<=Max; tmp *=10) { for(int i=0; i<5; ++i){ ll t = tmp * a[i]; if(t>

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. In the beginning, there are n numbers. For each turn, Bob will take out two numbers from the remaining numbers, and get the product of them. There i

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 sequence A[i] with N numbers. You have to perform Q operations on the given sequence. Here are the operations: A v l, add the value v to element with i

ZOJ 3905 Ant ZOJ Monthly, October 2015 - C

Cake Time Limit: 4 Seconds      Memory Limit: 65536 KB Alice and Bob like eating cake very much. One day, Alice and Bob went to a bakery and bought many cakes. Now we know that they have bought n cakes in the bakery. Both of them like delicious cakes

matrix_2015_1 138 - ZOJ Monthly, January 2015

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3844 第一个,n个数,每次操作最大数和最小数都变成他们的差值,最后n个数相同时输出此时的值,暴力跑. 1 #include<cstdio> 2 int main(){ 3 int t,n,a[16]; 4 while(~scanf("%d",&t)){ 5 while(t--){ 6 scanf("%d",&n);

ZOJ Monthly, January 2015 (B、E、G、H)

B题: 先处理出已有卡牌,然后进行dfs,dfs有个很大的剪枝,就是当前位置如果字典序小于了,那么后面就不用继续放了,直接用组合数学进行计算即可,如果大于就不用考虑了,如果等于才继续往后搜,这样的话,搜等于只要在字典序相等的一条路上搜,时间可以接受 E题:模拟即可,不存在无解情况 G题:先全部数字GCD一遍,如果不为1,就是无解,如果为1,那么构造答案,其实只要拿第一个数字,可以其他每个数字做一次gcd,第一个数字就是1了,然后再拿第一个数字和后面数字做gcd,就全部都是1了,一共进行n - 2