Educational Codeforces Round 103 (Rated for Div. 2)A. K-divisible Sum B. Inflation C. Longest Simple

A. K-divisible Sum

mathematics

Question: Choose n positive integers so that the sum can divide K, and the largest number of these n integers is as small as possible. The maximum is as small as possible, so the minimum sum is required, and then the minimum sum is equally divided among n numbers, and the maximum value is found.

When n<=k, the smallest sum is K, and the largest of the n numbers is k/n rounded up, and
rounded up method: (n+k-1)/k;

When n>k, the minimum sum is n/k rounded up and then *K, and then the maximum number of n numbers is the minimum sum divided by n and rounded up

In fact, regardless of the smallest sum of n and K, n/k is rounded up and then *K

AC code

#include <iostream>
using namespace std;
int main(){
    
    
    long long n,k,sum,ans;
    int T;
    cin >> T;
    for(int i = 0;i < T;i++){
    
    
        cin >> n >> k;
        sum =(n + k -1)/k * k;
        ans = (sum + n -1)/n;
        cout << ans << endl;
    }
    //system("pause");
    return 0;
}

B. Inflation

thinking

Meaning of the title: Give you n numbers, n numbers and k. The first number is the starting price of the product, and then how much it has increased each month. >Require everyone before/after the monthly price increase <= k/100.

Before the price increase/after the price increase <= k/100. Before the price increase 100 <= k After the price increase, you can use multiplication instead of division. Avoid the problem of loss of precision.

The above formula must be met every month, so if you are not satisfied, you can only increase the price! ! ! , Find at least how much price has been changed in total. So once you are not satisfied with the above formula, you only need to increase the price before the price increase until it just meets the above formula. It is not allowed to be like the Note below (it feels misleading)

Note
In the first test case, you can, for example, increase p0 by 50 and p1 by 49 and get array [20150,50,202,202]. Then you get the next inflation coefficients:

AC code

#include <iostream>
using namespace std;
int main(){
    
    
    int T,n;
    int k;
    cin >>T;
    long long sum,x,ans,sum1;
    for(int t = 0;t < T;t++){
    
    
        cin >> n >> k;
        ans = 0;
        cin >> sum;
        for(int i = 1;i < n;i++){
    
    
            cin >> x;
           
            if(x*100 > sum *k){
    
    
                //cout << x << " x" << endl;
                sum1 = (100*x + k -1)/k;
                
                ans+=(sum1-sum);
                sum = sum1;
                
            }
             sum +=x;
        }
        cout << ans << endl;
    }
    //system("pause");
    return 0;
}

C. Longest Simple Cycle

greedy

The meaning of the question: Let you find the longest simple ring from n connected chains (circle the ring and pass each point only once). Therefore, ai> bi is also satisfied, that is, crossed edges are also satisfied, so every time Add ai-the length of bi is abs(ai-bi), but when ai == bi, i and i -2 chains cannot be connected

Greedy, for the i-th article, if it can be connected to the i-2th article (ai!=bi), then judge which is long to connect and not to connect, and then choose the long one and save it as The largest ring currently contributing to the next chain, For the i-th chain. It is not difficult to draw a picture. If the i-2 is not connected (without passing through the point above i-2), then the i-2 will not help afterwards, so you can safely discard it. There is no aftereffect, so you can be greedy. Then compare with the existing maximum value.

Insert picture description here

AC code

#include <iostream>
#include <stdlib.h>
using namespace std;
int num[100200];
int a[100020];
int b[100020];
long long max(long long a,long long b){
    
    
    return a > b?a:b;
}
int main(){
    
    
    int T;
    int n;
    long long ans,sum,x,x1;
    cin >> T;
    for(int t  = 0;t < T;t++){
    
    
        scanf("%d",&n);
        ans = 0;
        sum = 0;
        for(int i = 0;i < n;i++)
            scanf("%d", &num[i]);
        for(int i = 0;i < n;i++)
            scanf("%d",&a[i]);
        for(int i = 0;i < n;i++)
            scanf("%d",&b[i]);
        ans = 0;
        for(int i = 1;i < n;i++){
    
    
            if(a[i] == b[i])
                sum = 0;//前面的连不起来 如果sum是max就已经被收了就没有意义了
            x = num[i]+1 + abs(a[i]-b[i]);//只取第i个
            x1 = sum;//在前面的基础上取第i个
            if(x1)  
                x1-=abs(a[i]-b[i]);

            x1+=(1+num[i]);

            sum = max(x,x1);//取了第i个的最大环 如果只取i 比取i和之前相连,就说明i之前的没有意义,
            //可以扔了
            ans = max(ans,sum); 

        }
        printf("%lld\n",ans);
    }
    //system("pause");
    return 0;
}

Guess you like

Origin blog.csdn.net/RunningBeef/article/details/113462077