Codeforces Round #357 (Div. 2) (A-D)(水题 + 暴力 + 堆模拟 + DFS)

这是寒假周训的题,因为看到CF上没有做完所以去补发现周训已经做出来四题了,于是就顺手写一下题解吧。

BTW,周训的时候一天一场CF确实提升很大。

A. A Good Contest

Codeforces user' handle color depends on his rating — it is red if his rating is greater or equal to 2400; it is orange if his rating is less than 2400 but greater or equal to 2200, etc. Each time participant takes part in a rated contest, his rating is changed depending on his performance.

Anton wants the color of his handle to become red. He considers his performance in the rated contest to be good if he outscored some participant, whose handle was colored red before the contest and his rating has increased after it.

Anton has written a program that analyses contest results and determines whether he performed good or not. Are you able to do the same?

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the number of participants Anton has outscored in this contest .

The next n lines describe participants results: the i-th of them consists of a participant handle namei and two integers beforei and afteri( - 4000 ≤ beforei, afteri ≤ 4000) — participant's rating before and after the contest, respectively. Each handle is a non-empty string, consisting of no more than 10 characters, which might be lowercase and uppercase English letters, digits, characters «_» and «-» characters.

It is guaranteed that all handles are distinct.

Output

Print «YES» (quotes for clarity), if Anton has performed good in the contest and «NO» (quotes for clarity) otherwise.

Examples
input
Copy
3
Burunduk1 2526 2537
BudAlNik 2084 2214
subscriber 2833 2749
output
YES
input
Copy
3
Applejack 2400 2400
Fluttershy 2390 2431
Pinkie_Pie -2500 -2450
output
NO
Note

In the first sample, Anton has outscored user with handle Burunduk1, whose handle was colored red before the contest and his rating has increased after the contest.

In the second sample, Applejack's rating has not increased after the contest, while both Fluttershy's and Pinkie_Pie's handles were not colored red before the contest.


A题:照例水题,背景是CF的算分方式。需要判断有没有红名大佬在打完这场之后分数上升了。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
    int num,be,af;
    char a[20];
    int can = 0;
    cin>>num;
    while(num--)
    {
        cin>>a>>be>>af;
        if(be>=2400 && af>be) can=1;
    }

    if(can==1) puts("YES");
    else puts("NO");
}

B. Economy Game

Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.

Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).

Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers ab and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?

Please help Kolya answer this question.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.

Output

Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).

Examples
input
Copy
1359257
output
YES
input
Copy
17851817
output
NO
Note

In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.


B题:问n个硬币买房,车,电脑,能不能刚好买完。两重for循环遍历所有可能解,直接用除余判断第三层能不能过。和原来的100 coins差不多。贪心的思路先买贵的,这样数量可以是很多的电脑直接用除余判断,省去一重循环。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int hou = 1234567;
const int car = 123456;
const int com = 1234;


int main()
{
    int num,can = 0;
    cin>>num;
    for(int i=num/hou;i>=0;i--)
    {
        for(int j=(num-i*hou)/car;j>=0;j--)
        {
            if((num-i*hou-j*car)%com==0) {can=1;break;}
        }
        if(can==1) break;
    }

    if(can) puts("YES");
    else puts("NO");
}
C. Heap Operations

Petya has recently learned data structure named "Binary heap".

The heap he is now operating with allows the following operations:

  • put the given number into the heap;
  • get the value of the minimum element in the heap;
  • extract the minimum element from the heap;

Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.

In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:

  • insert x — put the element with value x in the heap;
  • getMin x — the value of the minimum element contained in the heap was equal to x;
  • removeMin — the minimum element was extracted from the heap (only one instance, if there were many).

All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.

While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.

Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.

Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.

Input

The first line of the input contains the only integer n (1 ≤ n ≤ 100 000) — the number of the records left in Petya's journal.

Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.

Output

The first line of the output should contain a single integer m — the minimum possible number of records in the modified sequence of operations.

Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.

Note that the input sequence of operations must be the subsequence of the output sequence.

It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.

Examples
input
Copy
2
insert 3
getMin 4
output
4
insert 3
removeMin
insert 4
getMin 4
input
Copy
4
insert 1
insert 1
removeMin
getMin 2
output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note

In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4one should firstly remove number 3 from the heap and then add number 4 into the heap.

In the second sample case number 1 is inserted two times, so should be similarly removed twice.


C题:模拟堆操作。给出n个操作,n个操作是不完整有缺失的。要求按照其中的逻辑关系补全缺失操作。一道模拟题,用优先队列模拟堆,然后对读入的操作op1顺序执行,如果合法直接存入op2,如果不合法判断后加上应有操作存入op2。

insert:合法操作直接存入

removeMin:堆为空时,需要先加入一个任意数,然后再移除

getMin:堆顶数比所需数小时,需要弹出。堆为空时,需要就加入get的数。堆顶数比所需数大时,需要压入。

D. Gifts by the List

Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are nmen in Sasha's family, so let's number them with integers from 1 to n.

Each man has at most one father but may have arbitrary number of sons.

Man number A is considered to be the ancestor of the man number B if at least one of the following conditions is satisfied:

  • A = B;
  • the man number A is the father of the man number B;
  • there is a man number C, such that the man number A is his ancestor and the man number C is the father of the man number B.

Of course, if the man number A is an ancestor of the man number B and A ≠ B, then the man number B is not an ancestor of the man number A.

The tradition of the Sasha's family is to give gifts at the Man's Day. Because giving gifts in a normal way is boring, each year the following happens.

  1. A list of candidates is prepared, containing some (possibly all) of the n men in some order.
  2. Each of the n men decides to give a gift.
  3. In order to choose a person to give a gift to, man A looks through the list and picks the first man B in the list, such that B is an ancestor of A and gives him a gift. Note that according to definition it may happen that a person gives a gift to himself.
  4. If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone.

This year you have decided to help in organizing celebration and asked each of the n men, who do they want to give presents to (this person is chosen only among ancestors). Are you able to make a list of candidates, such that all the wishes will be satisfied if they give gifts according to the process described above?

Input

In the first line of the input two integers n and m (0 ≤ m < n ≤ 100 000) are given — the number of the men in the Sasha's family and the number of family relations in it respectively.

The next m lines describe family relations: the (i + 1)th line consists of pair of integers pi and qi (1 ≤ pi, qi ≤ npi ≠ qi) meaning that the man numbered pi is the father of the man numbered qi. It is guaranteed that every pair of numbers appears at most once, that among every pair of two different men at least one of them is not an ancestor of another and that every man has at most one father.

The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n), ith of which means that the man numbered i wants to give a gift to the man numbered ai. It is guaranteed that for every 1 ≤ i ≤ n the man numbered ai is an ancestor of the man numbered i.

Output

Print an integer k (1 ≤ k ≤ n) — the number of the men in the list of candidates, in the first line.

Print then k pairwise different positive integers not exceeding n — the numbers of the men in the list in an order satisfying every of the men's wishes, one per line.

If there are more than one appropriate lists, print any of them. If there is no appropriate list print  - 1 in the only line.

Examples
input
Copy
3 2
1 2
2 3
1 2 1
output
-1
input
Copy
4 2
1 2
3 4
1 2 3 3
output
3
2
1
3
Note

The first sample explanation:

  • if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (a1 = a3 = 1);
  • if there would be no 2 in the list then the second man wish would not be satisfied (a2 = 2);
  • if 1 would stay before 2 in the answer then the second man would have to give his gift to the first man, but he wants to give it to himself (a2 = 2).
  • if, at the other hand, the man numbered 2 would stay before the man numbered 1, then the third man would have to give his gift to the second man, but not to the first (a3 = 1).


D题:题中人与人的关系可以抽象为一棵树。每个人都想把自己的礼物给自己的某个祖先,需要你给出一个顺序表,每个人把礼物给表中出现的自己的第一个祖先(自己也算自己的祖先)。问存不存在一个顺序表,使每个人都给到了自己想要给的祖先。

这道题一开始想歪了,后来发现一个很有意思的结论:继承关系A->B->C->D,如果D想要把礼物给A的话,在顺序表中,A一定在B,C前面。那么BC也会把礼物给A。那么,如果C把礼物给A了,B也一定给了A,而D可以给A或者给D。因为D是自己的祖先而不是A的祖先。那么我们可以得到:对于任意一个节点,他给的礼物必须和他的前驱节点一样,或者给他自己。DFS一遍,END

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;

const int maxn = 1e6+5;

vector<int > v[maxn];
int lis[maxn];
int cant = 0,tol = 0;

struct Node
{
    int want;
    int rd;

}node[maxn];


void dfs(int s,int to)
{
    if(node[s].want == to) ;
    else if (node[s].want == s) {to = s; lis[tol++] = to;}
    else {cant = 1; return ;}

    for(int i=0;i<v[s].size();i++)
    {
        dfs(v[s][i],to);
    }
}


int main()
{
    int m,n,fa,son;
    cin>>n>>m;
    for(int i=0;i<n;i++) node[i].rd = 0;

    while(m--)
    {
        scanf("%d%d",&fa,&son);
        v[fa].push_back(son);
        node[son].rd++;
    }
    for(int i=1;i<=n;i++)
        scanf("%d",&node[i].want);

    for(int i=1;i<=n;i++)
        if(node[i].rd == 0)
            dfs(i,-1);
    if(cant == 1) printf("-1\n");
    else {
        printf("%d\n",tol);
        for(int i=tol-1;i>=0;i--) printf("%d\n",lis[i]);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值