Codeforces Round #258 (Div. 2) 总结

123 篇文章 0 订阅
A. Game With Sticks
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are playing a game on a grid made of nhorizontal and m vertical sticks.

An intersection point is any point on the grid which is formed by the intersection of one horizontal stick and one vertical stick.

In the grid shown below, n = 3 and m = 3. There are n + m = 6 sticks in total (horizontal sticks are shown in red and vertical sticks are shown in green). There are n·m = 9 intersection points, numbered from 1 to 9.

The rules of the game are very simple. The players move in turns. Akshat won gold, so he makes the first move. During his/her move, a player must choose any remaining intersection point and remove from the grid all sticks which pass through this point. A player will lose the game if he/she cannot make a move (i.e. there are no intersection points remaining on the grid at his/her move).

Assume that both players play optimally. Who will win the game?

Input

The first line of input contains two space-separated integers, n and m (1 ≤ n, m ≤ 100).

Output

Print a single line containing "Akshat" or "Malvika" (without the quotes), depending on the winner of the game.

Sample test(s)
input
2 2
output
Malvika
input
2 3
output
Malvika
input
3 3
output
Akshat
Note

Explanation of the first sample:

The grid has four intersection points, numbered from 1 to 4.

If Akshat chooses intersection point 1, then he will remove two sticks (1 - 2 and 1 - 3). The resulting grid will look like this.

Now there is only one remaining intersection point (i.e. 4). Malvika must choose it and remove both remaining sticks. After her move the grid will be empty.

In the empty grid, Akshat cannot make any move, hence he will lose.

Since all 4 intersection points of the grid are equivalent, Akshat will lose no matter which one he picks.


题意:给你n+m根筷子,将他们分别水平、垂直放置,形成n*m个节点。两个人轮流选择一个点,将穿过这个点的两根筷子拿走,谁可以逼迫对方率先无法继续操作,谁就获胜,问获胜的是先手还是后手。
解:我是模拟的,感觉这样比较好理解。
<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int ok=1;
        while(n+m>1&&n>0&&m>0)
        {
            n--;
            m--;
            ok++;
        }
        if(ok&1)
            printf("Malvika\n");
        else
            printf("Akshat\n");
    }
    return 0;
}</span>

B. Sort the Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.

Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a.

The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).

Output

Print "yes" or "no" (without quotes), depending on the answer.

If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.

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

Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.

Sample 3. No segment can be reversed such that the array will be sorted.

Definitions

A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].

If you have an array a of size n and you reverse its segment [l, r], the array will become:

a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].

题意:题目的意思是只交换一列数中的两个数能否实现数列有序。做法是先得到有序的数列,然后和原有数列进行比较,判断是否只有两个位置数字被交换。

<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e5+10;
int a[maxm];
struct node
{
    int x,id;
} t[maxm];
int cmp(node p,node q)
{
    return p.x<q.x;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%d",&t[i].x);
            t[i].id=i+1;
        }
        sort(t,t+n,cmp);
        int sum=0;
        for(int i=0; i<n; i++)
        {
            a[i]=t[i].id;
            if(a[i]==(i+1))
            {
                sum++;
            }
        }
        if(sum==n)
        {
            printf("yes\n");
            printf("1 1\n");
        }
        else
        {
            int x=0,y=0;
            for(int i=0; i<n; i++)
            {
                if(a[i]!=(i+1))
                {
                    x=i;
                    for(int j=n-1; j>=i+1; j--)
                    {
                        if(a[j]!=(j+1))
                        {
                            y=j+1;
                            break;
                        }
                    }
                    break;
                }
            }
            reverse(a+x,a+y);
            int cnt=0;
            for(int i=0; i<n; i++)
            {
                if(a[i]==(i+1))
                    cnt++;
            }
            if(cnt==n)
            {
                printf("yes\n");
                printf("%d %d\n",x+1,y);
            }
            else
            {
                printf("no\n");
            }
        }
    }
    return 0;
}</span>

C. Predict Outcome of the Game
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.

You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these kgames. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.

You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?

Note that outcome of a match can not be a draw, it has to be either win or loss.

Input

The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105).

Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case.

Output

For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).

Sample test(s)
input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
output
yes
yes
yes
no
no
Note

Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.

Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".

Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).


题意:有3支球队(假设编号为1、2、3),总共要打 n 场比赛,已知已经错过这n场比赛中的 k 场,但从 k 场比赛中可以获取一些信息:设w1表示 k 场比赛中编号为1的球队赢了w1场比赛(w2、w3 类似),绝对值 w1-w2 = d1,  w2-w3 = d2,问能否通过设置n-k 的 赛果来使得n场比赛都打完后,编号为1的球队的胜利次数 == 编号为2的球队的胜利次数  == 编号为3的球队的胜利次数。注意:每场比赛的赛果只有胜和输之分,没有平局。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL __int64
int main()
{
    LL t;
    scanf("%I64d",&t);
    while(t--)
    {
        LL n,k,d1,d2;
        LL w1,w2,w3;
        LL x1,x2,x3;
        LL y1,y2,y3;
        LL z1,z2,z3;
        scanf("%I64d%I64d%I64d%I64d",&n,&k,&d1,&d2);
        if((n%3))
        {
            printf("no\n");
        }
        else
        {
            LL p=n/3;
            w1=(k+d2+2*d1);
            w2=(k+d2-d1);
            w3=(k-2*d2-d1);
            x1=(k+2*d1-d2);
            x2=(k-d1-d2);
            x3=(k-d1+2*d2);
            y1=(k-2*d1+d2);
            y2=(k+d1+d2);
            y3=(k+d1-2*d2);
            z1=(k-2*d1-d2);
            z2=(k+d1-d2);
            z3=(k+d1+2*d2);
            if(w1/3<=p&&w2/3<=p&&w3/3<=p&&w1/3>=0&&w2/3>=0&&w3/3>=0&&w1%3==0&&w2%3==0&&w3%3==0)
                printf("yes\n");
            else if(x1/3<=p&&x2/3<=p&&x3/3<=p&&x1/3>=0&&x2/3>=0&&x3/3>=0&&x1%3==0&&x2%3==0&&x3%3==0)
                printf("yes\n");
            else if(y1/3<=p&&y2/3<=p&&y3/3<=p&&y1/3>=0&&y2/3>=0&&y3/3>=0&&y1%3==0&&y2%3==0&&y3%3==0)
                printf("yes\n");
            else if(z1/3<=p&&z2/3<=p&&z3/3<=p&&z1/3>=0&&z2/3>=0&&z3/3>=0&&z1%3==0&&z2%3==0&&z3%3==0)
                printf("yes\n");
            else
                printf("no\n");
        }
    }
    return 0;
}


D. Count Good Substrings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

We call a string good, if after merging all the consecutive equal characters, the resulting string is palindrome. For example, "aabba" is good, because after the merging step it will become "aba".

Given a string, you have to find two values:

  1. the number of good substrings of even length;
  2. the number of good substrings of odd length.
Input

The first line of the input contains a single string of length n (1 ≤ n ≤ 105). Each character of the string will be either 'a' or 'b'.

Output

Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.

Sample test(s)
input
bb
output
1 2
input
baab
output
2 4
input
babb
output
2 5
input
babaa
output
2 7
Note

In example 1, there are three good substrings ("b", "b", and "bb"). One of them has even length and two of them have odd length.

In example 2, there are six good substrings (i.e. "b", "a", "a", "b", "aa", "baab"). Two of them have even length and four of them have odd length.

In example 3, there are seven good substrings (i.e. "b", "a", "b", "b", "bb", "bab", "babb"). Two of them have even length and five of them have odd length.

Definitions

A substring s[l, r] (1 ≤ l ≤ r ≤ n) of string s = s1s2... sn is string slsl + 1... sr.

A string s = s1s2... sn is a palindrome if it is equal to string snsn - 1... s1.

题意:若一个字符串删除相同元素后剩下的是回文串,则称之为好串。给出一个由a和b组成的字符串,求奇数元素的好子串数量和偶数元素的好子串数量。

题解:DP统计。

注意字符串只有a和b,删除重复元素以后肯定是ababababab这样,两个a和之间的元素组成的串肯定是回文串,根本不用回文的算法。

由s[i]结尾的回文串数量等于0~i之间和s[i]相同字符的数量。要分奇数偶数长度,这和下标的奇偶有关。我们观察一下当前下标、之前的和当前字符相同的字符的下标、ans统计下标的关系(ans[0]记录偶数,ans[1]记录奇数)

now 之前 ans

可以发现当前下标为奇数,当前元素为a,ans偶+=之前的偶数位置的a的数量,ans奇+=之前奇数位置a的数量。

           当前下标为偶数,当前元素为a,ans偶+=之前的奇数位置的a的数量,ans奇+=之前偶数位置a的数量。

这样就总结出了比较简单的统计方法。(其实不总结,直接用if也可以,比赛时最好用直接点的方法,我这是事后做的才搞这种)

这样我们从头扫到尾,慢慢统计字符的数量和奇数偶数回文串的数量就行了。

 

<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL __int64
const int maxm=1e5+10;
char s[maxm];
int sumodd[5];
int sumeven[5];
int main()
{
    while(scanf("%s",s)!=EOF)
    {
        memset(sumodd,0,sizeof(sumodd));
        memset(sumeven,0,sizeof(sumeven));
        LL odd=0,even=0;
        for(int i=0;i<strlen(s);i++)
        {
            int t=s[i]-'a';
            odd++;
            if(i&1)
            {
                odd+=sumodd[t];
                even+=sumeven[t];
                sumodd[t]++;
            }
            else
            {
                odd+=sumeven[t];
                even+=sumodd[t];
                sumeven[t]++;
            }
        }
        printf("%I64d %I64d\n",even,odd);
    }
    return 0;
}</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值