Educational Codeforces Round 69 (Rated for Div. 2)

谁都会走 提交于 2019-11-27 18:02:54
                                                                                              A. DIY Wooden Ladder
                                                                                         time limit per test:2 seconds
                                                                                   memory limit per test:256 megabytes
                                                                                                             input:standard input
                                                                                                           output:standard output

Let's denote a kk-step ladder as the following structure: exactly k+2k+2 wooden planks, of which

  • two planks of length at least k+1k+1 — the base of the ladder;
  • kk planks of length at least 11 — the steps of the ladder;

Note that neither the base planks, nor the steps planks are required to be equal.

For example, ladders 11 and 33 are correct 22-step ladders and ladder 22 is a correct 11-step ladder. On the first picture the lengths of planks are [3,3][3,3] for the base and [1][1] for the step. On the second picture lengths are [3,3][3,3] for the base and [2][2] for the step. On the third picture lengths are [3,4][3,4] for the base and [2,3][2,3] for the steps.

You have nn planks. The length of the ii-th planks is aiai. You don't have a saw, so you can't cut the planks you have. Though you have a hammer and nails, so you can assemble the improvised "ladder" from the planks.

The question is: what is the maximum number kk such that you can choose some subset of the given planks and assemble a kk-step ladder using them?

Input

The first line contains a single integer TT (1T1001≤T≤100) — the number of queries. The queries are independent.

Each query consists of two lines. The first line contains a single integer nn (2n1052≤n≤105) — the number of planks you have.

The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1051≤ai≤105) — the lengths of the corresponding planks.

It's guaranteed that the total number of planks from all queries doesn't exceed 105105.

Output

Print TT integers — one per query. The ii-th integer is the maximum number kk, such that you can choose some subset of the planks given in the ii-th query and assemble a kk-step ladder using them.

Print 00 if you can't make even 11-step ladder from the given set of planks.

Example
input
Copy
4  4  1 3 1 3  3  3 3 2  5  2 3 3 4 2  3  1 1 2  
output
Copy
2  1  2  0  
Note

Examples for the queries 131−3 are shown at the image in the legend section.

The Russian meme to express the quality of the ladders:

                                                                                          

题解: n 个木棍,搭建 k 步梯的条件:

      1.有 k + 2个木头

      2.k + 2 个木头中选取最大的两个基地

      3.剩下的有多少个木头小于基地中最小的木头。

核心方程:min(a[n-1]-1,min(a[n-2]-1,n-2))

 1 #include <iostream>   2 #include <cstring>   3 #include <cstdio>   4 #include <algorithm>   5 using namespace std;   6 int main()   7 {   8     int T,a[100010];   9     while(~scanf(" %d",&T))  10     {  11         memset(a,0,sizeof(a));  12         while(T--)  13         {  14             int n;  15             scanf(" %d",&n);  16             for(int i = 0; i < n; i++)  17                 scanf("%d",&a[i]);  18             sort(a,a+n);  19            cout<<min(a[n-1]-1,min(a[n-2]-1,n-2))<<endl;  20         }  21     }  22     return 0;  23 }
View Code
                                                                                                                  B. Pillars
                                                                                                      time limit per test:1.5 seconds
                                                                                                memory limit per test:256 megabytes
                                                                                                                          input:standard input
                                                                                                                        output:
standard output

There are nn pillars aligned in a row and numbered from 11 to nn.

Initially each pillar contains exactly one disk. The ii-th pillar contains a disk having radius aiai.

You can move these disks from one pillar to another. You can take a disk from pillar ii and place it on top of pillar jj if all these conditions are met:

  1. there is no other pillar between pillars ii and jj. Formally, it means that |ij|=1|i−j|=1;
  2. pillar ii contains exactly one disk;
  3. either pillar jj contains no disks, or the topmost disk on pillar jj has radius strictly greater than the radius of the disk you move.

When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.

You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all nn disks on the same pillar simultaneously?

Input

The first line contains one integer nn (3n21053≤n≤2⋅105) — the number of pillars.

The second line contains nn integers a1a1, a2a2, ..., aiai (1ain1≤ai≤n), where aiai is the radius of the disk initially placed on the ii-th pillar. All numbers aiai are distinct.

Output

Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).

Examples
input
Copy
4  1 3 4 2  
output
Copy
YES  
input
Copy
3  3 1 2  
output
Copy
NO  
Note

In the first case it is possible to place all disks on pillar 33 using the following sequence of actions:

  1. take the disk with radius 33 from pillar 22 and place it on top of pillar 33;
  2. take the disk with radius 11 from pillar 11 and place it on top of pillar 22;
  3. take the disk with radius 22 from pillar 44 and place it on top of pillar 33;
  4. take the disk with radius 11 from pillar 22 and place it on top of pillar 33;

题解:n 个盘子从 i 移动到 j,题目限制条件:

       1. i 与 j 要相邻;

       2.呈现逐渐递减的趋势

若能够满足上述两个条件,输出"YES",反之输出"NO".

 1 #include <cstring>   2 #include <cmath>   3 #include <cstdio>   4 #include <cctype>   5 #include <cstdlib>   6 #include <ctime>   7 #include <vector>   8 #include <iostream>   9 #include <string>  10 #include <queue>  11 #include <set>  12 #include <map>  13 #include <algorithm>  14 #include <complex>  15 #include <stack>  16 #include <bitset>  17 #include <iomanip>  18 #include <list>  19 #if __cplusplus >= 201103L  20 #include <unordered_map>  21 #include <unordered_set>  22 #endif // __cplusplus  23 #define ll long long  24 #define ull unsigned long long  25 using namespace std;  26 const double clf = 1e-8;  27 const int INF = 0x7fffffff;  28 const int MMAX = 0xfffffff;  29 const int mod = 1e9 + 7;  30 int arr[200010];  31 int main()  32 {  33     ios::sync_with_stdio(false);  34     cin.tie(0);  35     cout.tie(0);  36     int n,position,MAXX = -1;  37     cin>>n;  38     for(int i = 1; i <= n; i++)  39     {  40         cin>>arr[i];  41         if(arr[i] > MAXX)  42         {  43             MAXX = arr[i];  44             position = i;  45         }  46     }  47     for(int i = position; i <= n; i++)  48         if(arr[i] < arr[i+1])  49         {  50             printf("NO\n");  51             return 0;  52         }  53     for(int i = position; i > 0; i--)  54         if(arr[i] < arr[i-1])  55         {  56             printf("NO\n");  57             return 0;  58         }  59     printf("YES\n");  60     return 0;  61 }
View Code
                                                                                                             C. Array Splitting
                                                                                                       time limit per test:2 seconds
                                                                                                 memory limit per test:256 megabytes
                                                                                                                           input:standard input
                                                                                                                         output:standard output

You are given a sorted array a1,a2,,ana1,a2,…,an (for each index i>1i>1 condition aiai1ai≥ai−1 holds) and an integer kk.

You are asked to divide this array into kk non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray.

Let max(i)max(i) be equal to the maximum in the ii-th subarray, and min(i)min(i) be equal to the minimum in the ii-th subarray. The cost of division is equal to i=1k(max(i)min(i))∑i=1k(max(i)−min(i)). For example, if a=[2,4,5,5,8,11,19]a=[2,4,5,5,8,11,19] and we divide it into 33 subarrays in the following way: [2,4],[5,5],[8,11,19][2,4],[5,5],[8,11,19], then the cost of division is equal to (42)+(55)+(198)=13(4−2)+(5−5)+(19−8)=13.

Calculate the minimum cost you can obtain by dividing the array aa into kk non-empty consecutive subarrays.

Input

The first line contains two integers nn and kk (1kn31051≤k≤n≤3⋅105).

The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109, aiai1ai≥ai−1).

Output

Print the minimum cost you can obtain by dividing the array aa into kk nonempty consecutive subarrays.

Examples
input
Copy
6 3  4 8 15 16 23 42  
output
Copy
12  
input
Copy
4 4  1 3 3 7  
output
Copy
0  
input
Copy
8 1  1 1 2 3 5 8 13 21  
output
Copy
20  
Note

In the first test we can divide array aa in the following way: [4,8,15,16],[23],[42][4,8,15,16],[23],[42].

  题解:n 个数分解成  k 个区间,用区间最大值减去最小值,问:m 个数 k 个 区间的最小值。

差分乱搞

 1 #include <iostream>   2 #include <cstdio>   3 #include <algorithm>   4 #define ll long long   5 using namespace std;   6 const int N = 3e5 + 7;   7 int a[N],dis[N];   8 int main()   9 {  10     ios::sync_with_stdio(false);  11     cin.tie(0); cout.tie(0);  12     int n,k;  13     cin>>n>>k;  14     for(int i = 0; i < n; i++)  15         cin>>a[i];  16     for(int i = 0; i < n - 1; i++)  17         dis[i] = a[i+1] - a[i];  18     sort(dis,dis+n-1);  19     ll ans = 0;  20     for(int i = 0; i < n - k; i++)  21         ans += dis[i];  22     cout<<ans<<endl;  23     return 0;  24 }
View Code
                                                                                                                   D. Yet Another Subarray Problem
                                                                                                                  time limit per test:2 seconds
                                                                                                            memory limit per test:256 megabytes
                                                                                                                                      input:standard input
                                                                                                                                    output:standard output

You are given an array a1,a2,,ana1,a2,…,an and two integers mm and kk.

You can choose some subarray al,al+1,,ar1,aral,al+1,…,ar−1,ar.

The cost of subarray al,al+1,,ar1,aral,al+1,…,ar−1,ar is equal to i=lraikrl+1m∑i=lrai−k⌈r−l+1m⌉, where x⌈x⌉ is the least integer greater than or equal to xx.

The cost of empty subarray is equal to zero.

For example, if m=3m=3, k=10k=10 and a=[2,4,15,3,4,8,3]a=[2,−4,15,−3,4,8,3], then the cost of some subarrays are:

  • a3a3:15k13=1510=5a3…a3:15−k⌈13⌉=15−10=5;
  • a3a4:(153)k23=1210=2a3…a4:(15−3)−k⌈23⌉=12−10=2;
  • a3a5:(153+4)k33=1610=6a3…a5:(15−3+4)−k⌈33⌉=16−10=6;
  • a3a6:(153+4+8)k43=2420=4a3…a6:(15−3+4+8)−k⌈43⌉=24−20=4;
  • a3a7:(153+4+8+3)k53=2720=7a3…a7:(15−3+4+8+3)−k⌈53⌉=27−20=7.

Your task is to find the maximum cost of some subarray (possibly empty) of array aa.

Input

The first line contains three integers nn, mm, and kk (1n3105,1m10,1k1091≤n≤3⋅105,1≤m≤10,1≤k≤109).

The second line contains nn integers a1,a2,,ana1,a2,…,an (109ai109−109≤ai≤109).

Output

Print the maximum cost of some subarray of array aa.

Examples
input
Copy
7 3 10  2 -4 15 -3 4 8 3  
output
Copy
7  
input
Copy
5 2 1000  -13 -4 -9 -20 -11  
output
Copy
0  

 题解:

        1.确定状态:dp[ i ][ j ] 表示第 i 个数,长度为 j 的区间

        2.状态转移方程:dp[ i ][ j ] = dp[ i - 1][ j - 1] + a[ i ]( j > 1)

                                    dp[ i ][ j ] = max(dp[ i - 1][ m - 1 ] + a[ i ] - k,a[ i ] - k)(j == 1)

                                    dp[ i ][ j ] = dp[ i - 1][ m - 1 ] + a[ i ];

 1 #include <bits/stdc++.h>   2 #define ll long long   3 using namespace std;   4 const int N = 3e5 + 7;   5 const int inf = 0x3f3f3f3f;   6 ll dp[N][11];  /*dp[i][j]代表的是以i为末尾位,长度对m取余等于j的这一段长度的计算和   7 */   8 ll a[N];   9 int main()  10 {  11     ios::sync_with_stdio(false);  12     cin.tie(0);cout.tie(0);  13     int n,m,k;  14     cin>>n>>m>>k;  15     for(int i = 1; i <= n; ++i)  16         cin>>a[i];  17     ll ans = 0;  18     memset(dp,-inf,sizeof(dp));  19     if(m == 1)  20     {  21         for(int i = 1; i <= n; ++i)  22         {  23             for(int j = 0; j < m; ++j)  24             {  25                 dp[i][j] = max(dp[i - 1][0] + a[i] - k,a[i] - k);  26                 ans = max(dp[i][j],ans);  27             }  28         }  29     }  30     else  31     {  32         for(int i = 1; i <= n; ++i)  33         {  34             for(int j = 0; j < m; ++j)  35             {  36                 if(j == 0)  37                     dp[i][j] = dp[i - 1][m - 1] + a[i];  38                 else if(j == 1) /*j == 1代表现在末尾取到第i位的长度比之前取到i-1位多了一个余数1,由于向上去整,所以多剪掉了一个k,还得在加上一个a[i]*/  39                     dp[i][j] = max(dp[i - 1][0] + a[i] - k,a[i] - k);  40                 else  41                     dp[i][j] = dp[i - 1][j - 1] + a[i];  42                 ans = max(dp[i][j],ans);  43             }  44         }  45     }  46     cout<<ans<<endl;  47     return 0;  48 }
View Code

 

 

 

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!