A - A
Each student eagerly awaits the day he would pass the exams successfully. Thus, Vasya was ready to celebrate, but, alas, he didn't pass it. However, many of Vasya's fellow students from the same group were more successful and celebrated after the exam.
Some of them celebrated in the BugDonalds restaurant, some of them — in the BeaverKing restaurant, the most successful ones were fast enough to celebrate in both of restaurants. Students which didn't pass the exam didn't celebrate in any of those restaurants and elected to stay home to prepare for their reexamination. However, this quickly bored Vasya and he started checking celebration photos on the Kilogramm. He found out that, in total, BugDonalds was visited by AA students, BeaverKing — by BB students and CC students visited both restaurants. Vasya also knows that there are NN students in his group.
Based on this info, Vasya wants to determine either if his data contradicts itself or, if it doesn't, how many students in his group didn't pass the exam. Can you help him so he won't waste his valuable preparation time?
Input
The first line contains four integers — AA, BB, CC and NN (0≤A,B,C,N≤1000≤A,B,C,N≤100).
Output
If a distribution of NN students exists in which AA students visited BugDonalds, BB — BeaverKing, CC — both of the restaurants and at least one student is left home (it is known that Vasya didn't pass the exam and stayed at home), output one integer — amount of students (including Vasya) who did not pass the exam.
If such a distribution does not exist and Vasya made a mistake while determining the numbers AA, BB, CC or NN (as in samples 2 and 3), output −1−1.
Examples
10 10 5 20
5
2 2 0 4
-1
2 2 2 1
-1
Note
The first sample describes following situation: 55 only visited BugDonalds, 55 students only visited BeaverKing, 55 visited both of them and 55 students (including Vasya) didn't pass the exam.
In the second sample 22 students only visited BugDonalds and 22 only visited BeaverKing, but that means all 44 students in group passed the exam which contradicts the fact that Vasya didn't pass meaning that this situation is impossible.
The third sample describes a situation where 22 students visited BugDonalds but the group has only 11 which makes it clearly impossible.
解题思路:给出a,b,c,n。表示去BugDonalds的有a个人,去BeaverKing的有b个人,两者都去的有c人,一共有n人,求有多少人没两者都没去,至少有1人没去,则答案为0或者是不合法的话,输出-1.
则根据题意,只需要判断c一定是大于a和b的,且若n-(a+b-c)<1也一定是不合法的即可。
代码:
#include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<map> using namespace std; int main(){ int a,b,c,i,n,d; cin>>a>>b>>c>>n; d=n-(a+b-c); if(c>a){ cout<<"-1"<<endl; } else if(c>b){ cout<<"-1"<<endl; } else if(d<1){ cout<<"-1"<<endl; } else{ cout<<d<<endl; } return
B - B
Translator's note: in Russia's most widespread grading system, there are four grades: 5, 4, 3, 2, the higher the better, roughly corresponding to A, B, C and F respectively in American grading system.
The term is coming to an end and students start thinking about their grades. Today, a professor told his students that the grades for his course would be given out automatically — he would calculate the simple average (arithmetic mean) of all grades given out for lab works this term and round to the nearest integer. The rounding would be done in favour of the student — 4.54.5 would be rounded up to 55 (as in example 3), but 4.44.4 would be rounded down to 44.
This does not bode well for Vasya who didn't think those lab works would influence anything, so he may receive a grade worse than 55 (maybe even the dreaded 22). However, the professor allowed him to redo some of his works of Vasya's choosing to increase his average grade. Vasya wants to redo as as few lab works as possible in order to get 55 for the course. Of course, Vasya will get 55 for the lab works he chooses to redo.
Help Vasya — calculate the minimum amount of lab works Vasya has to redo.
Input
The first line contains a single integer nn — the number of Vasya's grades (1≤n≤1001≤n≤100).
The second line contains nn integers from 22 to 55 — Vasya's grades for his lab works.
Output
Output a single integer — the minimum amount of lab works that Vasya has to redo. It can be shown that Vasya can always redo enough lab works to get a 55.
Examples
3
4 4 4
2
4
5 4 5 5
0
4
5 3 3 5
1
Note
In the first sample, it is enough to redo two lab works to make two 44s into 55s.
In the second sample, Vasya's average is already 4.754.75 so he doesn't have to redo anything to get a 55.
In the second sample Vasya has to redo one lab work to get rid of one of the 33s, that will make the average exactly 4.54.5 so the final grade would be 55.
解题思路:给出n个数 ai (2<=ai<=5),将其中任意一个不为5的数改成5.求出这些数的平均值(四舍五入),问最少改动几个数,可以使得平均值为5.
由题意得只需使平均数为4.5即可,则排序后开始从最小的数变成5,判断平均数是否大于等于4.5即可。
代码:
#include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<map> using namespace std; int main(){ int n,i,a[1000],sum=0,k=0; cin>>n; for(i=0;i<n;i++){ cin>>a[i]; sum+=a[i]; } if(sum*1.0/n>=4.5){ cout<<"0"<<endl; } else{ sort(a,a+n); for(i=0;i<n;i++){ sum=sum-a[i]+5; k++; if(sum*1.0/n>=4.5){ cout<<k<<endl; break; } a[i]=5; } } return 0; }
C - C
After passing a test, Vasya got himself a box of nn candies. He decided to eat an equal amount of candies each morning until there are no more candies. However, Petya also noticed the box and decided to get some candies for himself.
This means the process of eating candies is the following: in the beginning Vasya chooses a single integer kk, same for all days. After that, in the morning he eats kk candies from the box (if there are less than kk candies in the box, he eats them all), then in the evening Petya eats 10%10% of the candies remaining in the box. If there are still candies left in the box, the process repeats — next day Vasya eats kk candies again, and Petya — 10%10% of the candies left in a box, and so on.
If the amount of candies in the box is not divisible by 1010, Petya rounds the amount he takes from the box down. For example, if there were 9797 candies in the box, Petya would eat only 99 of them. In particular, if there are less than 1010 candies in a box, Petya won't eat any at all.
Your task is to find out the minimal amount of kk that can be chosen by Vasya so that he would eat at least half of the nn candies he initially got. Note that the number kk must be integer.
Input
The first line contains a single integer nn (1≤n≤10181≤n≤1018) — the initial amount of candies in the box.
Output
Output a single integer — the minimal amount of kk that would allow Vasya to eat at least half of candies he got.
Example
68
3
Note
In the sample, the amount of candies, with k=3k=3, would change in the following way (Vasya eats first):
68→65→59→56→51→48→44→41→37→34→31→28→26→23→21→18→17→14→13→10→9→6→6→3→3→068→65→59→56→51→48→44→41→37→34→31→28→26→23→21→18→17→14→13→10→9→6→6→3→3→0.
In total, Vasya would eat 3939 candies, while Petya — 2929.
解题思路:
代码:
Vasya有n个糖果,在开始的时候 Vasya 选择了一个整数k,表示他每天会吃k个糖果,Petya想偷吃一部分糖果,他每天会吃当前数量的10%(下取整)的糖果
输出最小的k,使得Vasya至少吃掉一半的糖果。
注意:
若Vasya吃糖果时数量不满k,则Vasya会全吃掉。
若Petya吃糖果时数量不满10个,则Petya不会吃糖果。
用二分查找,二分需要满足两种结构:第一,解可能存在于一个固定的区间;第二,这个区间必须具有单调性。
这里的二分区间就是1~n,二分转移的条件:只要按照这个值拿拿拿,拿到的东西满足sum>n/2,那么就是合法的,区间左移,否则右移。
代码:
#include<bits/stdc++.h> using namespace std; //typedef long long ll; long long n; long long check( long long x ) { long long now, sum; now = n, sum = 0; while( now ) { if( now <= x ) { sum += now; break; }//如果剩下的糖小于k时 now -= x; sum += x; now -= now / 10; } return sum;//sum是指Vasya吃的糖的数量。 } int main() { while( cin >> n ) { long long l = 1, r = n, mid; while( l < r ) { mid = ( l + r ) / 2; if( check(mid) >= ( n + 1 ) / 2 )//取中间的mid的值为k是vasya吃的糖比Petya的多时 { r = mid; } else { l = mid + 1; } } cout << l << endl; } return 0; }