Codeforces Round #306 (Div. 2)

来源:互联网 发布:sql bigdecimal 编辑:程序博客网 时间:2024/05/01 10:04

A. Two Substrings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given string s. Your task is to determine if the given string s 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 string s 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".

------------

就是简单的字符串处理。

#include <cstdio>#include <cstring>char ____[100100];int main(){int _;int __ = 0;int ___ = 0;gets(____);_ = strlen(____);for(int i = 1; i < _; i++) {if(____[i] == 'B'&&____[i - 1] == 'A') {if(___&&___ < i - 1) {printf("YES\n");return 0;} if(!__)__ = i;}if(____[i] == 'A'&&____[i - 1] == 'B') {if(__&&__ < i - 1) {printf("YES\n");return 0;}if(!___)___ = i;}}printf("NO\n");return 0;}
----------
B. Preparing Olympiad
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have n problems. You have estimated the difficulty of the i-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 least l 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 least x.

Find the number of ways to choose a problemset for the contest.

Input

The first line contains four integers nlrx (1 ≤ n ≤ 151 ≤ l ≤ r ≤ 1091 ≤ 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 11 2 3
output
2
input
4 40 50 1010 20 30 25
output
2
input
5 25 35 1010 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<=15(o^^o)♪

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int c[15];int n;int l, r, x;bool solve(int);int main(){int sum = 0;scanf("%d%d%d%d", &n, &l, &r, &x);for(int i = 0; i < n; i++)scanf("%d", c + i);sort(c, c + n);for(int i = 1; i <= (1 << n) - 1; i++)if(solve(i))sum++;printf("%d\n", sum);return 0;}bool solve(int a){int finger = 1;int min = 0, max = 0, sum = 0;for(int i = 0; i < n; i++) {if(finger&a) {if(min == 0)min = c[i];if(max < c[i])max = c[i];sum += c[i];}finger <<= 1;}if(sum >= l&&sum <= r&&max - min >= x)return true;elsereturn false;}
----------
C. Divisibility by Eight
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.

Output

Print "NO" (without quotes), if there is no such way to remove some digits from number n.

Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.

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
----------

暴力求解并不会超时。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;char buf[111];int toInt(char, char, char);int main(){int length;gets(buf);length = strlen(buf);if(length == 1) {if(buf[0] == '0' || buf[0] == '8') {printf("YES\n%c", *buf);return 0;} else {printf("NO\n");return 0;}}for(int i = 0; i < length - 2; i++) {for(int j = i + 1; j < length - 1; j++) {for(int k = j + 1; k < length; k++) {if(toInt(buf[i], buf[j], buf[k]) % 8 == 0) {printf("YES\n%d\n", toInt(buf[i], buf[j], buf[k]));return 0;}if(toInt('0', buf[j], buf[k]) % 8 == 0) {printf("YES\n%d\n", toInt('0', buf[j], buf[k]));return 0;}if(toInt('0', buf[i], buf[k]) % 8 == 0) {printf("YES\n%d\n", toInt('0', buf[i], buf[k]));return 0;}if(toInt('0', buf[i], buf[j]) % 8 == 0) {printf("YES\n%d\n", toInt('0', buf[i], buf[j]));return 0;}}}}if(toInt('0', buf[0], buf[1]) % 8 == 0) {printf("YES\n%d\n", toInt('0', buf[0], buf[1]));return 0;}if(buf[0] == '0' || buf[0] == '8') {printf("YES\n%c", *buf);return 0;}if(buf[1] == '0' || buf[1] == '8') {printf("YES\n%c", buf[1]);return 0;}printf("NO\n");return 0;}int toInt(char a, char b, char c){return (a - '0') * 100 + (b - '0') * 10 + c - '0';}

----------

D. Regular Bridge
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

An undirected graph is called k-regular, if the degrees of all its vertices are equal k. An edge of a connected graph is called a bridge, if after removing it the graph is being split into two connected components.

Build a connected undirected k-regular graph containing at least one bridge, or else state that such graph doesn't exist.

Input

The single line of the input contains integer k (1 ≤ k ≤ 100) — the required degree of the vertices of the regular graph.

Output

Print "NO" (without quotes), if such graph doesn't exist.

Otherwise, print "YES" in the first line and the description of any suitable graph in the next lines.

The description of the made graph must start with numbers n and m — the number of vertices and edges respectively.

Each of the next m lines must contain two integers, a and b (1 ≤ a, b ≤ na ≠ b), that mean that there is an edge connecting the vertices a and b. A graph shouldn't contain multiple edges and edges that lead from a vertex to itself. A graph must be connected, the degrees of all vertices of the graph must be equal k. At least one edge of the graph must be a bridge. You can print the edges of the graph in any order. You can print the ends of each edge in any order.

The constructed graph must contain at most 106 vertices and 106 edges (it is guaranteed that if at least one graph that meets the requirements exists, then there also exists the graph with at most 106 vertices and at most 106 edges).

Sample test(s)
input
1
output
YES2 11 2
Note

In the sample from the statement there is a suitable graph consisting of two vertices, connected by a single edge.

----------

好复杂啊,不说了。

import java.io.InputStream;import java.io.InputStreamReader;import java.io.BufferedReader;import java.io.OutputStream;import java.io.PrintWriter;import java.io.IOException;import java.util.StringTokenizer;/** * Built using CHelper plug-in * Actual solution is at the top */public class Main {public static void main(String[] args) {InputStream inputStream = System.in;OutputStream outputStream = System.out;InputReader in = new InputReader(inputStream);PrintWriter out = new PrintWriter(outputStream);TaskD solver = new TaskD();solver.solve(1, in, out);out.close();}}class TaskD {    private int k;    public void solve(int testNumber, InputReader in, PrintWriter out) {        k = in.nextInt();        if (k % 2 == 0) {            out.print("NO");            return ;        }        out.print("YES\n");        if (k == 1)            out.println("2 1\n1 2");        else {            int inc = 2;            int n = 4 * (k - 1) + 2;            int m = (n * k) / 2;            out.printf("%d %d\n", n, m);            out.printf("1 2\n");            for(int i = 1; i <= k - 1; ++i)                out.printf("%d %d\n", 1, (i + inc));            for(int i = 1; i <= k - 1; ++i)                for(int j = 1; j <= k - 1; ++j)                    out.printf("%d %d\n", i + inc, j + inc + k - 1);            for(int i = 1; i <= k - 1; i += 2)                out.printf("%d %d\n", i + inc + k - 1, i + inc + k);            inc += 2 * (k - 1);            for(int i = 1; i <= k - 1; ++i)                out.printf("%d %d\n", 2, (i + inc));            for(int i = 1; i <= k - 1; ++i)                for(int j = 1; j <= k - 1; ++j)                    out.printf("%d %d\n", i + inc, j + inc + k - 1);            for(int i = 1; i <= k - 1; i += 2)                out.printf("%d %d\n", i + inc + k - 1, i + inc + k);        }    }}class InputReader {    public BufferedReader reader;    public StringTokenizer tokenizer;    public InputReader(InputStream stream) {        reader = new BufferedReader(new InputStreamReader(stream));        tokenizer = null;    }    public String next() {        while (tokenizer == null || !tokenizer.hasMoreTokens()) {            try {                tokenizer = new StringTokenizer(reader.readLine());            } catch (IOException e) {                throw new RuntimeException(e);            }        }        return tokenizer.nextToken();    }    public int nextInt() {        return Integer.parseInt(next());    }}
----------

E. Brackets in Implications
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 numbers a1, 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 with a1, 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 v1v2 are correct, then v1->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 exceed 106.

If there are multiple possible answers, you are allowed to print any of them.

Sample test(s)
input
40 1 1 0
output
YES(((0)->1)->(1->0))
input
21 1
output
NO
input
10
output
YES0
----------

从后往前,分析各种情况。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int buf[100100];int bra[100100];int main(){int n;int finger;int flag = 0;int braCnt = 0;int lookFor = 1;scanf("%d", &n);finger = n - 1;for(int i = 0; i < n; i++) {scanf("%d", buf + i);}if(buf[finger]) {printf("NO\n");return 0;}while(--finger >= 0) {if(buf[finger] == lookFor) {if(lookFor == 0) {bra[finger] = 1;braCnt++;}printf("YES\n");flag = 1;break;} else if(lookFor == 1) {lookFor = 0;} else {bra[finger] = 1;braCnt++;}}if(!flag&&n>1) {printf("NO\n");} else {if(n == 1)printf("YES\n");for(int i = 0; i < n - 1; i++) {if(bra[i])putchar('(');putchar(buf[i] + '0');if(i == n - 2)while(braCnt--)putchar(')');printf("->");}printf("%d\n", buf[n - 1]);}return 0;}





0 0