Codeforces Round #238 (Div. 2) 总结

    昨晚的Code Forces,还在div2混。。。不过A了4题,加了近200分,直接变紫色了:),下一场就是div1了。

    题目总体并不难,下面简单总结一下:

A. Gravity Flip
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Chris is bored during his physics lessons (too easy), so he has built a toy box to keep himself occupied. The box is special, since it has the ability to change gravity.

There are n columns of toy cubes in the box arranged in a line. The i-th column contains ai cubes. At first, the gravity in the box is pulling the cubes downwards. When Chris switches the gravity, it begins to pull all the cubes to the right side of the box. The figure shows the initial and final configurations of the cubes in the box: the cubes that have changed their position are highlighted with orange.

Given the initial configuration of the toy cubes in the box, find the amounts of cubes in each of the n columns after the gravity switch!

Input

The first line of input contains an integer n (1 ≤ n ≤ 100), the number of the columns in the box. The next line contains nspace-separated integer numbers. The i-th number ai (1 ≤ ai ≤ 100) denotes the number of cubes in the i-th column.

Output

Output n integer numbers separated by spaces, where the i-th number is the amount of cubes in the i-th column after the gravity switch.

Sample test(s)
input
4
3 2 1 2
output
1 2 2 3 
input
3
2 3 8
output
2 3 8 
Note

The first example case is shown on the figure. The top cube of the first column falls to the top of the last column; the top cube of the second column falls to the top of the third column; the middle cube of the first column falls to the top of the second column.

In the second example case the gravity switch does not change the heights of the columns.


    A题,简单推测一下,应该就能知道这就是排序问题。一个sort搞定……代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;

int d[111];

int main()
{
    int n;
    scanf("%d",&n);

    for(int i=0;i<n;i++)
        scanf("%d",d+i);
    sort(d,d+n);

    for(int i=0;i<n;i++)
        printf("%d ", d[i]);
    puts("");
}

B. Domino Effect
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Chris knows there's no fun in playing dominoes, he thinks it's too random and doesn't require skill. Instead, he decided to play with the dominoes and make a "domino show".

Chris arranges n dominoes in a line, placing each piece vertically upright. In the beginning, he simultaneously pushes some of the dominoes either to the left or to the right. However, somewhere between every two dominoes pushed in the same direction there is at least one domino pushed in the opposite direction.

After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. The figure shows one possible example of the process.

Given the initial directions Chris has pushed the dominoes, find the number of the dominoes left standing vertically at the end of the process!

Input

The first line contains a single integer n (1 ≤ n ≤ 3000), the number of the dominoes in the line. The next line contains a character string s of length n. The i-th character of the string si is equal to

  • "L", if the i-th domino has been pushed to the left;
  • "R", if the i-th domino has been pushed to the right;
  • ".", if the i-th domino has not been pushed.

It is guaranteed that if si = sj = "L" and i < j, then there exists such k that i < k < j and sk = "R"; if si = sj = "R" and i < j, then there exists such k that i < k < j and sk = "L".

Output

Output a single integer, the number of the dominoes that remain vertical at the end of the process.

Sample test(s)
input
14
.L.R...LR..L..
output
4
input
5
R....
output
0
input
1
.
output
1
Note

The first example case is shown on the figure. The four pieces that remain standing vertically are highlighted with orange.

In the second example case, all pieces fall down since the first piece topples all the other pieces.

In the last example case, a single piece has not been pushed in either direction.


    B题,看起来觉得很复杂的样子。注意题目中的描述,两个R之间一定有L,两个L之间一定有R。那么整个序列中,L与R直接的肯定不会倒下来。R与L之间的,如果存在中间值,就会有一个不倒。如果没有L或者R,那么都不会倒下来。代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;

char str[10000];

int main()
{
    int n;
    scanf("%d",&n);

    scanf("%s",str);
    int len = strlen(str);

    int lpos = -1;
    int rpos = -1;

    int ans=0;

    int find = 0;

    for(int i=0; i<len; i++)
    {
        if(str[i]=='L')
        {
            lpos=i;
            if(find==0)
            {
                find = 2;
            }
            else if(find==1)
            {
                if((lpos-rpos)%2==0) ans++;
                find=2;
            }
        }
        if(str[i]=='R')
        {
            rpos=i;
            if(find==0)
            {
                ans+=i;
                find=1;
            }
            else if(find==2)
            {
                ans+=rpos-lpos-1;
                find=1;
            }
        }
    }

    if(rpos<lpos)
        ans+=n-1-lpos;
    else if(rpos==lpos && lpos==-1)
        ans=n;

    printf("%d\n", ans);
}

C. Unusual Product
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix.

The dot product of two integer number vectors x and y of size n is the sum of the products of the corresponding components of the vectors. The unusual square of an n × n square matrix A is defined as the sum of n dot products. The i-th of them is the dot product of the i-th row vector and the i-th column vector in the matrix A.

Fortunately for Chris, he has to work only in GF(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix A is binary: each element of A is either 0 or 1. For example, consider the following matrix A:

The unusual square of A is equal to (1·1 + 1·0 + 1·1) + (0·1 + 1·1 + 1·0) + (1·1 + 0·1 + 0·0) = 0 + 1 + 1 = 0.

However, there is much more to the homework. Chris has to process q queries; each query can be one of the following:

  1. given a row index i, flip all the values in the i-th row in A;
  2. given a column index i, flip all the values in the i-th column in A;
  3. find the unusual square of A.

To flip a bit value w means to change it to 1 - w, i.e., 1 changes to 0 and 0 changes to 1.

Given the initial matrix A, output the answers for each query of the third type! Can you solve Chris's homework?

Input

The first line of input contains an integer n (1 ≤ n ≤ 1000), the number of rows and the number of columns in the matrix A. The next n lines describe the matrix: the i-th line contains n space-separated bits and describes the i-th row of A. The j-th number of the i-th line aij (0 ≤ aij ≤ 1) is the element on the intersection of the i-th row and the j-th column of A.

The next line of input contains an integer q (1 ≤ q ≤ 106), the number of queries. Each of the next q lines describes a single query, which can be one of the following:

  • i — flip the values of the i-th row;
  • i — flip the values of the i-th column;
  • 3 — output the unusual square of A.

Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

Output

Let the number of the 3rd type queries in the input be m. Output a single string s of length m, where the i-th symbol of s is the value of the unusual square of A for the i-th query of the 3rd type as it appears in the input.

Sample test(s)
input
3
1 1 1
0 1 1
1 0 0
12
3
2 3
3
2 2
2 2
1 3
3
3
1 2
2 1
1 1
3
output
01001

    C题。建设n=3,9个值分别为

    a,b,c,

    d,e,f,

    g,h,i.

    那么所求的值就是aa+bd+cg+db+ee+fh+gc+hf+ii。出现两次的不论是0还是1都会等于0,所以式子就是aa+ee+ii。反转任意行或者一列都会使值改变。代码如下:

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

typedef long long LL;

int main()
{
    int n;
    scanf("%d",&n);

    int ans=0;

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            int tmp;
            scanf("%d",&tmp);
            if(i==j)
                ans^=tmp;
        }
    }

    int m;
    scanf("%d",&m);
    while(m--)
    {
        int op;
        scanf("%d",&op);
        if(op==3)
        {
            printf("%d",ans);
        }
        else
        {
            int tmp;
            scanf("%d",&tmp);
            ans = 1-ans;
        }
    }
    puts("");
}


D. Toy Sum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to solve more problems, so he decided to play a trick on Chris.

There are exactly s blocks in Chris's set, each block has a unique number from 1 to s. Chris's teacher picks a subset of blocks Xand keeps it to himself. He will give them back only if Chris can pick such a non-empty subset Y from the remaining blocks, that the equality holds:

"Are you kidding me?", asks Chris.

For example, consider a case where s = 8 and Chris's teacher took the blocks with numbers 1, 4 and 5. One way for Chris to choose a set is to pick the blocks with numbers 3 and 6, see figure. Then the required sums would be equal: (1 - 1) + (4 - 1) + (5 - 1) = (8 - 3) + (8 - 6) = 7.

However, now Chris has exactly s = 106 blocks. Given the set X of blocks his teacher chooses, help Chris to find the required setY!

Input

The first line of input contains a single integer n (1 ≤ n ≤ 5·105), the number of blocks in the set X. The next line contains ndistinct space-separated integers x1x2...xn (1 ≤ xi ≤ 106), the numbers of the blocks in X.

Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

Output

In the first line of output print a single integer m (1 ≤ m ≤ 106 - n), the number of blocks in the set Y. In the next line outputm distinct space-separated integers y1y2...ym (1 ≤ yi ≤ 106), such that the required equality holds. The sets X and Yshould not intersect, i.e. xi ≠ yj for all ij (1 ≤ i ≤ n1 ≤ j ≤ m). It is guaranteed that at least one solution always exists. If there are multiple solutions, output any of them.

Sample test(s)
input
3
1 4 5
output
2
999993 1000000
input
1
1
output
1
1000000 

    D题。有点难度了。不过依旧简单分析一下:如果取值范围是1-8,那么x选择这些值得时候实际的数值为

    0 1 2 3 4 5 6 7,y选择这些值时实际的数值为:

    7 6 5 4 3 2 1 0。

    通过这个图,我们可以猜测出:如果x选择了2,那么y可以选择对称位置的2。如果x同时选择了2和对称位置的5,那么y可以选择没有被使用的对称对,比如0和7。

    虽然没有证明,但是感觉是对的。代码也过了:

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

int d[1000010];
int num[1000010];

int main()
{
    int n;
    scanf("%d", &n);

    int maxn=1000000;

    for(int i=0;i<n;i++)
    {
        int tmp;
        scanf("%d",&tmp);
        d[tmp] = 1;
    }

    int pair=0;
    int half=1000000/2;
    int ans=0;

    for(int i=1;i<=half;i++)
    {
        if(d[i]==true && d[maxn-i+1]==true)
        {
            pair++;
        }
        else if(d[i])
        {
            num[ans++] = maxn-i+1;
            d[maxn-i+1] = 2;
        }
        else if(d[maxn-i+1])
        {
            num[ans++] = i;
            d[i] = 2;
        }
    }

    for(int i=1;i<=half;i++) if(d[i]==0 && d[maxn-i+1]==0)
    {
        if(pair==0) break;
        num[ans++] = i;
        num[ans++] = maxn-i+1;
        pair--;
    }

    printf("%d\n", ans);
    for(int i=0;i<ans;i++)
        printf("%d ",num[i]);
    puts("");
}

E. Graph Cutting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Chris is participating in a graph cutting contest. He's a pro. The time has come to test his skills to the fullest.

Chris is given a simple undirected connected graph with n vertices (numbered from 1 to n) and m edges. The problem is to cut it into edge-distinct paths of length 2. Formally, Chris has to partition all edges of the graph into pairs in such a way that the edges in a single pair are adjacent and each edge must be contained in exactly one pair.

For example, the figure shows a way Chris can cut a graph. The first sample test contains the description of this graph.

You are given a chance to compete with Chris. Find a way to cut the given graph or determine that it is impossible!

Input

The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 105), the number of vertices and the number of edges in the graph. The next m lines contain the description of the graph's edges. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ nai ≠ bi), the numbers of the vertices connected by the i-th edge. It is guaranteed that the given graph is simple (without self-loops and multi-edges) and connected.

Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

Output

If it is possible to cut the given graph into edge-distinct paths of length 2, output  lines. In the i-th line print three space-separated integers xiyi and zi, the description of the i-th path. The graph should contain this path, i.e., the graph should contain edges (xi, yi) and (yi, zi). Each edge should appear in exactly one path of length 2. If there are multiple solutions, output any of them.

If it is impossible to cut the given graph, print "No solution" (without quotes).

Sample test(s)
input
8 12
1 2
2 3
3 4
4 1
1 3
2 4
3 5
3 6
5 6
6 7
6 8
7 8
output
1 2 4
1 3 2
1 4 3
5 3 6
5 6 8
6 7 8
input
3 3
1 2
2 3
3 1
output
No solution
input
3 2
1 2
2 3
output
1 2 3

    E题……没什么思路,所以看了一下大牛的代码 = =||。

    当边数为奇数时,肯定是不行的。但是当边数为偶数时,就一定是可行的。怎么证明呢?简单来说,任意取一个点,取走该点上的偶数个边。如果该点的度数是偶数,那么取走这个点,剩下的图仍然是一个总边数为偶数边的图;如果度数是奇数,那么最后一条边不取,取该边连接的点为下一个取边的点,重复操作。因为总边数是偶数,所以最后一个取边点一定时偶数,而且刚好取完。

    知道思路,代码就很容易了:

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

const int maxn = 100010;
int first[maxn],nxt[maxn<<1],vv[maxn<<1];
bool vis[maxn];
int mark[maxn];
int edge[maxn];

void dfs(int n,int fa)
{
    vis[n] = true;

    for(int e=first[n];e;e=nxt[e]) if(vv[e]!=fa && !vis[vv[e]])
        dfs(vv[e], n);

    int top=0;
    for(int e=first[n];e;e=nxt[e]) if(vv[e]!=fa && mark[vv[e]]!=1)
        edge[top++]=vv[e];  // 记录可用点

    if(top&1)
        edge[top++]=fa,mark[n]=1;  // 用到当前点到父亲的边,当前点以后不可用
    else
        mark[n]=2;  // 不用到父亲的边,当前点还可以使用一次

    for(int i=0; i<top; i+=2)
    {
        printf("%d %d %d\n", edge[i], n, edge[i+1]);
        if(mark[edge[i]]==2) mark[edge[i]]=1;
        if(mark[edge[i+1]]==2) mark[edge[i+1]]=1;
    }
}

int main()
{
    int e=2;
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        nxt[e]=first[u],vv[e]=v,first[u]=e++;
        nxt[e]=first[v],vv[e]=u,first[v]=e++;
    }

    if(m&1)
        puts("No solution");
    else
        dfs(1,0);
}

    

    昨晚的收获就是比赛应该胆大心细,提交之前,好好检查一下题目。变紫没那么难,但是保持紫就有难度了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值