Codeforces Round #421 (Div. 2)
A. Mister B and Book Reading
Mister B once received a gift: it was a book about aliens, which he started read immediately. This book had c pages.
At first day Mister B read v0 pages, but after that he started to speed up. Every day, starting from the second, he read a pages more than on the previous day (at first day he read v0 pages, at second — v0 + a pages, at third — v0 + 2a pages, and so on). But Mister B is just a human, so he physically wasn’t able to read more than v1 pages per day.
Also, to refresh his memory, every day, starting from the second, Mister B had to reread last l pages he read on the previous day. Mister B finished the book when he read the last page for the first time.
Help Mister B to calculate how many days he needed to finish the book.
第一天看v0页,每天比前一天多a页但不会超过v1页,每天会往前看最后l页,问c页的书几天能看完.
模拟题:
#include <bits/stdc++.h>
using namespace std;
int main(){
int c,v0,v1,a,l;
scanf("%d%d%d%d%d",&c,&v0,&v1,&a,&l);
int nowr = 0,nowv = v0;
int ans = 0;
while(nowr<c){
nowr+=v0;
ans++;
if(nowr>=c){
cout<<ans<<endl;
return 0;
}
v0 = min(v0+a,v1);
nowr = max(0,nowr-l);
}
return 0;
}
B. Mister B and Angle in Polygon
On one quiet day all of sudden Mister B decided to draw angle a on his field. Aliens have already visited his field and left many different geometric figures on it. One of the figures is regular convex n-gon (regular convex polygon with n sides).
That’s why Mister B decided to use this polygon. Now Mister B must find three distinct vertices v1, v2, v3 such that the angle 角V1V2V3(where v2 is the vertex of the angle, and v1 and v3 lie on its sides) is as close as possible to a. In other words, the value |角V1V2V3 – a| should be minimum possible.
If there are many optimal solutions, Mister B should be satisfied with any of them.
从一个正n多边形上选择三个点,V1V2V3组成的角度离a最近。
由于同弧所对圆周角相等,所以V1=1,V2=2,找最长的V1V3即可。
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,a;
cin>>n>>a;
cout<<"2 1 "<<max(3,min(n,(n*a+90)/180+2))<<endl;
return 0;
}
D. Mister B and PR Shifts
Some time ago Mister B detected a strange signal from the space, which he started to study.
After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that’s why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.
Let’s define the deviation of a permutation p as ∑ni=1|p[i]−i|.
Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them.
Let’s denote id k (0 ≤ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example:
k = 0: shift p1, p2, … pn,
k = 1: shift pn, p1, … pn - 1,
…,
k = n - 1: shift p2, p3, … pn, p1.
n个数,从1-n乱序,可以将其向后移动p位,求秩序值最小到时候是第几次。
记录下每个数是在目标位置(a[i] – i ==0)的左边还是右边,存下所有在目标左边的数。
cur[i]记录距离目标位置为i的数字的个数。
L记录开始在左边的个数,R记录开始在右边的个数。
然后模拟右移,对于第i次右移,左边的有L = L-cur[i-1], 右边的有R = R+cur[i-1],即当次位移刚好有cur[i-1]个数原本在目标位置左跑到了目标右。
特判最后一个数跑到第一个。L+1,R-1;
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1000005;
int a[maxn], cur[maxn<<1];
int main(){
LL ans, sum = 0;
int n, L = 0, R = 0, temp = 0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
for(int i=1;i<=n;i++){
sum += abs(a[i]-i);
if(a[i]>=i) L++;
else R++;
if(a[i]>=i) cur[a[i]-i]++;
}
ans = sum;
for(int i=0;i<n-1;i++){
L -= cur[i]; R += cur[i];
sum = sum-L+R-abs(a[n-i]-n-1)+a[n-i]-1;
cur[a[n-i]+i]++;
L++;R--;
if(sum<ans)
ans = sum, temp = i+1;
}
cout<<ans<<" "<<temp<<endl;
return 0;
}

浙公网安备 33010602011771号