Codeforces Round #258 (Div. 2)(A,B,C,D)
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?
The first line of input contains two space-separated integers, n and m (1 ≤ n, m ≤ 100).
Print a single line containing "Akshat" or "Malvika" (without the quotes), depending on the winner of the game.
2 2
Malvika
2 3
Malvika
3 3
Akshat
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根线,如图摆放,将交点从1到n×m编号,两个人轮流走到交点上,然后将经过这个交点的某条线移走,最后没有交点可走的人输。
思路 :因为拿走一根线的话,所有别的跟这个线的交点也就没有了,所以实际上求的是min(n,m)对2取余。
//258A
#include <cstdio>
#include <cstring>
#include <iostream> using namespace std ; int main()
{
int n, m ;
while(scanf("%d %d",&n,&m) != EOF)
{
int s = min(n,m) ;
if(s % )
puts("Akshat") ;
else puts("Malvika") ;
}
return ;
}
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.
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).
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.
3
3 2 1
yes
1 3
4
2 1 3 4
yes
1 2
4
3 1 2 4
no
2
1 2
yes
1 1
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].
题意 : 通过将数组a里的某一段逆置之后,能否将这个数组变成有序的。
思路 :既然是逆置的,例如1 2 3 6 5 4 7 8 9
那就从头开始找大于下一个数的那个数的位置,然后再接着找小于下一个数的那个数的位置,将这两者之间的逆置,如果这样是有序的了,就yes,除此之外就是不可能的情况了,比如有两段需要逆置,或者这段逆置的也不是倒着有序的等等。
//258B
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm> using namespace std ; int a[],b[] ; int main()
{
int n ;
while(cin >> n)
{
for(int i = ; i < n ; i++)
{
cin >> a[i] ;
b[i] = a[i] ;
}
int be = -,en = - ;
for(int i = ; i < n- ; i++)
{
if(a[i] > a[i+])
{
be = i ;
break ;
}
}
if(be == -) {puts("yes") ; puts("1 1");continue ;}
int i ;
for(i = be ; i < n- ; i++)
{
if(a[i] < a[i+])
{
en = i ;
break ;
}
}
if(i == n-) en = n- ;
reverse(a+be,a+en+) ;
sort(b,b+n) ;
for(i = ; i < n ; i++)
{
if(a[i] != b[i]) break ;
}
if(i == n) {
puts("yes") ;
cout << be + << " "<< + en <<endl ;
}
else puts("no") ;
}
return ;
}
C. Predict Outcome of the Game
time limit per test:1 second
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.
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.
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).
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
yes
yes
yes
no
no
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).
题意 :三个队伍,一共n场比赛,已经进行了k场,在已经进行的k场中,三个队伍分别赢了A,B,C场,|A-B|=d1.|B-C|=d2 .在符合这些条件的情况下,安排这k场比赛以及还未比的n-k场,要求最后三个队伍打平。
思路 : 因为三个队伍要打平,所以n一定是可以整除3的,否则绝对无法打平。然后根据d1和d2的正负问题,即ABC的大小问题,分为正正、正负、负正、负负四种情况。
通过A+B+C=k与|A-B|=d1,|B-C|=d2,三式联合可求得ABC的表达式,然后再判断是不是满足各种要求
#include <cstdio>
#include <iostream>
#define LL long long using namespace std ; bool dose(LL n, LL k, LL d1,LL d2)
{
for(int i = - ; i <= ; i ++)
for(int j = - ; j <= ; j++)
{
if(i == || j == )
continue ;
LL D1 = d1*i ,D2 = d2*j ;
LL B = (k - D1 + D2) / ;
if((k - D1 + D2) % ) return false ;
if(B >= && B <= k)
{
LL A = D1+B,C = B-D2 ;
if(A >= && A <= k && C >= && C <= k)
if(A <= n/ && B <= n/ && C <= n/)
return true ;
}
}
return false ;
}
int main()
{
LL T,n,k,d1,d2 ;
cin >> T;
while(T--)
{
cin>>n >> k >> d1 >> d2 ;
if(n % )
{
puts("no" );
continue ;
}
bool flag = dose(n,k,d1,d2) ;
if(flag) puts("yes") ;
else puts("no") ;
}
return ;
}
//A + B + C = k ---(1)
//|A - B| = d1 ---(a)
//|B - C| = d2 ---(b)
#include<cstdio>
#include<iostream> using namespace std; #define LL long long
LL n,k,d1,d2,ave,x; int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>k>>d1>>d2;
if(n % )//如果n无法整除3,则无法达到三个平局
{
puts("no") ;
continue ;
}
ave = n/;
bool flag = false ;
//A>B&&B>C时,A-B=d1,B-C=d2
//最小的是C,最大的是A
//通过式子连立可求出C= (k-d1-2*d2)/3
if( (k-d1-*d2) % == && (k-d1-*d2) >= )//C必须是整数并且大于等于0
{
x = (k-d1-*d2)/;
if(x+d1+d2<=ave)//三者之中最大的是A,只要A小于等于平均值,BC都会满足
flag = true;
}
//A>B&&B<C时,A-B=d1,B-C=-d2
//最小的是B,AC无法辨大小
//通过式子连立可求出B = (k-d1-d2)/3
if( (k-d1-d2) % == && (k-d1-d2) >= )
{
x = (k-d1-d2)/;
if(x+d1<=ave&&x+d2<=ave)//AC比B大,只要判断AC是不是都小于平均数即可
flag = true;
}
//A<B&&B<C时,A-B=-d1,B-C=-d2
//最小的是A,C最大
//通过式子连立可求出A = (k-2*d1-d2)/3
if( (k-*d1-d2) % == && (k-*d1-d2) >= )
{
x = (k-*d1-d2)/;
if(x+d1+d2 <= ave)
flag = true;
}
//A<B&&B>C时,A-B=-d1,B-C=d2
//最大的是B,AC无法辨大小
//通过式子连立可求出A = (k-2*d1+d2)/3,C=(k+d1-2*d2)
if( (k+d2-*d1)%== && (k+d2-*d1)>= && (k+d1-*d2)%== && (k+d1-*d2)>=)
{
x = (k+d2-*d1)/;
if(x+d1 <= ave)
flag = true;
}
if(flag) puts("yes");
else puts("no");
}
return ;
}
D. Count Good Substrings
time limit per test:1 second
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:
- the number of good substrings of even length;
- the number of good substrings of odd length.
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'.
Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.
bb
1 2
baab
2 4
babb
2 5
babaa
2 7
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.
题意 : 如果将一个字符串中连续相同的字符合并成一个该字符之后,合并之后的字符串如果是回文的话,我们定义原串为good。然后给你一个字符串,让你找他的是good的子串中长度为奇数的串的个数以及长度为偶数的串的个数。
思路 :先看偶数长度的串,子串 slsl + 1... sr如果是偶数长度的话,那么左边界 L 和右边界r,肯定一个在奇数位置一个在偶数位置,这样长度就是偶数。对,忘了说,因为可以合并连续的相同的字母,所以只要一个串首跟尾是同字母,就肯定是good。求偶数长度的话,只要求出奇数位置的a,偶数位置的a有多少个,两者相乘,就可以求出以a为头和尾的字符串的并且长度是偶数的串的个数。同理,奇数长度的串,只要两者都在奇数位置或者都在偶数位置就可以。
//258D
#include <stdio.h>
#include <string.h>
#include <iostream> typedef long long LL ; using namespace std ; string str ; LL calcu(LL x)
{
return x * ( x - ) / ;
}
int main()
{
while(cin >> str)
{
LL odda = ,oddb = , evena = ,evenb = ;
for(int i = ; i < str.length() ; i++)
{
if(str[i] == 'a')
{
if(i & )
odda ++ ;
else evena ++ ;
}
else
{
if(i & )
oddb ++ ;
else evenb ++ ;
}
}
LL Odd = evena*odda+evenb*oddb ;
LL Even = str.length() + calcu(evena) + calcu(oddb) + calcu(odda) + calcu(evenb) ;
cout<< Odd <<" "<< Even << endl ;
}
return ;
}
Codeforces Round #258 (Div. 2)(A,B,C,D)的更多相关文章
- Codeforces Round #258 (Div. 2)[ABCD]
Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...
- Codeforces Round #258 (Div. 2) 小结
A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...
- Codeforces Round #524 (Div. 2)(前三题题解)
这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...
- Codeforces Round #624 (Div. 3)(题解)
Codeforces Round #624 (Div.3) 题目地址:https://codeforces.ml/contest/1311 B题:WeirdSort 题意:给出含有n个元素的数组a,和 ...
- Codeforces Round #253 (Div. 1) (A, B, C)
Codeforces Round #253 (Div. 1) 题目链接 A:给定一些牌,然后如今要提示一些牌的信息,要求提示最少,使得全部牌能够被分辨出来. 思路:一共2^10种情况,直接暴力枚举,然 ...
- D2. Kirk and a Binary String (hard version) D1 Kirk and a Binary String (easy version) Codeforces Round #581 (Div. 2) (实现,构造)
D2. Kirk and a Binary String (hard version) time limit per test1 second memory limit per test256 meg ...
- Codeforces Round #404 (Div. 2) A,B,C,D,E 暴力,暴力,二分,范德蒙恒等式,树状数组+分块
题目链接:http://codeforces.com/contest/785 A. Anton and Polyhedrons time limit per test 2 seconds memory ...
- Codeforces Round #383 (Div. 2) A,B,C,D 循环节,标记,暴力,并查集+分组背包
A. Arpa’s hard exam and Mehrdad’s naive cheat time limit per test 1 second memory limit per test 256 ...
- Codeforces Round #219 (Div. 1)(完全)
戳我看题目 A:给你n个数,要求尽可能多的找出匹配,如果两个数匹配,则ai*2 <= aj 排序,从中间切断,分成相等的两半后,对于较大的那一半,从大到小遍历,对于每个数在左边那组找到最大的满足 ...
随机推荐
- hdu 5272 Dylans loves numbers
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5272 Dylans loves numbers Description Who is Dylans?Y ...
- core java 5~6(OOP & 高级语言特征)
MODULE 5 OOP 面向对象程序设计--------------------------------------------------------Object Oriented Program ...
- NFC应用实例
package com.example.mynfcdemon; import android.app.Activity;import android.nfc.NfcAdapter;import and ...
- 6.Knockout.Js(加载或保存JSON数据)
前言 Knockout可以实现很复杂的客户端交互,但是几乎所有的web应用程序都要和服务器端交换数据(至少为了本地存储需要序列化数据),交换数据最方便的就是使用JSON格式 – 大多数的Ajax应用程 ...
- ASP.NET MVC掉过的坑_MVC初识及MVC应用程序结构
APS.Net MVC 浅谈[转] 来自MSDN 点击访问 MVC 理论结构 模型-视图-控制器 (MVC) 体系结构模式将应用程序分成三个主要组件:模型.视图和控制器. ASP.NET MVC 框架 ...
- ExtJS MVC 学习手记3
在演示应用中,我们已经创建好了viewport,并为之添加了一个菜单树.但也仅仅是这样,点击树或应用的其他地方获得不到任何响应.这个演示应用还是一个死的应用. 接下来,我们让这个应用活起来. 首先,给 ...
- Qt之SQL数据库
---------------------------- http://blog.csdn.net/reborntercel/article/details/6991147 http://blog.c ...
- Qt入门1---widget、mainwindow和Dialog区别
摘要: 看了一个月的Qt,居然没有理清Qt中 ------------------------------------ 1.QMainWindow A main window provides a f ...
- MSSQL Transaction[事务] and Procedure[存储过程]
--事务分三种 --1.显示事务 --我们手动begin transaction ...... commit transaction/rollback transaction --上面这种写法叫做“显 ...
- 玩耍Hibernate系列(二)--基础知识
Hibernate思维导图 Hibernate映射 关于hibernate的映射要说明的一点就是关于ID的访问权限,peroperty以及field的区别: 表的主键在内存中对应一个OID对象描述 ...