CoderForces Round526 (A~E)题解
A. The Fair Nut and Elevator
The Fair Nut lives in nn story house. aiai people live on the ii-th floor of the house. Every person uses elevator twice a day: to get from the floor where he/she lives to the ground (first) floor and to get from the first floor to the floor where he/she lives, when he/she comes back home in the evening.
It was decided that elevator, when it is not used, will stay on the xx-th floor, but xx hasn't been chosen yet. When a person needs to get from floor aa to floor bb, elevator follows the simple algorithm:
- Moves from the xx-th floor (initially it stays on the xx-th floor) to the aa-th and takes the passenger.
- Moves from the aa-th floor to the bb-th floor and lets out the passenger (if aa equals bb, elevator just opens and closes the doors, but stillcomes to the floor from the xx-th floor).
- Moves from the bb-th floor back to the xx-th.
Your task is to help Nut to find the minimum number of electricity units, that it would be enough for one day, by choosing an optimal the xx-th floor. Don't forget than elevator initially stays on the xx-th floor.
The first line contains one integer nn (1≤n≤1001≤n≤100) — the number of floors.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1000≤ai≤100) — the number of people on each floor.
In a single line, print the answer to the problem — the minimum number of electricity units.
3
0 2 1
16
2
1 1
In the first example, the answer can be achieved by choosing the second floor as the xx-th floor. Each person from the second floor (there are two of them) would spend 44 units of electricity per day (22 to get down and 22 to get up), and one person from the third would spend 88units of electricity per day (44 to get down and 44 to get up). 4⋅2+8⋅1=164⋅2+8⋅1=16.
In the second example, the answer can be achieved by choosing the first floor as the xx-th floor.
题解:就是一部电梯固定在某一位置,然后给你一些人,他们每天会上下楼,从a层到b层会花费|a-b|的费用,电梯运行为:从固定点x到a接乘客,送到b,然后再回到固定点x;
求最小花费;
枚举即可;
参考代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int INF=0x3f3f3f3f; 5 int a[105]; 6 int main() 7 { 8 int n,s,Mix=INF; 9 cin>>n; 10 for(int i=0;i<n;i++) cin>>a[i]; 11 for(int i=0;i<n;i++) 12 { 13 s=0; 14 for(int j=1;j<n;j++) s+=(abs(i-j)*2+j*2+i*2)*a[j]; 15 Mix=min(Mix,s); 16 } 17 cout<<Mix<<endl; 18 return 0; 19 }
B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him nn kegs of kvass. There are vivi liters of kvass in the ii-th keg. Each keg has a lever. You can pour your glass by exactly 11 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by ss liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by ss liters of kvass.
The first line contains two integers nn and ss (1≤n≤1031≤n≤103, 1≤s≤10121≤s≤1012) — the number of kegs and glass volume.
The second line contains nn integers v1,v2,…,vnv1,v2,…,vn (1≤vi≤1091≤vi≤109) — the volume of ii-th keg.
If the Fair Nut cannot pour his glass by ss liters of kvass, print −1−1. Otherwise, print a single integer — how much kvass in the least keg can be.
3 3
4 3 5
3
3 4
5 3 4
2
3 7
1 2 3
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is −1.
题解:
题意:就是给你N个酒桶,每个酒桶有不同的酒,然后给你一个杯子k,问能不能倒满这个杯子,不能输出-1,能的话输出剩余酒桶中最多的为多少。
题解:一开始暴力,结果一直tle,后来发现是二分,时间不够了,实际上二分即可解决,下来又想想,发现一个巧妙思路 很简单就过了。
参考代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,s,sum,Min,a[1010]; 5 bool Judge(ll x) {return sum-n*x>=s? 1:0;} 6 int main() 7 { 8 scanf("%lld%lld",&n,&s);Min=1000000000010; 9 for(int i=1;i<=n;++i) { scanf("%lld",a+i);sum+=a[i];Min=min(Min,a[i]);} 10 ll l=0,r=Min,ans=-1; 11 if(sum<s) {puts("-1");return 0;} 12 while(l<=r) 13 { 14 ll mid=l+r>>1; 15 if(Judge(mid)) l=mid+1,ans=max(ans,mid); 16 else r=mid-1; 17 } 18 printf("%lld\n",ans); 19 return 0; 20 }