CodeForces Round #550 Div.3

http://codeforces.com/contest/1144

A. Diverse Strings

A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.

Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).

You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".

Input

The first line contains integer nn (1n1001≤n≤100), denoting the number of strings to process. The following nn lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between 11 and 100100, inclusive.

Output

Print nn lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.

Example
input
Copy
8
fced
xyz
r
dabcef
az
aa
bad
babc
output
Copy
Yes
Yes
Yes
Yes
No
No
No
No

代码:

#include <bits/stdc++.h>
using namespace std;

int N;
map<char, int> mp;

int main() {
    scanf("%d", &N);
    getchar();
    for(int i = 0; i < N; i ++) {
        mp.clear();
        string s;
        cin >> s;
        bool flag = true;
        int len = s.length();
        int minn = 89;
        for(int j = 0; j < len; j ++) {
            mp[s[j]] ++;
            minn = min(minn, s[j] - 'a');
            if(mp[s[j]] > 1) flag = false;
        }
        //printf("!!!%d\n", minn);
        for(int j = minn; j < minn + len; j ++) {
            if(mp[j + 'a'] == 0) {
                flag = false;
                break;
            }
        }
        if(flag) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
View Code

B. Parity Alternated Deletions

Polycarp has an array aa consisting of nn integers.

He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains n1n−1 elements). For each of the next moves he chooses any element with the only restriction: its parity should differ from the parity of the element deleted on the previous move. In other words, he alternates parities (even-odd-even-odd-... or odd-even-odd-even-...) of the removed elements. Polycarp stops if he can't make a move.

Formally:

  • If it is the first move, he chooses any element and deletes it;
  • If it is the second or any next move:
    • if the last deleted element was odd, Polycarp chooses any even element and deletes it;
    • if the last deleted element was even, Polycarp chooses any odd element and deletes it.
  • If after some move Polycarp cannot make a move, the game ends.

Polycarp's goal is to minimize the sum of non-deleted elements of the array after end of the game. If Polycarp can delete the whole array, then the sum of non-deleted elements is zero.

Help Polycarp find this value.

Input

The first line of the input contains one integer nn (1n20001≤n≤2000) — the number of elements of aa.

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (0ai1060≤ai≤106), where aiai is the ii-th element of aa.

Output

Print one integer — the minimum possible sum of non-deleted elements of the array after end of the game.

Examples
input
Copy
5
1 5 7 8 2
output
Copy
0
input
Copy
6
5 1 2 4 6 3
output
Copy
0
input
Copy
2
1000000 1000000
output
Copy
1000000

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;
int N;
int num[maxn];
vector<int> odd, even;

int main() {
    scanf("%d", &N);
    for(int i = 0; i < N; i ++) {
        scanf("%d", &num[i]);
        if(num[i] % 2) odd.push_back(num[i]);
        else even.push_back(num[i]);
    }
    int ans = 0;
    sort(odd.rbegin(), odd.rend());
    sort(even.rbegin(), even.rend());
    int osz = odd.size(), esz = even.size();
    if(osz > esz + 1) {
        for(int i = esz + 1; i < osz; i ++)
            ans += odd[i];
    }
    else if(osz == esz + 1) ans = 0;
    else {
        for(int i = osz + 1; i < esz; i ++)
            ans += even[i];
    }
    printf("%d\n", ans);
    return 0;
}
View Code

C.Two Shuffled Sequences

output
standard output

Two integer sequences existed initially — one of them was strictly increasing, and the other one — strictly decreasing.

Strictly increasing sequence is a sequence of integers [x1<x2<<xk][x1<x2<⋯<xk]. And strictly decreasing sequence is a sequence of integers [y1>y2>>yl][y1>y2>⋯>yl]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.

They were merged into one sequence aa. After that sequence aa got shuffled. For example, some of the possible resulting sequences aa for an increasing sequence [1,3,4][1,3,4] and a decreasing sequence [10,4,2][10,4,2] are sequences [1,2,3,4,4,10][1,2,3,4,4,10] or [4,2,1,10,4,3][4,2,1,10,4,3].

This shuffled sequence aa is given in the input.

Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one — strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.

If there is a contradiction in the input and it is impossible to split the given sequence aa to increasing and decreasing sequences, print "NO".

Input

The first line of the input contains one integer nn (1n21051≤n≤2⋅105) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (0ai21050≤ai≤2⋅105), where aiai is the ii-th element of aa.

Output

If there is a contradiction in the input and it is impossible to split the given sequence aa to increasing and decreasing sequences, print "NO" in the first line.

Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.

In the second line print nini — the number of elements in the strictly increasing sequence. nini can be zero, in this case the increasing sequence is empty.

In the third line print nini integers inc1,inc2,,incniinc1,inc2,…,incni in the increasing order of its values (inc1<inc2<<incniinc1<inc2<⋯<incni) — the strictly increasing sequence itself. You can keep this line empty if ni=0ni=0 (or just print the empty line).

In the fourth line print ndnd — the number of elements in the strictly decreasing sequence. ndnd can be zero, in this case the decreasing sequence is empty.

In the fifth line print ndnd integers dec1,dec2,,decnddec1,dec2,…,decnd in the decreasing order of its values (dec1>dec2>>decnddec1>dec2>⋯>decnd) — the strictly decreasing sequence itself. You can keep this line empty if nd=0nd=0 (or just print the empty line).

ni+ndni+nd should be equal to nn and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).

Examples
input
Copy
7
7 2 7 3 3 1 4
output
Copy
YES
2
3 7 
5
7 4 3 2 1 
input
Copy
5
4 3 1 5 3
output
Copy
YES
1
3 
4
5 4 3 1 
input
Copy
5
1 1 2 1 2
output
Copy
NO
input
Copy
5
0 1 2 3 4
output
Copy
YES
0

5
4 3 2 1 0 

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10;
int N;
int a[maxn];
int vis[maxn];

int main() {
    scanf("%d", &N);
    bool flag = true;
    for(int i = 0; i < N; i ++) {
        scanf("%d", &a[i]);
        vis[a[i]] ++;
        if(vis[a[i]] > 2) flag = false;
    }
    if(!flag) {
        printf("NO\n");
        return 0;
    }
    sort(a, a + N);
    vector<int> up, down;
    memset(vis, 0, sizeof(vis));
    for(int i = 0; i < N; i ++) {
        if(!vis[a[i]]) {
            up.push_back(a[i]);
            vis[a[i]] = 1;
        } else down.push_back(a[i]);
    }
    printf("YES\n");
    printf("%d\n", down.size());
    if(down.size() == 0) printf("\n");
    else {
        sort(down.begin(), down.end());
        for(int i = 0; i < down.size(); i ++)
            printf("%d%s", down[i], i != down.size() - 1 ? " " : "\n");
    }
    printf("%d\n", up.size());
    if(up.size() == 0) printf("\n");
    else {
        sort(up.rbegin(), up.rend());
        for(int i = 0; i < up.size(); i ++)
            printf("%d%s", up[i], i != up.size() - 1 ? " " : "\n");
    }
    return 0;
}
View Code

F. Graph Without Long Directed Paths

You are given a connected undirected graph consisting of nn vertices and mm edges. There are no self-loops or multiple edges in the given graph.

You have to direct its edges in such a way that the obtained directed graph does not contain any paths of length two or greater (where the length of path is denoted as the number of traversed edges).

Input

The first line contains two integer numbers nn and mm (2n21052≤n≤2⋅105, n1m2105n−1≤m≤2⋅105) — the number of vertices and edges, respectively.

The following mm lines contain edges: edge ii is given as a pair of vertices uiui, vivi (1ui,vin1≤ui,vi≤n, uiviui≠vi). There are no multiple edges in the given graph, i. e. for each pair (ui,viui,vi) there are no other pairs (ui,viui,vi) and (vi,uivi,ui) in the list of edges. It is also guaranteed that the given graph is connected (there is a path between any pair of vertex in the given graph).

Output

If it is impossible to direct edges of the given graph in such a way that the obtained directed graph does not contain paths of length at least two, print "NO" in the first line.

Otherwise print "YES" in the first line, and then print any suitable orientation of edges: a binary string (the string consisting only of '0' and '1') of length mm. The ii-th element of this string should be '0' if the ii-th edge of the graph should be directed from uiui to vivi, and '1' otherwise. Edges are numbered in the order they are given in the input.

Example
input
Copy
6 5
1 5
2 1
1 4
3 1
6 1
output
Copy
YES
10100
Note

The picture corresponding to the first example:

And one of possible answers:

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10;
int N, M;
int col[maxn], st[maxn], en[maxn];
vector<int> v[maxn];
bool flag = true;

void dfs(int st, int fa, int color) {
    col[st] = color;
    if(v[st].size() == 0) return;
    for(int i = 0; i < v[st].size(); i ++) {
        if(v[st][i] == fa) continue;
        if(col[v[st][i]] == -1)
            dfs(v[st][i], st, 1 - color);
        else if(col[v[st][i]] == col[st])
            flag = false;
    }
}

int main() {
    scanf("%d%d", &N, &M);
    memset(col, -1, sizeof(col));
    for(int i = 0; i < M; i ++) {
        scanf("%d%d", &st[i], &en[i]);
        v[st[i]].push_back(en[i]);
        v[en[i]].push_back(st[i]);
    }
    flag = true;
    dfs(1, -1, 0);
    if(!flag) printf("NO\n");
    else {
        printf("YES\n");
        for(int i = 0; i < M; i ++) {
            if(col[st[i]])
                printf("0");
            else printf("1");
        }
    }

    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/zlrrrr/p/10691771.html