Codeforces Global Round 9——A.B.C

Codeforces Global Round 9

A. Sign Flipping

题目描述
You are given nn integers a1,a2,…,an, where nn is odd. You are allowed to flip the sign of some (possibly all or none) of them. You wish to perform these flips in such a way that the following conditions hold:At least (n−1)/2 of the adjacent differences ai+1−ai for i=1,2,…,n−1 are greater than or equal to 0.At least (n−1)/2 of the adjacent differences ai+1−ai for i=1,2,…,n−1 are less than or equal to 0.Find any valid way to flip the signs. It can be shown that under the given constraints, there always exists at least one choice of signs to flip that satisfies the required condition. If there are several solutions, you can find any of them.
Input
The input consists of multiple test cases. The first line contains an integer tt (1≤t≤5001≤t≤500) — the number of test cases. The description of the test cases follows.The first line of each test case contains an integer nn (3≤n≤993≤n≤99, nn is odd) — the number of integers given to you.The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109) — the numbers themselves.It is guaranteed that the sum of nn over all test cases does not exceed 1000010000.
Output
For each test case, print n integers b1,b2,…,bn,corresponding to the integers after flipping signs. bi has to be equal to either ai or −ai, and of the adjacent differences bi+1−bi for i=1,…,n−1, at least (n−1)/2should be non-negative and at least (n−1)/2 should be non-positive.It can be shown that under the given constraints, there always exists at least one choice of signs to flip that satisfies the required condition. If there are several solutions, you can find any of them.
Example
input

5
3
-2 4 3
5
1 1 1 1 1
5
-2 4 7 -6 4
9
9 7 -4 -2 1 -3 9 -4 -5
9
-4 1 9 4 8 9 5 1 -9

output

-2 -4 3
1 1 1 1 1
-2 -4 7 -6 4
-9 -7 -4 2 1 -3 -9 -4 -5
4 -1 -9 -4 -8 -9 -5 -1 9

题意:给你n个数(n为奇数),让你输出操作后的这n个数,使满足a[i+1]-a[i]>=0的个数为(n-1)/2个,a[i+1]-a[i]<=0的个数也为(n-1)/2个。可进行的操作:不能改变n个数的顺序,只能改变每一个数的正负。
代码

#include
#include
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,a[10005];
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            if(i%2==1&&a[i]>0)
                a[i]=-a[i];
            if(i%2==0&&a[i]<0)
                a[i]=-a[i];
        }
        for(int i=1; i<n; i++)
            printf("%d ",a[i]);
        printf("%d\n",a[n]);
    }
    return 0;
}

B. Neighbor Grid

题目描述
You are given a grid with n rows and m columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number k>0 written on it, then exactly k of its neighboring cells have a number greater than 0 written on them. Note that if the number in the cell is 0, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by 1. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
Input
The input consists of multiple test cases. The first line contains an integer t (1≤t≤50001≤t≤5000) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers n and m (2≤n,m≤3002≤n,m≤300) — the number of rows and columns, respectively.The following n lines contain m integers each, the j-th element in the i-th line ai,j is the number written in the jj-th cell of the i-th row (0≤ai,j≤10^9).It is guaranteed that the sum of n⋅m over all test cases does not exceed 10^5.
Output
If it is impossible to obtain a good grid, print a single line containing “NO”.Otherwise, print a single line containing “YES”, followed by n lines each containing m integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero).If there are multiple possible answers, you may print any of them.
Example
input

5
3 4
0 0 0 0
0 1 0 0
0 0 0 0
2 2
3 0
0 0
2 2
0 0
0 0
2 3
0 0 0
0 4 0
4 4
0 0 0 0
0 2 0 1
0 0 0 0
0 0 0 0

output

YES
0 0 0 0
0 1 1 0
0 0 0 0
NO
YES
0 0
0 0
NO
YES
0 1 0 0
1 4 2 1
0 2 0 0
1 3 1 0

题意:给你一个n*m的矩阵,矩阵的每个格有对应的数值,该数值表示与他有公共边的格子且该各自的数值不为零的这样的格子个数。一开始给的矩阵可能不符合要求,所以经过改正使得矩阵是合格的。
思路:一开始就是想着那就判断当前格子是否在合理范围内(四个角最大数值为2,边上的格子最大数值为3,内部格子最大数值为4),然后就对不符合的格子进行改正,在其附近为0的格子上加一,这样操作之后,就没有想到又会引发该格子的周围格子变化。然后发现,反正所有的格子都有可能变化,那么最极端情况就是所有格子都最大化,这样就可以全部满足,在这之前需要判断是否输入的矩阵的格子的数值在合理范围内。
代码

#include
#include
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m,a[305][305];
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                scanf("%d",&a[i][j]);
        int flag=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                int maxn=4;
                if(i==0||i==n-1||j==0||j==m-1)
                    maxn--;
                if((i==0&&j==0)||(i==n-1&&j==0)||(i==n-1&&j==m-1)||(i==0&&j==m-1))
                    maxn=2;
                if(a[i][j]>maxn)
                {
                    flag=1;
                    break;
                }
            }
            if(flag==1)
                break;
        }
        if(flag==1)
            printf("NO\n");
        else
        {
            printf("YES\n");
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                    if((i==0&&j==0)||(i==n-1&&j==0)||(i==n-1&&j==m-1)||(i==0&&j==m-1))
                        a[i][j]=2;
                    else if(i==0||i==n-1||j==0||j==m-1)
                        a[i][j]=3;
                    else
                        a[i][j]=4;
                }
            }
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                    printf("%d ",a[i][j]);
                printf("\n");
            }
        }
    }
    return 0;
}

C. Element Extermination

题目描述
You are given an array a of length n, which initially is a permutation of numbers from 1 to n. In one operation, you can choose an index ii (1≤i Input
The first line contains a single integer t (1≤t≤2⋅104) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer nn (2≤n≤3⋅105) — the length of the array.The second line of each test case contains n integers a1, a2, …, an (1≤ai≤n, ai are pairwise distinct) — elements of the array.It is guaranteed that the sum of n over all test cases doesn’t exceed 3⋅105.
Output
For each test case, output on a single line the word “YES” if it is possible to reduce the array to a single element using the aforementioned operation, or “NO” if it is impossible to do so.
Example
input

4
3
1 2 3
4
3 1 2 4
3
2 3 1
6
2 4 6 1 3 5

output

YES
YES
NO
YES

题意:给n个数的排列(乱序),若有a[i] 思路 :通过观察(em…)可以发现只要最左边的数小于最右边的数就可以删到只剩一个数。
代码

#include
#include
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,a[300005];
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        if(a[0]<a[n-1])
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

D. Replace by MEX

你可能感兴趣的:(codeforces)