Petya studies in a school and he adores Maths. His class has been studying arithmetic expressions. On the last class the teacher wrote three positive integers a, b, c on the blackboard. The task was to insert signs of operations '+' and '*', and probably brackets between the numbers so that the value of the resulting expression is as large as possible. Let's consider an example: assume that the teacher wrote numbers 1, 2 and 3 on the blackboard. Here are some ways of placing signs and brackets:
Note that you can insert operation signs only between a and b, and between b and c, that is, you cannot swap integers. For instance, in the given sample you cannot get expression (1+3)*2.
It's easy to see that the maximum value that you can obtain is 9.
Your task is: given a, b and c print the maximum value that you can get.
InputThe input contains three integers a, b and c, each on a single line (1?≤?a,?b,?c?≤?10).
OutputPrint the maximum value of the expression that you can obtain.
Sample test(s) input1 2 3output
9input
2 10 3output
60
即使題目再簡單也不要心急,心急就會wa...
#include#include #include #include #define LL long long const int MOD = 1e9+7; using namespace std; int main() { int a,b,c; while(cin>>a>>b>>c) { int d = a+b*c; int e = a*(b+c); int f = a*b*c; int g = (a+b)*c; int h = a+b+c; int Max = max(h,max(d,max(max(e,f),g))); cout<
B. Towers time limit per test 1 second memory limit per test 256 megabytes input standard input output standard outputAs you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of ai cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2).
The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time.
Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task.
InputThe first line contains two space-separated positive integers n and k (1?≤?n?≤?100, 1?≤?k?≤?1000) — the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers ai (1?≤?ai?≤?104) — the towers' initial heights.
OutputIn the first line print two space-separated non-negative integers s and m (m?≤?k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that.
In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i?≠?j). Note that in the process of performing operations the heights of some towers can become equal to zero.
If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them.
Sample test(s) input3 2 5 8 5output0 2 2 1 2 3input3 4 2 2 4output1 1 3 2input5 3 8 3 2 6 3output3 3 1 3 1 2 1 3NoteIn the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
以後做cf多測幾組數據再交,小數據太水了....這題數據范圍小,暴力+排序,每次把最大的移給最小的1個,如果發現移玩以後最大的小於最小的則結束#include#include #include #include #include #define LL long long const int MOD = 1e9+7; using namespace std; struct node { int x; int id; }d[200]; struct s { int a; int b; }p[10005]; bool cmp(node a, node b) { return a.x < b.x; } int main() { #ifdef xxz freopen("in","r",stdin); #endif // xxz int n,k; while(cin>>n>>k) { for(int i = 1; i <= n; i++) { cin>>d[i].x; d[i].id = i; } int cha = 0, cent = 0; sort(d,d+n+1,cmp); int m = 0; for(int i = 0; i < k; i++) { if(d[1].x+1 > d[n].x-1) { cha = d[n].x - d[1].x; break; } d[1] .x+= 1; d[n] .x-= 1; cent++; p[m].a = d[n].id; p[m++].b = d[1].id; sort(d,d+n+1,cmp); cha = d[n] .x- d[1].x; } cout< C. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.
According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi?ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai.
Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date.
InputThe first line contains a single positive integer n (1?≤?n?≤?5000) — the number of exams Valera will take.
Each of the next n lines contains two positive space-separated integers ai and bi (1?≤?bi?ai?≤?109) — the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly.
OutputPrint a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.
Sample test(s) input3 5 2 3 1 4 2output2input3 6 1 5 2 4 3output6NoteIn the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5.
In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
這題題意讀懂了就很容易了,Valera要參加n場考試,每場考試i,考試規定的時間是ai,但Valera通過老師可以提前到bi(bi
顯然先對ai為第一關鍵字,bi為第二關鍵字從小到大排序,然後從小到大掃描,
比如
4 3
5 2
6 1
那麼除了第一個取3,其它兩個都取5,6 如果只排序第一個的話 4 3 4 4 4 1 其實只要1天即可,這樣的話需要4題...不行#include#include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef pair P; P p[5010]; bool cmp(P a, P b) { if(a.first == b.first) return a.second < b.second; return a.first < b.first; } int main() { #ifdef xxz freopen("in","r",stdin); #endif // xxz int n; while(cin>>n) { for(int i = 0; i >p[i].first>>p[i].second; } sort(p,p+n,cmp); int ans = 0; for(int i =0; i < n; i++) { if(p[i].second >= ans) ans = max(ans,p[i].second); else ans = max(ans,p[i].first); } cout<
D. Long Jumps time limit per test 1 second memory limit per test 256 megabytes input standard input output standard outputValery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler!
However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has nmarks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1,?a2,?...,?an, where aidenotes the distance of the i-th mark from the origin (a1?=?0, an?=?l).
Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1?≤?i?≤?j?≤?n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj?-?ai?=?d).
Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x?y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y.
Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler.
InputThe first line contains four positive space-separated integers n, l, x, y (2?≤?n?≤?105, 2?≤?l?≤?109, 1?≤?x?y?≤?l) — the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly.
The second line contains a sequence of n integers a1,?a2,?...,?an (0?=?a1?a2?...?an?=?l), where ai shows the distance from the i-th mark to the origin.
OutputIn the first line print a single non-negative integer v — the minimum number of marks that you need to add on the ruler.
In the second line print v space-separated integers p1,?p2,?...,?pv (0?≤?pi?≤?l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them.
Sample test(s) input3 250 185 230 0 185 250output1 230input4 250 185 230 0 20 185 250output0input2 300 185 230 0 300output2 185 230NoteIn the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark.
In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks.
In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills.
一開始我想使用暴力解決,也就是把尺子點間距離全部算出來,但是需要o(n^2),不行,後來仔細想想,這題目答案只會由三種0,1,2,
題目大意:
給出一把尺子,總長度為l,其中有n個標記,其中第一個標記為0, 第n個標記為l。要求量出x和y,如若不能量出,可以在上面增加標記。要求增加的標記最少。
由題意可知,標記增加量為:0, 1, 2;
0個為x和y都可以在尺子上找到,即a[i]+x和a[i]+y均可在尺子上找到,我們可以采用二分搜索。復雜度為O(nlogn)(可以用STL自帶的)
1個為可以在上面標記一個點,使得x和y都可以在尺子上找到,即a[i]+x+y , a[i]+x-y, a[i]-x+y, a[i]+x, a[i]+y,其中一個可以在尺子上找到就可以了。
2個直接標記x,y就夠了。
a[i] - a[j] = y-x;
a[i] - a[j = x-y;(存在這種差的話也可以加一個)
#include#include #include #include using namespace std; const int maxn = 1e5+10; int a[maxn]; int n,l,x,y; bool check_0() { int fx,fy; fx = fy = 0 ; for(int i = 0; i < n; i++) { if(binary_search(a,a+n,a[i]+x) && a[i]+x >=0 && a[i]+x <= l) { fx = 1; } if(binary_search(a,a+n,a[i]+y) && a[i]+y >= 0 && a[i] + y <= l) { fy = 1; } } if(fx && fy) return true; else return false; } bool check_1() { for(int i = 0; i < n; i++) { if(binary_search(a,a+n,a[i]+x) && y >=0 && y <= l) { cout<<1< = 0 && x <= l) { cout<<1< = 0 && a[i] -x <= l) { cout<<1< n>>l>>x>>y) { for(int i = 0; i < n; i++) { cin>>a[i]; } if(check_0()) { cout<<0<