Educational Codeforces Round 69 (Rated for Div. 2) A B C D
Educational Codeforces Round 69 (Rated for Div. 2)
A
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5+100; int t; int n; int a[N]; int main(){ cin >> t; while(t--){ cin >> n; for(int i=0;i<n;i++){ cin >> a[i]; } sort(a,a+n); int h=min(a[n-1],a[n-2]); if(h<2){ cout << "0\n"; } else cout << min(n-2,h-1) << "\n"; } return 0; }
B
数组必须先增后减,或者单调递增,单调递减
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5+100; int n; int a[N]; int main(){ cin >> n; for(int i = 0; i < n; i++){ cin >> a[i]; } int i; for(i = 1; i < n; i++){ if(a[i] < a[i-1]) break; } int flag = 1; for(i=i+1;i<n;i++){ if(a[i] > a[i-1]){ flag = 0; } } if(flag) cout << "YES\n"; else cout << "NO\n"; return 0; }
C
要使数组分成k份,必须选择k-1个间隔,间隔差值越大,最后的每个区间max-min的和越小
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 3e5+100; int n,k; int a[N]; int p[N]; bool cmp(int x,int y){ return x>y; } int main(){ cin >> n >> k; for(int i = 0; i < n; i++){ cin >> a[i]; } for(int i=1; i < n; i++){ p[i] = a[i] - a[i-1]; } sort(p+1,p+n,cmp); ll res = 0; for(int i=1; i<=k-1;i++){ res += p[i]; } cout << a[n-1]- a[0]-res << "\n"; return 0; }
D
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=3e5+100; int n,m,k; int a[N]; ll sum[N]; ll dp[N]; int main(){ cin >> n >> m >> k; for(int i = 1; i <= n; i++){ cin >> a[i]; dp[i] = max(0, a[i]-k); sum[i] = sum[i-1] + a[i]; } ll res = 0; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(i-j >= 0){ dp[i] = max(dp[i], dp[i-j]-k + sum[i]-sum[i-j]); res = max(res,dp[i]); } } } cout << res << endl; }