Educational Codeforces Round 69 (Rated for Div. 2)

A. DIY Wooden Ladder

The meaning of problems: take the ladder, selected from the two longest sides of the edge based, and select the other side when the step, and the step base side is smaller than the number;

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int main()
{
        ios::sync_with_stdio(false);
        int T;
        cin>>T;
        while(T--){
                int n;
                cin>>n;
                int a[maxn];
                for(int i=0;i<n;i++){
                        cin>>a[i];
                }
                sort(a,a+n);
                int flag=a[n-2]-1;
                if(n-2>=flag){
                        cout<<flag<<endl;
                }
                else cout<<n-2<<endl;
        }
        return 0;
}
View Code

B. Pillars

The meaning of problems: n-positions, each position of a radius ai plate, moving plate, all stacked determines whether movement conditions are satisfied: position i-> j, abs (ij) == 1, we have dishes, j or not, or to a radius greater than I;

Thinking: maximum radius of the recording position, the position of the maximum radius will not move, and then find the maximum radius smaller than the sides, (especially not the same radius, and a 1-n)

#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+100;
int main()
{
        ios::sync_with_stdio(false);
        int a[maxn];
        int n;
        cin>>n;
        int flag;
        for(int i=1;i<=n;i++){
                cin>>a[i];
                if(a[i]==n){
                        flag=i;
                }
        }
        int cnt=n-1,l=flag-1,r=flag+1;
        while(cnt>0){
                if(l>0&&a[l]==cnt)
                {
                        l--;
                        cnt--;
                }
                else if(r<=n&&a[r]==cnt)
                {
                        r++;
                        cnt--;
                }
                else
                {
                        cnt=-2;
                        break;
                }
                //cout<<"l="<<l<<"r="<<r<<"cnt"<<cnt<<endl;
        }
        if(cnt==-2)
                cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
        return 0;
}
View Code

C. Array Splitting

The meaning of problems: a number n, and has an ascending sort, to be divided into k subsets, all subsets of computing the difference between the maximum and minimum values ​​and that this minimum;

Idea: When the number of the subset 1, this difference is zero, when the number of divided parts of n k, the k-1 when a subset of a number, nk is not a number of sub-1 when the minimum set, computing array twenty-two difference sorting, the answer is minimum and before the nk;

#include <bits/stdc++.h>
using namespace std;
const int maxn=3e5+100;
int main()
{
        ios::sync_with_stdio(false);
        int a[maxn];
        int n,k;
        cin>>n>>k;
        for(int i=1;i<=n;i++){
                cin>>a[i];
        }
        int b[maxn];
        for(int i=1;i<n;i++){
                b[i]=a[i+1]-a[i];
        }
        sort(b+1,b+n);
        int sum=0;
        for(int i=1;i<=n-k;i++)
        {
                sum+=b[i];
        }
        cout<<sum<<endl;
}
View Code

 

Guess you like

Origin www.cnblogs.com/lin1874/p/11229539.html