CodeForces 1144(A - C)题解 补题(D)

A. Diverse Strings

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

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 (1≤n≤1001≤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

 

原地址在这

题意:

给你一个字符串,规定一个规则,规则是在这个字符串中,全部字符都是小写字符,并且,在这个字符串中必须全部都是顺序的一个串,输入不一定是有序的,但是当有序后是必须顺序的,比如:输入bdecfa,是无序的,但是排序后,是abcdef是有序的,相邻两个字符是相差一的,还有就是不能输出两个及两个以上相同字符,不然就是输出no,符合的都输出yes,这里定义了yes和no市随意的。

题解:

不用说了,直接模拟暴力吧。。。。

代码:

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <math.h>
#define ll long long
using namespace std ;

bool vis[30] ;
char s[105] ;

int main()
{
    int T ;
    scanf("%d" , &T) ;
    getchar() ;
    while(T --)
    {
        memset(vis , false , sizeof(vis)) ;
        scanf("%s" , s) ;
        int len = strlen(s) ;
        sort(s , s + len) ;
        int flag = 0 ;
        for(int i = 0 ; i < len ; i ++)
        {
            if(vis[s[i] - 'a'])
            {
                flag = 1 ;
                break ;
            }
            else vis[s[i] - 'a'] = true ;
            if(s[i] != s[i - 1] + 1 && i != 0)
            {
                flag = 1 ;
                break ;
            }
        }
        printf("%s\n" , flag ? "No" : "Yes") ;
    }
    return 0 ;
}

B. Parity Alternated Deletions

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 n−1n−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 (1≤n≤20001≤n≤2000) — the number of elements of aa.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1060≤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

题意:

给你n个数,让你将其不断的拿走,不过规定了拿的规则,那就是你只能一奇一偶的拿走,当然,是先拿奇数还是先拿偶数都行,只要是后续拿的数是按照一奇数一偶数的规则拿走的就行了,最后,问你想得到当不能再拿时,这些数的综合的最小数是多少。

题解:

你只需要记录一下奇数和偶数数量大小,将其分类,分为奇数和偶数,然后就可以分类讨论了,如果两类数相差一或者相等,那都是肯定可以全部拿完的,所以是0,当不同时,只需要那类数的数量多,将其减去另一类数的数量加一,在把剩下的数加起来就是需要求解的总和,当然是加的小的那些多余数啦。

代码:

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <math.h>
#define ll long long
using namespace std ;

int odd[2005] , even[2005] ;

int main()
{
    int n ;
    scanf("%d" , &n) ;
    int oddnum = 0 , evennum = 0 ;
    for(int i = 0 ; i < n ; i ++)
    {
        int x ;
        scanf("%d" , &x) ;
        if(x & 1) odd[oddnum ++] = x ;
        else even[evennum ++] = x ;
    }
    if(oddnum == evennum + 1 || evennum == oddnum + 1 || oddnum == evennum) printf("0\n") ;
    else
    {
        int ans = 0 ;
        if(oddnum > evennum)
        {
            sort(odd , odd + oddnum) ;
            for(int i = 0 ; i < oddnum - evennum - 1 ; i ++) ans += odd[i] ;
            printf("%d\n" , ans) ;
        }
        else
        {
            sort(even , even + evennum) ;
            for(int i = 0 ; i < evennum - oddnum - 1 ; i ++) ans += even[i] ;
            printf("%d\n" , ans) ;
        }
    }
    return 0 ;
}

C. Two Shuffled Sequences

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

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 (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤2⋅1050≤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 

题意:

其实看着这个的题面,我内心是拒绝去做的,但是想了一下,自己那么菜,还是只能做这种简单题开始吧。这题就是有两种数组,一种严格增,一种严格减,然后给你n个数,让你将其分成两个数组,就是一个严格增,一个严格减,当然你可以随意分开,输出就是输出两个数组的大小,在输出这个数组的数。小输出严格增,在输出严格减,如果不满足就输出NO。

题解:

既然是严格的,那么只要同一个数出现三次就是NO,然后你就随意了啦,两个的一个分在严格增,一个分在严格减,一个的你就随意了啦。

代码:

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <math.h>
#define ll long long
using namespace std ;

int a[200005] , b[200005] , vis[200005] ;

bool cmp(int a , int b) {return a > b ;}

int main()
{
    int  n ;
    scanf("%d" , &n) ;
    int numa = 0 , numb = 0 , flag = 0 ;
    for(int i = 0 ; i < n ; i ++)
    {
        int x ;
        scanf("%d" , &x) ;
        if(vis[x]) a[numa ++] = x , vis[x] ++ ;
        else b[numb ++] = x , vis[x] ++ ;
        if(vis[x] > 2) flag = 1 ;
    }
    if(flag) printf("NO\n") ;
    else
    {
        printf("YES\n") ;
        sort(a , a + numa) ;
        sort(b , b + numb , cmp) ;
        printf("%d\n" , numa) ;
        int flag1 = 0 ;
        for(int i = 0 ; i < numa ; i ++)
        {
            if(flag1) printf(" ") ;
            flag1 = 1 ;
            printf("%d" , a[i]) ;
        }
        printf("\n") ;
        flag1 = 0 ;
        printf("%d\n" , numb) ;
        for(int i = 0 ; i < numb ; i ++)
        {
            if(flag1) printf(" ") ;
            flag1 = 1 ;
            printf("%d" , b[i]) ;
        }
    }
    return 0 ;
}

补:

D. Equalize Them All

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):

  1. Choose a pair of indices (i,j) such that |i−j|=1 (indices i and j are adjacent) and set ai:=ai+|ai−aj|;
  2. Choose a pair of indices (i,j) such that |i−j|=1 (indices i and j are adjacent) and set ai:=ai−|ai−aj|.

The value |x| means the absolute value of x. For example, |4|=4, |−3|=3.

Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.

It is guaranteed that you always can obtain the array of equal elements using such operations.

Note that after each operation each element of the current array should not exceed 1018 by absolute value.

Input

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

The second line of the input contains n integers a1,a2,…,an (0≤ai≤2⋅105), where ai is the i-th element of a.

Output

In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.

In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (tp,ip,jp), where tp is either 1 or 2 (1means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and ip and jp are indices of adjacent elements of the array such that 1≤ip,jp≤n, |ip−jp|=1. See the examples for better understanding.

Note that after each operation each element of the current array should not exceed 1018 by absolute value.

If there are many possible answers, you can print any.

Examples

input

Copy

5
2 4 6 6 6

output

Copy

2
1 2 3 
1 1 2 

input

Copy

3
2 8 10

output

Copy

2
2 2 1 
2 3 2 

input

Copy

4
1 1 1 1

output

Copy

0

题意:

在这一题中真是我只能是弟弟,明明想法都是正确的,但是居然会操作问题 ,还是看了别人的题解才知道我的问题,看来这样的模拟我只能扮演弟弟。这题是这样的,给你n个数,让你将其经过m次操作,全部变成相同的数,要求操作次数最少,而且只能有两种操作方式:

1、将当前的数变成:a[i] = a[i] + |a[i]-a[j]|("||"表示是取结果的绝对值)而且i和j相邻

2、将当前的数变成:a[i] = a[i] - |a[i]-a[j]|("||"表示是取结果的绝对值)同样i 和j相邻

题解:

将里面的众数求出来,将其中某一个众数位置记录,然后再众数这一个位置,将其往两边,分别进行判断就行了,判断现在是1操作还是2操作,输出就行了,记住,判断操作完要进行改变这里的数,还有就是不必那么复杂的取判断存结果,直接就判断这书是大于还是小于众数,然后输出在相应的时候的需要的操作就行了。

代码:

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <math.h>
#define ll long long
using namespace std ;

int a[200005] , num[200005] ;

int main()
{
    int n ;
    scanf("%d" , &n) ;
    int maxx = 0 , maxxnum = 0 ;
    for(int i = 1 ; i <= n ; i ++)
    {
        scanf("%d" , &a[i]) ;
        num[a[i]] ++ ;
        if(num[a[i]] > maxx)
        {
            maxx = num[a[i]] ;
            maxxnum = a[i] ;
        }
    }
    int maxxi = 0 ;
    for(int i = 1 ; i <= n ; i ++) if(a[i] == maxxnum) {maxxi = i ; break ;}
    printf("%d\n" , n - maxx) ;
    for(int i = maxxi - 1 ; i > 0 ; i --)
    {
        if(a[i] < a[i + 1]) printf("1 %d %d\n" , i , i + 1) ;
        else if(a[i] > a[i + 1])printf("2 %d %d\n", i , i + 1) ;
        a[i] = a[i + 1] ;
    }
    for(int i = maxxi + 1 ; i <= n ; i ++)
    {
        if(a[i] < a[i - 1]) printf("1 %d %d\n" , i , i - 1) ;
        else if(a[i] > a[i - 1]) printf("2 %d %d\n" , i , i - 1) ;
        a[i] = a[i - 1] ;
    }
    return 0 ;
}

菜得不一样,菜出新高度。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值