//目录

Educational Codeforces Round 27

 

A. Chess Tourney

Berland annual chess tournament is coming!

Organizers have gathered 2·n chess players who should be divided into two teams with n people each. The first team is sponsored by BerOil and the second team is sponsored by BerMobile. Obviously, organizers should guarantee the win for the team of BerOil.

Thus, organizers should divide all 2·n players into two teams with n people each in such a way that the first team always wins.

Every chess player has its rating r**i. It is known that chess player with the greater rating always wins the player with the lower rating. If their ratings are equal then any of the players can win.

After teams assignment there will come a drawing to form n pairs of opponents: in each pair there is a player from the first team and a player from the second team. Every chess player should be in exactly one pair. Every pair plays once. The drawing is totally random.

Is it possible to divide all 2·n players into two teams with n people each so that the player from the first team in every pair wins regardlessof the results of the drawing?

Input

The first line contains one integer n (1 ≤ n ≤ 100).

The second line contains 2·n integers a1, a2, ... a2n (1 ≤ a**i ≤ 1000).

Output

If it's possible to divide all 2·n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing, then print "YES". Otherwise print "NO".

Examples

input

 
 
 
 
 
2
1 3 2 4
 

output

 
 
 
 
 
YES
 

input

 
 
 
 
 
1
3 3
 

output

 
 
 
 
 
NO
 

 

题意:一个团体是否可以分成两个部分,使得从两个部分的人,任意两对,都是一个队赢;

分析:当然第一个队,全都是厉害的人。分好后挑人比赛,第二个队的人,只要有一个人能赢第一个队中,最弱的。就是NO。

 
 
 
 
 
#include <bits/stdc++.h>
using namespace std;
int a[205];
int main(int argc, char const *argv[]) {
    int n;
    scanf("%d",&n);
    for(int i=0; i < 2*n; i++)
        scanf("%d",&a[i]);
    sort(a,a+2*n);
    bool flag = true;
    for(int i=0; i < n; i++)
        if(a[i]>=a[n]) {
            flag=false;
            break;
        }
    if(flag)
        puts("YES");
    else puts("NO");
    return 0;
}
 

 

 

B. Luba And The Ticket

Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky.

The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits.

Input

You are given a string consisting of 6 characters (all characters are digits from 0 to 9) — this string denotes Luba's ticket. The ticket can start with the digit 0.

Output

Print one number — the minimum possible number of digits Luba needs to replace to make the ticket lucky.

Examples

input

 
 
 
 
 
000000
 

output

 
 
 
 
 
0
 

input

 
 
 
 
 
123456
 

output

 
 
 
 
 
2
 

input

 
 
 
 
 
111000
 

output

 
 
 
 
 
1
 

Note

In the first example the ticket is already lucky, so the answer is 0.

In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required.

In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.

 

题意:让前三个数字之和,与后三个数字之和相等,最少变化的位数。

分析:最后变化的差值,就是目标结果,然后,前后都可以变,分情况,到底是变小还是变大。根据变化的能力排序。

 
 
 
 
 
#include <bits/stdc++.h>
using namespace std;
bool cmp(int a,int b) {
    return a > b;
}
int main(int argc, char const *argv[]) {
    char str[10];
    scanf("%s",str);
    int a = 0;
    for(int i=0; i < 3; i++)
        a = a+ str[i] - '0';
    int b = 0;
    for(int i=3; i < 6; i++)
        b = b + str[i] - '0';
    if(a==b)
        puts("0");
    else if(a<b) {
        int ans = b - a;
        int num[10];
        for(int i=0;i < 3;i++)
            num[i] = 9 - (str[i] - '0');
        for(int i=3;i < 6;i++)
            num[i] = (str[i]-'0');
        sort(num,num+6,cmp);
        for(int i=1; i<6;i++)
            num[i] = num[i-1] +num[i];
        for(int i=0;i<6;i++)
            if(num[i]>=ans) {
                printf("%d\n",i+1);
                break;
            }
    }
    else {
        int ans = a - b;
        int num[10];
        for(int i=3;i < 6;i++)
            num[i] = 9 - (str[i] - '0');
        for(int i=0;i < 3;i++)
            num[i] = str[i] - '0';
        sort(num,num+6,cmp);
        for(int i=1; i<6;i++)
            num[i] = num[i-1] +num[i];
        for(int i=0;i<6;i++)
            if(num[i]>=ans) {
                printf("%d\n",i+1);
                break;
            }
    }
    return 0;
}
 

 

C. Two TVs

Polycarp is a great fan of television.

He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l**i and ends at moment r**i.

Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.

Polycarp wants to check out all n shows. Are two TVs enough to do so?

Input

The first line contains one integer n (1 ≤ n ≤ 2·105) — the number of shows.

Each of the next n lines contains two integers l**i and r**i (0 ≤ l**i < r**i ≤ 109) — starting and ending time of i-th show.

Output

If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).

Examples

input

 
 
 
 
 
3
1 2
2 3
4 5
 

output

 
 
 
 
 
YES
 

input

 
 
 
 
 
4
1 2
2 3
2 3
1 2
 

output

 
 
 
 
 
NO
 

 

题意:两台电视机,很多节目时间段,是否可以全部都看。

分析:刚开始,贪心,两个指针每台电视机的最短时间。结果被hack掉了。

然后,将所有节目,分成两个部分,一个是开始时间。一个是结束时间,排序好后,队列中的电视机不能同时上映>2的节目。

 
 
 
 
 
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<pair<int,int> > e;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        e.push_back(make_pair(l,1));
        e.push_back(make_pair(r+1,-1));
    }
    sort(e.begin(),e.end());
    int cnt=0;
    for(int i=0;i<(int)e.size();i++)
    {
        cnt+=e[i].second;
        if(cnt>2)return 0*printf("NO\n");
    }
    return 0*printf("YES\n");
}
 

 

G. Shortest Path Problem?

You are given an undirected graph with weighted edges. The length of some path between two vertices is the bitwise xor of weights of all edges belonging to this path (if some edge is traversed more than once, then it is included in bitwise xor the same number of times). You have to find the minimum length of path between vertex 1 and vertex n.

Note that graph can contain multiple edges and loops. It is guaranteed that the graph is connected.

Input

The first line contains two numbers n and m (1 ≤ n ≤ 100000, n - 1 ≤ m ≤ 100000) — the number of vertices and the number of edges, respectively.

Then m lines follow, each line containing three integer numbers xy and w (1 ≤ x, y ≤ n, 0 ≤ w ≤ 108). These numbers denote an edge that connects vertices x and y and has weight w.

Output

Print one number — the minimum length of path between vertices 1 and n.

Examples

input

 
 
 
 
 
3 3
1 2 3
1 3 2
3 2 0
 

output

 
 
 
 
 
2
 

input

 
 
 
 
 
2 2
1 1 3
1 2 3
 

output

 
 
 
 
 
0
 

 

题意:XOR最短路。

分析:论文题,两次异或,有环的时候,

然后最后答案是 ,这样t[v] 的影响就消掉了,然后,就是一个贪心,对每个环,都去贪心最小。

 
 
 
 
 
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N = 100005;
std::vector<pair<int,ll> > e[MAX_N];
ll t[MAX_N];
bool vis[MAX_N];
std::vector<ll> base;
void add(ll x) {
    for(int i=0;i<(int)base.size();i++)
        x=min(x,x^base[i]);
    if(x)base.push_back(x);
}
void dfs(int u,ll now) {
    t[u] = now;
    vis[u] = 1;
    for(int i=0; i < (int)e[u].size(); i++) {
        int v = e[u][i].first;
        if(vis[v]) {
            add(now^e[u][i].second^t[v]);
        }
        else dfs(v,now^e[u][i].second); 
    }
}
int main(int argc, char const *argv[]) {

    int n,m;
    scanf("%d%d",&n,&m);
    int u,v,w;
    for(int i=0; i < m; i++) {
        scanf("%d%d%d",&u,&v,&w);
        e[u].push_back(make_pair(v,w));
        e[v].push_back(make_pair(u,w));
    }
    dfs(1,0);
    for(int i=0; i < (int)base.size();i++)
        t[n] = min(t[n],t[n]^base[i]);
    printf("%I64d\n", t[n]);
    return 0;
}
 

 

posted @ 2017-08-22 21:05  小草的大树梦  阅读(287)  评论(0编辑  收藏  举报