Codeforces Round #310 (Div. 2)

看来最近身体状况并不是很好啊。。。

最近竟然有一次熬夜,然后心脏开始难受了,我可不想有心脏病啊,劳资还要上分呢。。。 ==b

首先是第一题,手速慢了一些,脑子也慢了些,然后回过神来,woc,1600个人过了。。。虽然是1A,但是时间太长,所以导致我后面的题目都没来的及看。

A. Case of the Zeros and Ones
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.

Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacentpositions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.

Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.

Input

First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has.

The second line contains the string of length n consisting only from zeros and ones.

Output

Output the minimum length of the string that may remain after applying the described operations several times.

Sample test(s)
input
4
1100
output
0
input
5
01010
output
1
input
8
11101111
output
6
Note

In the first sample test it is possible to change the string like the following: .

In the second sample test it is possible to change the string like the following: .

In the third sample test it is possible to change the string like the following: .


这道题目的大概意思是:

现在这个人有一个由0与1所组成的字符串,然后如果遇到相邻的0和1在一起的情况的话,那么就可以把这两个数给去掉,然后字符串的长度就会减少2,然后一直进行这个操作直到不可以,然后问你这个字符串最后的长度是多少。

一开始我想的是模拟的,但是由于数据太大而且一下子过了那么多人,所以我就觉得肯定不是这么做的。。。

那么这里的做法是:

最多减少的次数肯定是0或是1中个数较少的那个,然后乘以2,就是最多减少的长度。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
#define maxn 222222
char a[maxn];
int main(){
	int n;
	scanf("%d",&n);
	scanf("%s",a);
	int tx,ty;
	int num0,num1;
	num0=num1=0;
	for(int i=0;i<n;i++){
		if(a[i]=='0') num0++;
		if(a[i]=='1') num1++;
	}
	int num=min(num0,num1);
	int len=n-num*2;
	printf("%d\n",len);
}

B. Case of Fake Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was.

Its most important components are a button and a line of n similar gears. Each gear has n teeth containing all numbers from 0 to n - 1 in the counter-clockwise order. When you push a button, the first gear rotates clockwise, then the second gear rotates counter-clockwise, the the third gear rotates clockwise an so on.

Besides, each gear has exactly one active tooth. When a gear turns, a new active tooth is the one following after the current active tooth according to the direction of the rotation. For example, if n = 5, and the active tooth is the one containing number 0, then clockwise rotation makes the tooth with number 1 active, or the counter-clockwise rotating makes the tooth number 4 active.

Andrewid remembers that the real puzzle has the following property: you can push the button multiple times in such a way that in the end the numbers on the active teeth of the gears from first to last form sequence 0, 1, 2, ..., n - 1. Write a program that determines whether the given puzzle is real or fake.

Input

The first line contains integer n (1 ≤ n ≤ 1000) — the number of gears.

The second line contains n digits a1, a2, ..., an (0 ≤ ai ≤ n - 1) — the sequence of active teeth: the active tooth of the i-th gear contains number ai.

Output

In a single line print "Yes" (without the quotes), if the given Stolp's gears puzzle is real, and "No" (without the quotes) otherwise.

Sample test(s)
input
3
1 0 0
output
Yes
input
5
4 2 1 4 3
output
Yes
input
4
0 2 3 1
output
No
Note

In the first sample test when you push the button for the first time, the sequence of active teeth will be 2 2 1, when you push it for the second time, you get 0 1 2.


然后是B题,题目读了好久,最后发现是水题一枚~

题意大概是:要你最后完成的是0~n-1的按顺序排列的齿轮,如果可以达到这个目的,那么我们就输出Yes,否则输出No。

大概意思是:

如果那个是奇数位上的齿轮,那么它是顺时针旋转的,注意一个细节,当为n-1时,它则要返回到0去。

如果是偶数位上的齿轮,那么它是逆时针旋转的,当为0时,它要返回到n-1去。

如果在很多次循环之后,出现了循环节(就是出现了和原先一样的数列),那么我们就认为这个任务是永远都不可能完成的。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
#define maxn 1111
int a[maxn],tt[maxn];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
        tt[i]=a[i];
    }
    bool ff=true;
    #if 1
    for(int i=1;i<=n;i++){
        if(a[i]==i-1) continue;
        else {
           ff=false;
           break;
        }
    }
    if(ff) {
        puts("Yes");
        return 0;
    }
    #endif
    while(1){
        ff=true;
        bool flag=true;
        for(int i=1;i<=n;i++){
            if(i%2){
                if(tt[i]==n-1) tt[i]=0;
                else tt[i]++;
            }
            else if(i%2==0){
                if(tt[i]==0) tt[i]=n-1;
                else tt[i]--;
            }
        }
        for(int i=1;i<=n;i++){
            if(tt[i]==i-1) continue;
            else {
               ff=false;
               break;
            }
        }
        if(ff){
            printf("Yes\n");
            break;
        }
        for(int i=1;i<=n;i++){
            if(a[i]!=tt[i]){
                flag=false;
                break;
            }
        }
        if(flag){
            printf("No\n"); 
            break;
        }
        
    }
}


C. Case of Matryoshkas
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.

The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.

In one second, you can perform one of the two following operations:

  • Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
  • Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out ofb.

According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.

Input

The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.

The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbersai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).

It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.

Output

In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.

Sample test(s)
input
3 2
2 1 2
1 3
output
1
input
7 3
3 1 3 7
2 2 5
2 4 6
output
10
Note

In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get1 → 2 → 3.

In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.


唔。。。C题一开始看懂了,但是只剩下20分钟左右的时间,然后就没仔细去想。

其实就是一个纯的想法题。

首先题目的意思是:

给出n,k; n代表的意思是总共有几个分开来的俄罗斯套娃,k代表的意思是有几个已经被合并在一起的俄罗斯套娃。

然后题目的要求是你需要通过一连串的操作,比如说把它取出来或是放进去,然后达到最后要满足这些俄罗斯套娃是要按顺序排列的,也就是说它们的序号是按照1~n按顺序排列的。然后输出的是完成这个任务所需的最小步数。

这里题目设置了两个操作(也就是说你只能通过这两个操作来完成这个任务):

1.a没有被放置在其他套娃中,且b没有包含其他任何套娃且没有被放置在其他任何套娃中(也就是说b是单独的一个才可以),那么你可以把a放到b中去。

2.如果a直接放在b套娃中,且b没有放在其他的套娃中,那么你可以把a从b中拿出来(这里a套娃中可以包含多个,但是b必须是单独的一个才可以)

思路是:

除了直接与1号套娃直接相连的套娃,其他的套娃都得全部拆掉,因为你得把1号套娃它们放进去吧,所以其他的都必须是单个的才行

所以这里我们只需要看一下与1号套娃相连的有几个,然后它们是不用被拆掉的,然后加上其余套娃拆掉的次数和再次合起来的次数,最后就是最少的所需的次数了。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
#define maxn 111111
int a[maxn],m[maxn],t[maxn];
int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    int num=0,sum=0;
    for(int i=1;i<=k;i++){
        scanf("%d",&m[i]);
        for(int j=0;j<m[i];j++){
            scanf("%d",&t[j]);
        }
        if(t[0]==1){
            num=1;
            for(int j=1;j<m[i];j++){
                if(t[j]==t[0]+j) num++;
            }
            sum=n-k-num+1;
        }
    }
    sum+=n-num;
    printf("%d\n",sum);
}

希望自己的身体还是能够好好的,毕竟我还想再追梦啊。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值