Codeforces Round#490 Div3

这次的题解,我想划划水,因为前三题很快就写出来了,而且对了,第四题写了一个不过一直超时,赛后改了一下,也过了。

A题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
int n,k;
int a[104];
int main()
{
    scanf("%d %d",&n,&k);
    for( int i = 1 ; i <= n ; i++ )
        scanf("%d",&a[i]);
    int l = 1;
    while( a[l] <= k && l <= n )
        l++;
    int r = n;
    while( a[r] <= k && r >= 1 )
        r--;
    int ans = l-1+n-r;
    if( ans > n )
        ans = n;
    cout<<ans<<endl;
    return 0;
}

B题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
int n,k;
char s[105];
vector<int> v;

void change( int r )
{
    char temp[104];
    int cnt = 1;
    for( int i = r ; i >= 1 ; i-- )
        temp[cnt++] = s[i];
    for( int i = 1 ; i <= r ; i++ )
        s[i] = temp[i];
}
int main()
{
    scanf("%d",&n);
    scanf("%s",s+1);
    for( int i = 2 ; i <= n ; i++ )
        if( n%i == 0 )
            v.push_back(i);
    for( int i = 0 ; i < v.size() ; i++ )
    {
        change(v[i]);
    }
    printf("%s\n",s+1);
    return 0;
}

C题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn = 5e5+5;
char s[maxn];
int n,k;
int num[30];
int judge[30];
int main()
{
    memset(judge,0,sizeof judge);
    memset(num,0,sizeof num);
    scanf("%d %d",&n,&k);
    scanf("%s",s);
    for( int i = 0 ; i < n ; i++ )
    {
        int t = s[i]-'a';
        num[t]++;
    }
    int rem = k;
    for( int i = 0 ; i < 26 ; i++ )
    {
        if( rem <= num[i] )
        {
            judge[i] = rem;
            break;
        }
        else
        {
            judge[i] = -1;
            rem -= num[i];
        }
    }
    for( int i = 0 ; i < n; i++ )
    {
        int t = s[i]-'a';
        if( judge[t] == 0 )
            printf("%c",s[i]);
        else if( judge[t] > 0 )
            judge[t]--;
    }
    printf("\n");
    return 0;
}

D题

D. Equalize the Remainders
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array consisting of nn integers a1,a2,,ana1,a2,…,an, and a positive integer mm. It is guaranteed that mm is a divisor of nn.

In a single move, you can choose any position ii between 11 and nn and increase aiai by 11.

Let's calculate crcr (0rm1)0≤r≤m−1) — the number of elements having remainder rr when divided by mm. In other words, for each remainder, let's find the number of corresponding elements in aa with that remainder.

Your task is to change the array in such a way that c0=c1==cm1=nmc0=c1=⋯=cm−1=nm.

Find the minimum number of moves to satisfy the above requirement.

扫描二维码关注公众号,回复: 2657894 查看本文章
Input

The first line of input contains two integers nn and mm (1n2105,1mn1≤n≤2⋅105,1≤m≤n). It is guaranteed that mm is a divisor of nn.

The second line of input contains nn integers a1,a2,,ana1,a2,…,an (0ai1090≤ai≤109), the elements of the array.

Output

In the first line, print a single integer — the minimum number of moves required to satisfy the following condition: for each remainder from 00 to m1m−1, the number of elements of the array having this remainder equals nmnm.

In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed 10181018.

Examples
input
Copy
6 3
3 2 0 6 10 12
output
Copy
3
3 2 0 7 10 14 
input
Copy
4 2
0 1 2 3
output
Copy
0
0 1 2 3 

有n个数,和一个除数。

对于这n个数,每个数去除以m,得到的余数进行统计,使0---- m-1 每个都是n/m。

比如  8 4

1 1 2 2 3 3 4 4

余数为0 的要有8/4 = 2个,余数为1的要有2个,余数为2和3的也是。

所以,很显然,我们需要把两个4加1

即,通过增加某个数的值,使得满足条件

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn = 2e5+5;
int n,m;
ll a[maxn];
int b[maxn];
int num[maxn];
struct spe
{
    int time;
    int add;
};
struct sm
{
    int index,time;
};
vector<spe> ans[maxn];
vector<sm> small;
vector<sm> solve;
int main()
{
    memset(num,0,sizeof num);
    scanf("%d %d",&n,&m);
    for( int i =  1; i <= n ; i++ )
     {
        scanf("%I64d",&a[i]);
        int t = a[i]%m;
        b[i] = t;
        num[t]++;
     }
    int lev = n/m;
    sm temp1;
    spe temp2;
    for( int i = m-1 ; i >= 0 ; i-- )
    {
        if( num[i] < lev )
        {
            temp1.index = i;
            temp1.time = lev-num[i];
            small.push_back(temp1);
        }
        else if( num[i] > lev )
        {
            int t = num[i]-lev;
            while( !small.empty() && t > 0 )
            {
                if( t >= small.back().time )
                {
                    temp2.time = small.back().time;
                    temp2.add = small.back().index-i;
                    ans[i].push_back(temp2);
                    t -= small.back().time;
                    small.pop_back();
                }
                else if( t < small.back().time )
                {
                    temp2.time = t;
                    temp2.add = small.back().index-i;
                    ans[i].push_back(temp2);
                    small[ small.size()-1 ].time -= t;
                    t = 0;
                }
            }
            if( t > 0 )
            {
                temp1.index = i;
                temp1.time = t;
                solve.push_back(temp1);
            }
        }
    }
    for( int i = 0 ; i < solve.size() ; i++ )
    {
        int t = solve[i].time;
        for( int j = small.size()-1 ; j >= 0 ; j-- )
        {
            if( small[j].time >= t )
            {
                temp2.time = t;
                temp2.add = m-( solve[i].index-small[j].index );
                small[j].time -= t;
                ans[ solve[i].index ].push_back(temp2);
                break;
            }
            else if( t > small[j].time && small[j].time )
            {
                t -= small[j].time;
                temp2.time = small[j].time;
                small[j].time = 0;
                temp2.add = m-( solve[i].index-small[j].index );
                ans[ solve[i].index ].push_back(temp2);
            }
        }
        if( small.back().time == 0 )
            small.pop_back();
    }
    ll total = 0;
    for( int i = 1 ; i <= n ; i++ )
    {
        int len = ans[ b[i] ].size();
        if( len )
        {
            a[i] += ans[ b[i] ][len-1].add;
            total += ans[ b[i] ][len-1].add;
            ans[ b[i] ][len-1].time--;
            if( ans[ b[i] ][ len-1 ].time == 0 )
                ans[ b[i] ].pop_back();
        }
    }
    printf("%I64d\n",total);
    printf("%I64d",a[1]);
    for( int i = 2 ; i <= n ; i++ )
        printf(" %I64d",a[i]);
    printf("\n");
    return 0;
}
/*
10 10 3
7 1 2 2 3 3 2 1 1 7
5 4 1
1 1 2 2 3

8 4
3 3 3 1 3 3 3 1
*/

猜你喜欢

转载自blog.csdn.net/qq_39627843/article/details/80787972