Codeforces Round #306 (Div. 2) (ABCE题解)
2015-06-09 01:34
399 查看
比赛链接:http://codeforces.com/contest/550
A. Two Substrings
time limit per test
2 seconds
memory limit per test
256 megabytes
You are given string s. Your task is to determine if the given strings contains two non-overlapping substrings "AB"
and "BA" (the substrings can go in any order).
Input
The only line of input contains a string
s of length between 1 and
105 consisting of uppercase Latin letters.
Output
Print "YES" (without the quotes), if strings contains two non-overlapping substrings "AB" and "BA",
and "NO" otherwise.
Sample test(s)
Input
Output
Input
Output
Input
Output
Note
In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO".
In the second sample test there are the following occurrences of the substrings:BACFAB.
In the third sample test there is no substring "AB" nor substring "BA".
题目大意:给一个字符串,问能否找到两个不相互覆盖的子串"AB"和"BA"
题目分析:暴力扫4次,第一二次先扫"AB"后扫"BA",第三四次先扫"BA"后扫"AB",注意这组样例ABACCCAB
B. Preparing Olympiad
time limit per test
2 seconds
memory limit per test
256 megabytes
You have n problems. You have estimated the difficulty of thei-th one as integer
ci. Now you want to prepare a problemset for a contest, using some of the problems you've made.
A problemset for the contest must consist of at least two problems. You think that the total difficulty of the problems of the contest must be at leastl and at most
r. Also, you think that the difference between difficulties of the easiest and the hardest of the chosen problems must be at leastx.
Find the number of ways to choose a problemset for the contest.
Input
The first line contains four integers n,l,
r,x (1 ≤ n ≤ 15,1 ≤ l ≤ r ≤ 109,1 ≤ x ≤ 106)
— the number of problems you have, the minimum and maximum value of total difficulty of the problemset and the minimum difference in difficulty between the hardest problem in the pack and the easiest one, respectively.
The second line contains n integers
c1, c2, ..., cn (1 ≤ ci ≤ 106)
— the difficulty of each problem.
Output
Print the number of ways to choose a suitable problemset for the contest.
Sample test(s)
Input
Output
Input
Output
Input
Output
Note
In the first example two sets are suitable, one consisting of the second and third problem, another one consisting of all three problems.
In the second example, two sets of problems are suitable — the set of problems with difficulties 10 and 30 as well as the set of problems with difficulties 20 and 30.
In the third example any set consisting of one problem of difficulty 10 and one problem of difficulty 20 is suitable.
题目大意:有n个问题每个难度为ci,要选一些题出来,要求这些题的总难度不超过r且不小于l,且难度最大的和难度最小的差不能小于x,问有多少种选法
题目分析:n等于15,随便搞,我是直接深搜,简洁明了
C. Divisibility by Eight
time limit per test
2 seconds
memory limit per test
256 megabytes
You are given a non-negative integer
n, its decimal representation consists of at most
100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes
and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
Input
The single line of the input contains a non-negative integern. The representation of number
n doesn't contain any leading zeroes and its length doesn't exceed100 digits.
Output
Print "NO" (without quotes), if there is no such way to remove some digits from numbern.
Otherwise, print "YES" in the first line and the resulting number after removing digits from numbern in the second line. The printed number must be
divisible by8.
If there are multiple possible answers, you may print any of them.
Sample test(s)
Input
Output
Input
Output
Input
Output
题目大意:给个数字,问删去其中一些数字后能不能被8整除
题目分析:打了个小表,找了找规律,首先有8或0的直接可以,其次包含16,24,32,56,64,72,96之一的都可以,再其次,先找一个奇数,它后面为12,36,44,52,76,92之一的都可以,数字长度很小,暴搞
E. Brackets in Implications
time limit per test:2 seconds
memory limit per test:256 megabytes
Implication is a function of two logical arguments, its value is false if and only if the value of the first argument is true and the value of the second argument is false.
Implication is written by using character '
',
and the arguments and the result of the implication are written as '0' (false) and '1' (true). According
to the definition of the implication:
When a logical expression contains multiple implications, then when there are no brackets, it will be calculated from left to fight. For example,
.
When there are brackets, we first calculate the expression in brackets. For example,
.
For the given logical expression
determine if it is possible to place there brackets
so that the value of a logical expression is false. If it is possible, your task is to find such an arrangement of brackets.
Input
The first line contains integer
n (1 ≤ n ≤ 100 000) — the number of arguments in a logical expression.
The second line contains n numbersa1, a2, ..., an
(
), which means the values of arguments in the expression in the order they occur.
Output
Print "NO" (without the quotes), if it is impossible to place brackets in the expression so that its value was equal to 0.
Otherwise, print "YES" in the first line and the logical expression with the required arrangement of brackets in the second line.
The expression should only contain characters '0', '1', '-' (character with ASCII code 45), '>'
(character with ASCII code 62), '(' and ')'. Characters '-' and '>' can occur in an expression only
paired like that: ("->") and represent implication. The total number of logical arguments (i.e. digits '0' and '1') in the expression must
be equal to n. The order in which the digits follow in the expression from left to right must coincide witha1, a2, ..., an.
The expression should be
correct. More formally, a correct expression is determined as follows:
Expressions "0", "1" (without the quotes) are correct.
If v1,v2 are correct, thenv1->v2 is a correct expression.
If v is a correct expression, then(v) is a correct expression.
The total number of characters in the resulting expression mustn't exceed106.
If there are multiple possible answers, you are allowed to print any of them.
Sample test(s)
Input
Output
Input
Output
Input
Output
题目大意:给四个转移式子,然后给一个0/1串问是否存在某总计算方式使得最后答案为0,存在则输出任意一种合法的方式
题目分析:从给的四个式子中可以发现如果结果要为0,最后一位必须是0,现在要做的就是再最后一个0之前构造1,我们可以发现如果最后一个0的前面一个是1,那么不管这个1之前是什么最后答案都是1,因为0 ->1=1,1 ->1=1,即与前面的值无关,所以我们转过来考虑不可能的情况,考虑最后一个0的前面是0,因为要构1,又0->0=1,1->0=0也就是说这个0前面不能是1,依次类推可以得到,如果倒数第二个0的前面都是1,那必然无解,否则就有解
D也是个构造,没来及补
A. Two Substrings
time limit per test
2 seconds
memory limit per test
256 megabytes
You are given string s. Your task is to determine if the given strings contains two non-overlapping substrings "AB"
and "BA" (the substrings can go in any order).
Input
The only line of input contains a string
s of length between 1 and
105 consisting of uppercase Latin letters.
Output
Print "YES" (without the quotes), if strings contains two non-overlapping substrings "AB" and "BA",
and "NO" otherwise.
Sample test(s)
Input
ABA
Output
NO
Input
BACFAB
Output
YES
Input
AXBYBXA
Output
NO
Note
In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO".
In the second sample test there are the following occurrences of the substrings:BACFAB.
In the third sample test there is no substring "AB" nor substring "BA".
题目大意:给一个字符串,问能否找到两个不相互覆盖的子串"AB"和"BA"
题目分析:暴力扫4次,第一二次先扫"AB"后扫"BA",第三四次先扫"BA"后扫"AB",注意这组样例ABACCCAB
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int const MAX = 1e5 + 5; char s1[MAX], s2[MAX]; int main() { scanf("%s", s1); memcpy(s2, s1, sizeof(s1)); int len = strlen(s1); bool f1 = false; bool f2 = false; for(int i = 0; i < len; i++) { if(s1[i] == 'A' && s1[i + 1] == 'B') { f1 = true; s1[i] = s1[i + 1] = '*'; break; } } for(int i = 0; i < len; i++) if(s1[i] == 'B' && s1[i + 1] == 'A') f2 = true; if(f1 && f2) { printf("YES\n"); return 0; } f1 = f2 = false; for(int i = 0; i < len; i++) { if(s2[i] == 'B' && s2[i + 1] == 'A') { f1 = true; s2[i] = s2[i + 1] = '*'; break; } } for(int i = 0; i < len; i++) if(s2[i] == 'A' && s2[i + 1] == 'B') f2 = true; if(f1 && f2) { printf("YES\n"); return 0; } printf("NO\n"); }
B. Preparing Olympiad
time limit per test
2 seconds
memory limit per test
256 megabytes
You have n problems. You have estimated the difficulty of thei-th one as integer
ci. Now you want to prepare a problemset for a contest, using some of the problems you've made.
A problemset for the contest must consist of at least two problems. You think that the total difficulty of the problems of the contest must be at leastl and at most
r. Also, you think that the difference between difficulties of the easiest and the hardest of the chosen problems must be at leastx.
Find the number of ways to choose a problemset for the contest.
Input
The first line contains four integers n,l,
r,x (1 ≤ n ≤ 15,1 ≤ l ≤ r ≤ 109,1 ≤ x ≤ 106)
— the number of problems you have, the minimum and maximum value of total difficulty of the problemset and the minimum difference in difficulty between the hardest problem in the pack and the easiest one, respectively.
The second line contains n integers
c1, c2, ..., cn (1 ≤ ci ≤ 106)
— the difficulty of each problem.
Output
Print the number of ways to choose a suitable problemset for the contest.
Sample test(s)
Input
3 5 6 1 1 2 3
Output
2
Input
4 40 50 10 10 20 30 25
Output
2
Input
5 25 35 10 10 10 20 10 20
Output
6
Note
In the first example two sets are suitable, one consisting of the second and third problem, another one consisting of all three problems.
In the second example, two sets of problems are suitable — the set of problems with difficulties 10 and 30 as well as the set of problems with difficulties 20 and 30.
In the third example any set consisting of one problem of difficulty 10 and one problem of difficulty 20 is suitable.
题目大意:有n个问题每个难度为ci,要选一些题出来,要求这些题的总难度不超过r且不小于l,且难度最大的和难度最小的差不能小于x,问有多少种选法
题目分析:n等于15,随便搞,我是直接深搜,简洁明了
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int const INF = 0x3fffffff; int n, l, r, x; int c[20]; int ans; void DFS(int idx, int ma, int mi, int sum) //第几个题,当前最大,当前最小,难度总和 { if(idx == n + 1) return; if(sum <= r && sum >= l && x <= ma - mi && idx == n) ans ++; DFS(idx + 1, max(ma, c[idx]), min(mi, c[idx]), sum + c[idx]); //选第idx个 DFS(idx + 1, ma, mi, sum); //不选第idx个 return; } int main() { scanf("%d %d %d %d", &n, &l, &r, &x); for(int i = 0; i < n; i++) scanf("%d", &c[i]); ans = 0; DFS(0, 0, INF, 0); printf("%d\n", ans); }
C. Divisibility by Eight
time limit per test
2 seconds
memory limit per test
256 megabytes
You are given a non-negative integer
n, its decimal representation consists of at most
100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes
and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
Input
The single line of the input contains a non-negative integern. The representation of number
n doesn't contain any leading zeroes and its length doesn't exceed100 digits.
Output
Print "NO" (without quotes), if there is no such way to remove some digits from numbern.
Otherwise, print "YES" in the first line and the resulting number after removing digits from numbern in the second line. The printed number must be
divisible by8.
If there are multiple possible answers, you may print any of them.
Sample test(s)
Input
3454
Output
YES344
Input
10
Output
YES0
Input
111111
Output
NO
题目大意:给个数字,问删去其中一些数字后能不能被8整除
题目分析:打了个小表,找了找规律,首先有8或0的直接可以,其次包含16,24,32,56,64,72,96之一的都可以,再其次,先找一个奇数,它后面为12,36,44,52,76,92之一的都可以,数字长度很小,暴搞
#include <cstdio>
#include <cstring>
int const MAX = 105;
char s[MAX];
int len;
// 0
// 8
// 16// 24
// 32// 56// 64
// 72// 96// 112// 136// 144
// 152// 176// 192// 312// 336bool judge1(int i, char ch1, char ch2)
{
if(s[i] == ch1)
{
for(int j = i + 1; j < len; j++)
{
if(s[j] == ch2)
{
printf("YES\n%c%c\n", ch1, ch2);
return true;
}
}
}
return false;
}
bool judge2(char ch, int i, char ch1, char ch2)
{
for(int j = i + 1; j < len; j++)
{
if(s[j] == ch1)
{
for(int k = j + 1; k < len; k ++)
{
if(s[k] == ch2)
{
printf("YES\n%c%c%c\n", ch, ch1, ch2);
return true;
}
}
}
}
return false;
}
int main()
{
scanf("%s", s);
len = strlen(s);
for(int i = 0; i < len; i++)
{
if(s[i] == '0')
{
printf("YES\n0\n");
return 0;
}
if(s[i] == '8')
{
printf("YES\n8\n");
return 0;
}
if(judge1(i, '1', '6'))
return 0;
if(judge1(i, '2', '4'))
return 0;
if(judge1(i, '3', '2'))
return 0;
if(judge1(i, '5', '6'))
return 0;
if(judge1(i, '6', '4'))
return 0;
if(judge1(i, '7', '2'))
return 0;
if(judge1(i, '9', '6'))
return 0;
if((s[i] - '0') % 2 == 1 && judge2(s[i], i, '1', '2'))
return 0;
if((s[i] - '0') % 2 == 1 && judge2(s[i], i, '3', '6'))
return 0;
if((s[i] - '0') % 2 == 1 && judge2(s[i], i, '4', '4'))
return 0;
if((s[i] - '0') % 2 == 1 && judge2(s[i], i, '5', '2'))
return 0;
if((s[i] - '0') % 2 == 1 && judge2(s[i], i, '7', '6'))
return 0;
if((s[i] - '0') % 2 == 1 && judge2(s[i], i, '9', '2'))
return 0;
}
printf("NO\n");
}
E. Brackets in Implications
time limit per test:2 seconds
memory limit per test:256 megabytes
Implication is a function of two logical arguments, its value is false if and only if the value of the first argument is true and the value of the second argument is false.
Implication is written by using character '
',
and the arguments and the result of the implication are written as '0' (false) and '1' (true). According
to the definition of the implication:
When a logical expression contains multiple implications, then when there are no brackets, it will be calculated from left to fight. For example,
.
When there are brackets, we first calculate the expression in brackets. For example,
.
For the given logical expression
determine if it is possible to place there brackets
so that the value of a logical expression is false. If it is possible, your task is to find such an arrangement of brackets.
Input
The first line contains integer
n (1 ≤ n ≤ 100 000) — the number of arguments in a logical expression.
The second line contains n numbersa1, a2, ..., an
(
), which means the values of arguments in the expression in the order they occur.
Output
Print "NO" (without the quotes), if it is impossible to place brackets in the expression so that its value was equal to 0.
Otherwise, print "YES" in the first line and the logical expression with the required arrangement of brackets in the second line.
The expression should only contain characters '0', '1', '-' (character with ASCII code 45), '>'
(character with ASCII code 62), '(' and ')'. Characters '-' and '>' can occur in an expression only
paired like that: ("->") and represent implication. The total number of logical arguments (i.e. digits '0' and '1') in the expression must
be equal to n. The order in which the digits follow in the expression from left to right must coincide witha1, a2, ..., an.
The expression should be
correct. More formally, a correct expression is determined as follows:
Expressions "0", "1" (without the quotes) are correct.
If v1,v2 are correct, thenv1->v2 is a correct expression.
If v is a correct expression, then(v) is a correct expression.
The total number of characters in the resulting expression mustn't exceed106.
If there are multiple possible answers, you are allowed to print any of them.
Sample test(s)
Input
4 0 1 1 0
Output
YES(((0)->1)->(1->0))
Input
21 1
Output
NO
Input
1 0
Output
YES0
题目大意:给四个转移式子,然后给一个0/1串问是否存在某总计算方式使得最后答案为0,存在则输出任意一种合法的方式
题目分析:从给的四个式子中可以发现如果结果要为0,最后一位必须是0,现在要做的就是再最后一个0之前构造1,我们可以发现如果最后一个0的前面一个是1,那么不管这个1之前是什么最后答案都是1,因为0 ->1=1,1 ->1=1,即与前面的值无关,所以我们转过来考虑不可能的情况,考虑最后一个0的前面是0,因为要构1,又0->0=1,1->0=0也就是说这个0前面不能是1,依次类推可以得到,如果倒数第二个0的前面都是1,那必然无解,否则就有解
#include <cstdio> #include <cstring> int const MAX = 1e6 + 5; int n, a[MAX]; int main() { scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); if(n == 1) //特判一个数 { if(a[1] == 1) printf("NO\n"); else printf("YES\n0\n"); return 0; } if(a == 1) { printf("NO\n"); return 0; } bool flag = false; for(int i = 1; i <= n - 2; i++) { if(a[i] != 1) { flag = true; break; } } if(!flag && a[n - 1] == 0 && a == 0) //1111111 1 0Y 0 0N { printf("NO\n"); return 0; } printf("YES\n"); for(int i = 1; i <= n - 2; i++) printf("(%d->", a[i]); printf("%d", a[n - 1]); for(int i = 1; i <= n - 2; i++) printf(")"); printf("->0\n"); }
D也是个构造,没来及补
相关文章推荐
- OpenLayers学习笔记5——使用jQuery UI实现查询并标注(UI篇)
- Java NIO使用及原理分析 (四)
- [Swust OJ 1094]--中位数(巧用set,堆排序)
- shutdown reboot init 重启命令 关机命令 简单介绍
- HDU 5266 pog loves szh III ( LCA + SegTree||RMQ )
- qt大全(连接)
- EXCEL文件行交替背景色设置
- HDU 5265 pog loves szh II
- win8中如何设定editplus为txt默认打开程序
- 循环顺序队列2——使用标示位判断队满或空
- EXCEL内的字符比较公式
- 2015区域赛起航
- 菜鸟学Android笔记(二十六):Response数据输出
- APP Launchimage软件启动图片
- 黑马程序员_多线程间通信
- UVA 10635 Prince and Princess 最长公共子序列(nlongn)
- Rectangle Area
- mount 挂接命令及技巧光盘镜像文件、移动硬盘及U盘 简单说明
- 减少linux服务器大量TIME_WAIT
- lua 中的 bit extend 库 (可能仅对非负数有效)