Codeforces Educational Codeforces Round 21 [5.30] Tea Party&Array Division&Average Sleep Time&Lucky

大家都很强, 可与之共勉

A. Lucky Year
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Apart from having lots of holidays throughout the year, residents of Berland also have whole lucky years. Year is considered lucky if it has no more than 1 non-zero digit in its number. So years 100, 40000, 5 are lucky and 12, 3001 and 12345 are not.

You are given current year in Berland. Your task is to find how long will residents of Berland wait till the next lucky year.

Input
The first line contains integer number n (1 ≤ n ≤ 109) — current year in Berland.

Output
Output amount of years from the current year to the next lucky one.

Examples
input
4
output
1
input
201
output
99
input
4000
output
1000

#include <cctype>  
#include <cstdio>  
#include <cstring>

namespace FastIO  {
    const size_t str = 1 << 16;

    struct Reader  {
        char buf[str], *s, *t;
        Reader ( ) : s( ), t( ), buf() {    }
        inline char pick ( )  {
            return (s == t) ? ( t = buf + fread ( s = buf, 1, str , stdin ), *s++ ) : ( *s++ );
        }

        template < class T >
        inline Reader& operator >> ( T& x )  {
            static char ch;
            static short opt;
            opt = (ch != 45);
            while ( !isdigit ( ch = pick () ) && (ch ^ -1) && ( ch ^ 45 ) );
            if ( ch == -1 )     return *this;
            if ( ch == 45 )     {   opt = 0; ch = pick ();   }
            for ( x = -48 + ch; isdigit ( ch = pick () ); ( x *= 10 ) += ch - 48 );
            opt ? 1 : x = -x;
            return *this;
        }
    } cin;

    struct Writer  {
        char buf[str], *s, *t;
        Writer () : s ( buf ), t( buf + str ), buf ( ) {    }
        ~Writer () { fwrite( buf, 1, s - buf, stdout ); }

        inline void echo ( char c )  {
            ( s == t ) ? ( fwrite ( s = buf, 1, str, stdout ), *s++ = c ) : ( *s++ = c );
        }

        inline Writer& operator << ( long long x )  {
            if( !x ) return echo( 48 ), *this;
            static int t[21], top;
            while ( x ) t[++top] = x % 10, x /= 10;
            while ( top ) echo(t[top--] + 48);
            return *this;
        }
        inline Writer& operator << (const char* s)  {
            while ( *s ) echo( *s++ ) ;
            return *this;
        }
    } cout;
    const char *endl = "\n";
}

using FastIO :: cin;
using FastIO :: cout;
using FastIO :: endl;

#define max(a, b)  ( (a) > (b) ? (a) : (b) )
#define min(a, b)  ( (a) < (b) ? (a) : (b) )

int n, tmp, cnt, nzero, pos, a[15];
const unsigned int C[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };

int main()  {
    cin >> n;  tmp = n;
    while ( tmp )  a[cnt ++] = tmp % 10, tmp /= 10;
    if ( cnt == 1 )  {  puts ( "1" ); return 0;   }
    for ( int i = 0; i < cnt; ++i )
        nzero += a[i] != 0, pos = cnt;
    if (nzero == 1)  {
        ++a[pos - 1];
        if ( a[pos - 1] == 10 )  a[pos - 1] = 0, ++a[pos++];
        int res = 0;
        for ( int i = 0; i < pos; ++i)  res += C[i] * a[i];
        cout << res - n << endl;
    }  else  {
        int res;
        a[cnt - 1] < 9 ?  res = C[cnt - 1] * (a[cnt - 1] + 1)
                       :  res = C[cnt];
        cout << res - n << endl;
    }
}

B. Average Sleep Time
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
It’s been almost a week since Polycarp couldn’t get rid of insomnia. And as you may already know, one week in Berland lasts k days!

When Polycarp went to a doctor with his problem, the doctor asked him about his sleeping schedule (more specifically, the average amount of hours of sleep per week). Luckily, Polycarp kept records of sleep times for the last n days. So now he has a sequence a1, a2, …, an, where ai is the sleep time on the i-th day.

The number of records is so large that Polycarp is unable to calculate the average value by himself. Thus he is asking you to help him with the calculations. To get the average Polycarp is going to consider k consecutive days as a week. So there will be n - k + 1 weeks to take into consideration. For example, if k = 2, n = 3 and a = [3, 4, 7], then the result is .

You should write a program which will calculate average sleep times of Polycarp over all weeks.

Input
The first line contains two integer numbers n and k (1 ≤ k ≤ n ≤ 2·105).

The second line contains n integer numbers a1, a2, …, an (1 ≤ ai ≤ 105).

Output
Output average sleeping time over all weeks.

The answer is considered to be correct if its absolute or relative error does not exceed 10 - 6. In particular, it is enough to output real number with at least 6 digits after the decimal point.

Examples
input
3 2
3 4 7
output
9.0000000000
input
1 1
10
output
10.0000000000
input
8 2
1 2 4 100000 123 456 789 1
output
28964.2857142857

#include <bits/stdc++.h>

typedef long long LL;

LL n, c, k, ans;
double Ans;

LL a[200005];

int main ( )  {
    scanf ( "%d%d", &n, &k );
    for( int i = 1; i <= n; ++i )   scanf ( "%I64d",a + i );
    for( int i = 1; i <= n; ++i )  {
        if( i <= n - k + 1 )  {
            if ( c < k )  ++c;
            ans += c * a[i];
        }
        else  {
            if ( c > n - i + 1 )  --c;
            ans += c * a[i];
        }
    }
    Ans = ans * 1.0 / ( n - k + 1 );
    printf ( "%0.12lf\n", Ans );
}

C. Tea Party
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp invited all his friends to the tea party to celebrate the holiday. He has n cups, one for each of his n friends, with volumes a1, a2, …, an. His teapot stores w milliliters of tea (w ≤ a1 + a2 + … + an). Polycarp wants to pour tea in cups in such a way that:

Every cup will contain tea for at least half of its volume
Every cup will contain integer number of milliliters of tea
All the tea from the teapot will be poured into cups
All friends will be satisfied.
Friend with cup i won’t be satisfied, if there exists such cup j that cup i contains less tea than cup j but ai > aj.

For each cup output how many milliliters of tea should be poured in it. If it’s impossible to pour all the tea and satisfy all conditions then output -1.

Input
The first line contains two integer numbers n and w (1 ≤ n ≤ 100, ).

The second line contains n numbers a1, a2, …, an (1 ≤ ai ≤ 100).

Output
Output how many milliliters of tea every cup should contain. If there are multiple answers, print any of them.

If it’s impossible to pour all the tea and satisfy all conditions then output -1.

Examples
input
2 10
8 7
output
6 4
input
4 4
1 1 1 1
output
1 1 1 1
input
3 10
9 8 10
output
-1

(Wa 在第二个点)
贪心 可以AC

#include <cstdio>  
#include <algorithm>

int n, w, sum, cur, a[105], cap[105], ans[105];

struct A  {
    int x, id;
    A ( ) {   }
    A ( int x, int id ) : x ( x ), id ( id ) {     }
    inline short operator < ( const A& rhs )  const  {
        return x < rhs.x;
    }
} aa[105];

int main ( )  {
    scanf ( "%d%d", &n, &w );
    for ( int i = 1; i <= n; ++i )  scanf ( "%d", a + i );
    for ( int i = 1; i <= n; ++i )  sum += (a[i] + 1) >> 1;
    if ( sum > w )  {   puts ( "-1" ); return 0;   }
    for ( int i = 1; i <= n; ++i )  aa[i] = A ( a[i], i );
    std :: sort ( aa + 1, aa + 1 + n );
    for ( int i = n; i >= 1; --i )  {
        if ( w <= 0 )  {    puts ( "-1" ); return 0;   }
        cur = aa[i].id;
        ans[cur] = (aa[i].x + 1) >> 1;
        cap[cur] = a[cur] - ans[cur];
        w -= ans[cur];
    }
    if ( w > 0 )
        for ( int  i = n; w > 0 && i >= 1; --i )  
            (w - cap[aa[i].id] >= 0) ? (ans[aa[i].id] = aa[i].x, w -= cap[aa[i].id]) : (ans[aa[i].id] += w, w = 0);
//  if ( w )  { puts ( "-1" ); return 0;   }
    for ( int i = 1; i <= n; ++i )  printf( "%d%c", ans[i], ( i == n ) ? '\n' : ' ' );
}

D. Array Division
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position).

Inserting an element in the same position he was erased from is also considered moving.

Can Vasya divide the array after choosing the right element to move and its new position?

Input
The first line contains single integer n (1 ≤ n ≤ 100000) — the size of the array.

The second line contains n integers a1, a2… an (1 ≤ ai ≤ 109) — the elements of the array.

Output
Print YES if Vasya can divide the array after moving one element. Otherwise print NO.

Examples
input
3
1 3 2
output
YES
input
5
1 2 3 4 5
output
NO
input
5
2 2 3 4 5
output

#include <bits/stdc++.h>

typedef long long LL;

const int MaxN = 100005;

int n;  

struct node  {  
    LL v, id;  
    inline short operator < ( const node& rhs )  const  {
        return v < rhs.v;
    }
} s[MaxN];  

LL u[MaxN];  

inline int lower(int l,int r,LL v)  {  
    static int mid;
    while( l + 1 < r ){  
        mid = (l + r) >> 1;  
        ( s[mid].v >= v ) ? r = mid : l = mid;  
    }  
    if ( s[l].v == v ) return l;  
    else if ( s[r].v==v ) return r;  
    else return -1;  
}  
int main ( )   {  
    while( ~scanf( "%d", &n ) )  {  
        u[0] = 0;  
        LL sum = 0;  
        for ( int i = 1;i <= n; ++i )  {  
            scanf("%LLd",&u[i]);  
            s[i].v = u[i];  
            sum += u[i];  
            s[i].id = i;  
            u[i] += u[i-1];  
        }  
        if( sum & 1 ){  
            puts ( "NO" );
            continue;  
        }  
        std :: sort ( s + 1, s + 1 + n );  
        short flag = 0;  
        for ( int i = 1;i <= n; ++i )  {  
            if( u[i] > sum - u[i] )  {  
                LL t = ( u[i] - sum + u[i] ) >> 1;  
                int pos = lower(1, n, t);  
                if ( ~pos )
                for ( int j = pos; j <= n && s[j].v == t; ++j )  
                    if( s[j].id <= i ) { flag = 1; break; }   
            }  
            else if( u[i] < sum - u[i] )  {  
                LL t = ( sum - u[i] - u[i] ) >> 1;  
                int pos = lower(1, n, t);  
                if ( ~pos )  
                for(int j = pos; j <= n&&s[j].v == t; ++j)  {  
                    if ( s[j].id > i ) { flag = 1; break; }  
                }  
            }  
            else {  flag = 1;  break;  }  
            if ( flag )  break;  
        }  
        flag ? puts ( "YES" ) : puts ( "NO" );
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值