ProblemA: A. Protocol. BizontheChampionhasrecentlygotapresent-
Problem A:. rewards time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Bizon the Champion is called the Champion for a reason. bizon the Champion has recently got a present-a n
Problem:
A. Rewards
Time limit per test
1 second
Memory limit per test
256 megabytes
Input
Standard input
Output
Standard output
Bizon the Champion is called the Champion for a reason.
Bizon the Champion has recently got a present-a new glass cupboardNShelves and he decided to put all his presents there. All the presents can be pided into two types: medals and cups. Bizon the Champion hasA1 first prize cups,A2 second prize cups andA3 third prize cups. Besides, he hasB1 first prize medals,B2 second prize medals andB3 third prize medals.
Naturally, the rewards in the cupboard must look good, that's why Bizon the Champion decided to follow the rules:
- Any shelf cannot contain both cups and medals at the same time;
- No shelf can contain more than five cups;
- No shelf can have more than ten medals.
Help Bizon the Champion find out if we can put all the rewards so that all the conditions are fulfilled.
Input
The first line contains integersA1,A2 andA3 (0? ≤?A1 ,?A2 ,?A3? ≤? 100). The second line contains integersB1,B2 andB3 (0? ≤?B1 ,?B2 ,?B3? ≤? 100). The third line contains integerN(1? ≤?N? ≤? 100 ).
The numbers in the lines are separated by single spaces.
Output
Print "YES" (without the quotes) if all the rewards can be put on the shelves in the described manner. Otherwise, print "NO" (without the quotes ).
Sample test (s)
Input
1 1 11 1 14
Output
YES
Input
1 1 32 3 42
Output
YES
Input
1 0 01 0 01
Output
NO
Portal: Click to open the link
Solution:
Find the minimum number of racks required for placing all trophies and medals num, and compare them with n.
Code:
#include
#include
int main(){ int a1, a2, a3, b1, b2, b3, n; scanf("%d%d%d%d%d%d%d", &a1, &a2, &a3, &b1, &b2, &b3, &n); int num = ceil((a1 + a2 + a3) * 1.0 / 5) + ceil((b1 + b2 + b3) * 1.0 / 10); printf("%s\n", num <= n ? "YES" : "NO"); return 0;}
Problem B:
B. Suffix Structures
Time limit per test
1 second
Memory limit per test
256 megabytes
Input
Standard input
Output
Standard output
Bizon the Champion isn't just a bison. He also is a favorite of the "Bizons" team.
At a competition the "Bizons" got the following problem: "You are given two distinct words (strings of English letters ),SAndT. You need to transform wordSInto wordT". The task looked simple to the guys because they know the suffix data structures well. bizon Senior loves suffix automatic. by applying it once to a string, he can remove from this string any single character. bizon Middle knows suffix array well. by applying it once to a string, he can swap any two characters of this string. the guys do not know anything about the suffix tree, but it can help them do much more.
Bizon the Champion wonders whether the "Bizons" can solve the problem. perhaps, the solution do not require both data structures. find out whether the guys can solve the problem and if they can, how do they do it? Can they solve it either only with use of suffix automaton or only with use of suffix array or they need both structures? Note that any structure may be used an unlimited number of times, the structures may be used in any order.
Input
The first line contains a non-empty wordS. The second line contains a non-empty wordT. WordsSAndTAre different. Each word consists only of lowercase English letters. Each word contains at most 100 letters.
Output
In the single line print the answer to the problem. Print "need tree" (without the quotes) if wordSCannot be transformed into wordTEven with use of both suffix array and suffix automation. print "automaton" (without the quotes) if you need only the suffix automation to solve the problem. print "array" (without the quotes) if you need only the suffix array to solve the problem. print "both" (without the quotes), if you need both data structures to solve the problem.
It's guaranteed that if you can solve the problem only with use of suffix array, then it is impossible to solve it only with use of suffix automation. This is also true for suffix automation.
Sample test (s)
Input
automatontomat
Output
automaton
Input
arrayarary
Output
array
Input
bothhot
Output
both
Input
needtree
Output
need tree
Portal: Click to open the link
Solution:
If string A contains all the letters of string B, and these letters are arranged in the same order as string A and string B, output "automaton". Otherwise, if string A contains all the letters of string B, we will discuss it in this case. If the length of A and B is equal, the output is "array". If A is longer than B, output "both"; otherwise, output "need tree ".
Code:
#include
#include
#include
using namespace std;const int MAXN = 105;int vis[MAXN];bool cmpa(string stra, string strb){ memset(vis, 0, sizeof(vis)); int lena = stra.length(); int lenb = strb.length(); int k = 0; for(int i = 0; i < lenb; i++) { bool flag = true; for(int j = k; j < lena; j++) { if(!vis[j] && strb[i] == stra[j]) { vis[j] = 1; k = j + 1; flag = false; break; } } if(flag) return false; } return true;}bool cmpb(string stra, string strb){ memset(vis, 0, sizeof(vis)); int lena = stra.length(); int lenb = strb.length(); for(int i = 0; i < lenb; i++) { bool flag = true; for(int j = 0; j < lena; j++) { if(!vis[j] && strb[i] == stra[j]) { vis[j] = 1; flag = false; break; } } if(flag) return false; } return true;}int main(){ string stra, strb; cin >> stra >> strb; int lena = stra.length(); int lenb = strb.length(); if(cmpa(stra, strb)) { cout << "automaton" << endl; } else if(cmpb(stra, strb)) { if(lena == lenb) { cout << "array" << endl; } else if(lena > lenb) { cout << "both" << endl; } else { cout << "need tree" << endl; } } else { cout << "need tree" << endl; } return 0;}
Problem C:
C. Painting Fence
Time limit per test
1 second
Memory limit per test
512 megabytes
Input
Standard input
Output
Standard output
Bizon the Champion isn' t just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is representedNVertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one,I-Th plank has the width of 1 meter and the heightAIMeters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. he can make vertical and horizontal strokes with the brush. during a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding ). what minimum number of strokes shocould Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
Input
The first line contains integerN(1? ≤?N? ≤? 5000)-the number of fence planks. The second line containsNSpace-separated integersA1 ,?A2 ,?...,?AN(1? ≤?AI? ≤? 109 ).
Output
Print a single integer-the minimum number of strokes needed to paint the whole fence.
Sample test (s)
Input
52 2 1 2 1
Output
3
Input
22 2
Output
2
Input
15
Output
1
Portal: Click to open the link
Solution:
Recursive and greedy. Paint the fence. We can paint the fence horizontally (only the continuous part can be painted) or vertically. The minimum number of paint times in the horizontal direction is min (a1, a2, a3 ,..., an-1); the minimum number of times to brush in the vertical direction is the length of the continuous fence n. After the above method is used, a fence with a height of 0 will inevitably be generated (all of which have been flushed). We divide the fence into the left half, and the right half, the two parts are a continuous fence, and so on.
A wrong idea: Find the maximum value h of the height of all the fences and the length len of the longest continuous fence each time you brush them. Take max (h, len) for the brush ), the algorithm complexity is O (n2 ). In this case, the fence may become dispersed, resulting in more paint times.
Counterexample:
3
1 10 1
Error code:
#include
#include using namespace std; const int MAXN = 5010; int n, ans = 0, a[MAXN]; int main() { scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &a[i]); } while(true) { int maxv = *max_element(a, a + n); int maxh = -1, tmp = 0, first = 1; int s = 0, t = 0, ts = 0, tt = 0; for(int i = 0; i < n; i++) { if(a[i]) { if(first) { ts = i; first = 0; } tt = i; tmp++; if(i == n - 1) { if(tmp > maxh) { maxh = tmp; s = ts; t = tt; } } } else { if(tmp > maxh) { maxh = tmp; s = ts; t = tt; } tmp = 0; first = 1; } } if(maxv > maxh) { *max_element(a, a + n) = 0; } else { for(int i = s; i <= t; i++) { a[i]--; } } ans++; if(0 == *max_element(a, a + n)) break; } printf("%d\n", ans); return 0; }
Code:
#include
#include using namespace std;const int MAXN = 5010;int n, a[MAXN];int solve(int l, int r){ if(l > r) return 0; int minh = *min_element(a + l, a + r + 1); int ret = r - l + 1, tot = minh; if(ret < minh) { for(int i = l; i <= r; i++) a[i] = 0; return ret; } else { for(int i = l; i <= r; i++) a[i] -= minh; int t = min_element(a + l, a + r + 1) - a; tot += solve(l, t - 1) + solve(t + 1, r); } return min(ret, tot);}int main(){ scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d", &a[i]); printf("%d\n", solve(0, n - 1)); return 0;}
Problem D:
D. Multiplication Table
Time limit per test
1 second
Memory limit per test
256 megabytes
Input
Standard input
Output
Standard output
Bizon the Champion isn't just charming, he also is very smart.
While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion paintedN? ×?MMultiplication table, where the element on the interp ofI-Th row andJ-Th column equalsI·J(The rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table isK-Th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?
Consider the given multiplication table. If you write out allN·MNumbers from the table in the non-decreasing order, thenK-Th number you write out is calledK-Th largest number.
Input
The single line contains integersN,MAndK(1? ≤?N,?M? ≤? 5 · 105; 1? ≤?K? ≤?N·M).
Output
PrintK-Th largest number inN? ×?MMultiplication table.
Sample test (s)
Input
2 2 2
Output
2
Input
2 3 4
Output
3
Input
1 10 5
Output
5
Portal: Click to open the link
Solution:
Binary. The requirement is the k number in the n * m multiplication table. We can perform a binary search for this number ans, with the range [1, n * m]. for every possible ans, we can find the number of smaller numbers than him: sum + = min (mid-1)/I, m); (I =, 3 ,.., n), record the largest mid smaller than k, that is, the ans we want.
Code:
#include
inline long long min(long long a, int b){ if(a < b) return a; return b;}int main(){ int n, m; long long k, ans, l, r, sum, mid; scanf("%d%d%I64d", &n, &m, &k); l = 1, r = 1ll * n * m;//r = (long long)n * m; while(l <= r) { mid = (l + r) >> 1; sum = 0; for(int i = 1; i <= n; i++) sum += min((mid - 1) / i, m); if(sum < k) l = mid + 1, ans = mid; else r = mid - 1; } printf("%I64d\n", ans); return 0;}