Educational Codeforces Round 69 (Rated for Div. 2) A~D Sloution

A. DIY Wooden Ladder

The meaning of problems: some can not cut wood, each with a length, to do a ladder, seeking the maximum number of steps of the ladder

Wooden ladder made into two intermediate planks and two, on both sides of several pieces of wood step

$ K $ is the number of steps required both sides of the wooden ladder length greater than equal to $ k + 1 $, is equal to the number of the intermediate board $ k $.

 

Directly to the largest and the second largest place on both sides of the board, the rest of the steps, set up the second largest wooden length $ x $, the number of steps the board is $ k $ answer is $ min (x-1, k) $

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=2e6+7;
int T,n,a[N];
int main()
{
    T=read();
    while(T--)
    {
        n=read();
        for(int i=1;i<=n;i++) a[i]=read();
        sort(a+1,a+n+1);
        printf("%d\n",min(a[n-1]-1,n-2));
    }
    return 0;
}
A. DIY Wooden Ladder

 

B. Pillars

The meaning of problems: a row of struts, each strut has a disc exactly, the size of each disk are not identical, determining whether the requirements can all move to a strut on the disk

Mobile requirements: 1. $ $ $ disc can only be moved to an adjacent pillar 2. This is only $ 3 $ $ a disc to move the disc size required on the holding strut decreases from bottom to top

 

Since the conditions of $ 2 and $ 3 $ $ conditions obviously must be the final pillar when the pillar where the initial maximum disk, and easy to find the maximum size of the disc on both sides of the pillars must be decremented

Otherwise, there must be a shift position does not move, otherwise it will be able to shift was finished (the current maximum of each disc to move step by step to the final position)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=2e5+7;
int n,a[N],pos;
int main()
{
    n=read();
    for(int i=1;i<=n;i++)
    {
        a[i]=read();
        if(a[i]>a[pos]) pos=i;
    }
    bool flag=1;
    for(int i=pos-1;i;i--) if(a[i]>a[i+1]) flag=0;
    for(int i=pos+1;i<=n;i++) if(a[i]>a[i-1]) flag=0;
    if(flag) printf("YES\n");
    else printf("NO\n");
    return 0;
}
B. Pillars

 

C. Array Splitting

The meaning of problems: a positive integer non subtrahend column $ A $, $ K $ happens to be divided into non-empty sequence, namely the final value of each subsequence and the difference between the maximum and minimum values

Seeking optimal partitioning scheme so that the total value of the minimum, the minimum output value

 

Consider a division of contributions, for the first time $ 0 $ dividing a total value of $ A [n] -A [1] $ (non-decreasing number of columns)

For the first division, the dividing position is set $ i, i + 1 $, the total value of $ A [i] -A [1] \ + \ A [n] -A [i + 1] $

The second division, location $ $ J, may assume $ j> i $, a total value of $ A [i] -A [1] \ + \ A [j] -A [i + 1] \ + \ A [n] -A [j + 1] $

Found that for each position of the divided $ k $, added value than the last time division multiple $ A [k] -A [k-1] $ (negative)

Therefore, all $ A [i] -A [i + 1] $ sorted from the former $ K-1 $ and the small initial $ A [n] -A [1] $ to accumulate

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
// f[i][k]=min a[i]+(f[j][k-1]-a[j+1])
//a[n]-a[1]
//a[i]-a[1]+a[n]-a[i+1]
//a[j]-a[1]+a[i]-a[j+1]+a[n]-a[i+1]
const int N=2e6+7;
priority_queue <int,vector<int>,greater<int> > Q;
int n,K,a[N],b[N],ans;
int main()
{
    n=read(),K=read();
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<n;i++) b[i]=a[i]-a[i+1];
    sort(b+1,b+n); ans=a[n]-a[1];
    for(int i=1;i<K;i++) ans+=b[i];
    printf("%d\n",ans);
    return 0;
}
C. Array Splitting

 

 

D. Yet Another Subarray Problem

The meaning of problems, given an integer column $ A $, to remove a 'slice' $ [l, r] $, the value of the slice is $ (\ sum_ {i = l} ^ {r} A [i]) - k \ left \ lceil \ frac {r-l + 1} {m} \ right \ rceil $

Seeking maximum value

Consider how do $ m = 1 $, greedy be apparent, to maintain the right point and the current $ I $ and columns $ $ now, the right end of each move $ I $, the $ A [i] -k $ $ now added $, if $ now <0 $ then the left point to the $ i $ not contribute to this section, and direct throw, then $ now = 0 $, and update the global answer in each update time $ now $ $ ans $

Found consider the number of columns per $ m $ a as a block, greedy by the same method, found that this considers only the left end of the lower mold $ m $ meaning $ 1 $ case, but because little $ m $, can be direct enumeration mode at the left end point $ m $ meaning, then are greedy

Note that only the right end and left end, taking into account the point of congruence, so enumerate each block must be considered when the current enumeration position right point in the block, the same update just fine

Look at the code may better understand it ..., note $ long \ long $

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=1e6+7;
int n,m,K,a[N];
vector <ll> V;//存每个块的和
ll ans;
int main()
{
    n=read(),m=read(),K=read();
    for(int= I . 1 ; I <= n-; I ++) A [I] = Read ();
     for ( int I = . 1 ; I <= m; I ++) // left end point in the enumeration mode m Significance 
    { 
        V.clear ( ); 
        for ( int J = I; J <= n-; J + = m) // the value of a block thrown vector in 
        { 
            LL T = 0 ;
             for ( int K = 0 ; K <m; K ++) T + = a [J + K]; 
            V.push_back (T -K); // remember '-K' 
        } 
        LL now = 0 ; // current interval value 
        for( Int J = 0 ; J <v.size (); J ++) // Enumeration block 
        {
             int POS = I + J * m; LL = -K nnow; // Enumeration right end position in the current block 
            for ( int K = 0 ; K <m; K ++) {nnow + = A [POS + K]; ANS = max (ANS, now + nnow);} // move the right end of 
            now + = V [J]; IF (now < 0 ) now = 0 ; 
            ANS = max (ANS, now); 
        } 
    } 
    COUT << ANS << endl;
     return  0 ; 
}
D. Yet Another Subarray Problem

 

 

 

 How like before $ 4 $ title are greedy ...

Guess you like

Origin www.cnblogs.com/LLTYYC/p/11241610.html