Codeforces Round #421 (Div. 2)

Codeforces Round #421 (Div. 2)

Table of Contents Codeforces Round #421 (Div. 2)A. Mister B and Book ReadingB. Mister B and Angle in PolygonD. Mister B and PR Shifts

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.

Input

First and only line contains five space-separated integers: c, v0, v1, a and l (1 ≤ c ≤ 1000, 0 ≤ l < v0 ≤ v1 ≤ 1000, 0 ≤ a ≤ 1000) — the length of the book in pages, the initial reading speed, the maximum reading speed, the acceleration in reading speed and the number of pages for rereading.

Output

Print one integer — the number of days Mister B needed to finish the book.

题意:有一本书c页,每天看v0+i*a页,最多看v1页,每次返回l页,几天看完?

#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 ans = 0;
    while(true) {
        ans++;
        c-=v0;
        if(c<=0)
            break;
        v0+=a;
        if(v0>v1)
            v0=v1;
        c+=l;
    }

    printf("%d
",ans);
    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 (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 should be minimum possible.

If there are many optimal solutions, Mister B should be satisfied with any of them.

Input

First and only line contains two space-separated integers n and a (3 ≤ n ≤ 105, 1 ≤ a ≤ 180) — the number of vertices in the polygon and the needed angle, in degrees.

Output

Print three space-separated integers: the vertices v1, v2, v3, which form . If there are multiple optimal solutions, print any of them. The vertices are numbered from 1 to n in clockwise order.

题意:正n边形,选三个点,组成的三角形最接近;

分析:求出最小角,180/n

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    double a;
    scanf("%d%lf",&n,&a);

    double k = 180.0/n;

    double ans = k;
    double minx = abs(a-ans);
    int flag = 2;
    for(int i=1;i<n-2;i++) {
        ans+=k;
        if(minx>abs(a-ans)) {
            flag = i+2;
            minx = abs(a-ans);
        }
    }

    printf("%d %d %d
",1,n,flag);
    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 .

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, ... p**n,
  • k = 1: shift pn, p1, ... pn - 1,
  • ...,
  • k = n - 1: shift p2, p3, ... p**n, p1.

Input

First line contains single integer n (2 ≤ n ≤ 106) — the length of the permutation.

The second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the elements of the permutation. It is guaranteed that all elements are distinct.

Output

Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them.

题意:循环队列n次,使得sum_{i=1}^{n}|p[i]-i| 最小;

分析:和CSUFTOJ1021原理很类似;

记录下来cur移动k次,有多少个正数要变符号,这个cur是一个动态的,最后一个数移动到最前面会产生影响,最后一个数的移走,会对整体要L,R产生影响。

#include<stdio.h>
#include<stdlib.h>
#define LL long long
int p[1000005], cur[2000005];
int main(void)
{
    LL ans, sum;
    int n, L, r, i, temp;
    scanf("%d", &n);
    sum = L = r = temp = 0;
    for(i=1; i<=n; i++)
        scanf("%d", &p[i]);

    for(i=1; i<=n; i++)
    {
        sum += abs(p[i]-i);
        if(p[i]>=i) {
            L++;
            cur[p[i]-i]++;
        }
        else  r++;
    }

    ans = sum;
    for(i=0; i<n-1; i++)
    {
        L -= cur[i];
        r += cur[i];
        sum = sum-L+r-abs(p[n-i]-n-1)+p[n-i]-1;
        cur[p[n-i]+i]++;
        L++, r--;   //最后一个数产生的影响
        if(sum<ans) {
            ans = sum;
            temp = i+1;
        }
    }

    printf("%lld %d
", ans, temp);
    return 0;
}
原文地址:https://www.cnblogs.com/TreeDream/p/7107579.html