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 (1≤T≤1001≤T≤100) — the number of queries. The queries are independent.

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

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1051≤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 1−31−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))

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int T,a[];
while(~scanf(" %d",&T))
{
memset(a,,sizeof(a));
while(T--)
{
int n;
scanf(" %d",&n);
for(int i = ; i < n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
cout<<min(a[n-]-,min(a[n-]-,n-))<<endl;
}
}
return ;
}
                                                                                                                  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 |i−j|=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 (3≤n≤2⋅1053≤n≤2⋅105) — the number of pillars.

The second line contains nn integers a1a1, a2a2, ..., aiai (1≤ai≤n1≤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".

 #include <cstring>
#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <iostream>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <complex>
#include <stack>
#include <bitset>
#include <iomanip>
#include <list>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif // __cplusplus
#define ll long long
#define ull unsigned long long
using namespace std;
const double clf = 1e-;
const int INF = 0x7fffffff;
const int MMAX = 0xfffffff;
const int mod = 1e9 + ;
int arr[];
int main()
{
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n,position,MAXX = -;
cin>>n;
for(int i = ; i <= n; i++)
{
cin>>arr[i];
if(arr[i] > MAXX)
{
MAXX = arr[i];
position = i;
}
}
for(int i = position; i <= n; i++)
if(arr[i] < arr[i+])
{
printf("NO\n");
return ;
}
for(int i = position; i > ; i--)
if(arr[i] < arr[i-])
{
printf("NO\n");
return ;
}
printf("YES\n");
return ;
}
                                                                                                             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 ai≥ai−1ai≥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 (4−2)+(5−5)+(19−8)=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 (1≤k≤n≤3⋅1051≤k≤n≤3⋅105).

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109, ai≥ai−1ai≥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 个 区间的最小值。

差分乱搞

 #include <iostream>
#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 3e5 + ;
int a[N],dis[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(); cout.tie();
int n,k;
cin>>n>>k;
for(int i = ; i < n; i++)
cin>>a[i];
for(int i = ; i < n - ; i++)
dis[i] = a[i+] - a[i];
sort(dis,dis+n-);
ll ans = ;
for(int i = ; i < n - k; i++)
ans += dis[i];
cout<<ans<<endl;
return ;
}
                                                                                                                   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,…,ar−1,aral,al+1,…,ar−1,ar.

The cost of subarray al,al+1,…,ar−1,aral,al+1,…,ar−1,ar is equal to ∑i=lrai−k⌈r−l+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:

  • a3…a3:15−k⌈13⌉=15−10=5a3…a3:15−k⌈13⌉=15−10=5;
  • a3…a4:(15−3)−k⌈23⌉=12−10=2a3…a4:(15−3)−k⌈23⌉=12−10=2;
  • a3…a5:(15−3+4)−k⌈33⌉=16−10=6a3…a5:(15−3+4)−k⌈33⌉=16−10=6;
  • a3…a6:(15−3+4+8)−k⌈43⌉=24−20=4a3…a6:(15−3+4+8)−k⌈43⌉=24−20=4;
  • a3…a7:(15−3+4+8+3)−k⌈53⌉=27−20=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 (1≤n≤3⋅105,1≤m≤10,1≤k≤1091≤n≤3⋅105,1≤m≤10,1≤k≤109).

The second line contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−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 ];

 #include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 3e5 + ;
const int inf = 0x3f3f3f3f;
ll dp[N][]; /*dp[i][j]代表的是以i为末尾位,长度对m取余等于j的这一段长度的计算和
*/
ll a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie();cout.tie();
int n,m,k;
cin>>n>>m>>k;
for(int i = ; i <= n; ++i)
cin>>a[i];
ll ans = ;
memset(dp,-inf,sizeof(dp));
if(m == )
{
for(int i = ; i <= n; ++i)
{
for(int j = ; j < m; ++j)
{
dp[i][j] = max(dp[i - ][] + a[i] - k,a[i] - k);
ans = max(dp[i][j],ans);
}
}
}
else
{
for(int i = ; i <= n; ++i)
{
for(int j = ; j < m; ++j)
{
if(j == )
dp[i][j] = dp[i - ][m - ] + a[i];
else if(j == ) /*j == 1代表现在末尾取到第i位的长度比之前取到i-1位多了一个余数1,由于向上去整,所以多剪掉了一个k,还得在加上一个a[i]*/
dp[i][j] = max(dp[i - ][] + a[i] - k,a[i] - k);
else
dp[i][j] = dp[i - ][j - ] + a[i];
ans = max(dp[i][j],ans);
}
}
}
cout<<ans<<endl;
return ;
}

Educational Codeforces Round 69 (Rated for Div. 2)的更多相关文章

  1. Educational Codeforces Round 69 (Rated for Div. 2) E. Culture Code

    Educational Codeforces Round 69 (Rated for Div. 2) E. Culture Code 题目链接 题意: 给出\(n\)个俄罗斯套娃,每个套娃都有一个\( ...

  2. Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 背包dp

    D. Yet Another Subarray Problem You are given an array \(a_1, a_2, \dots , a_n\) and two integers \( ...

  3. Educational Codeforces Round 69 (Rated for Div. 2) C. Array Splitting 水题

    C. Array Splitting You are given a sorted array

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

    A. DIY Wooden Ladder 题意:有一些不能切的木板,每个都有一个长度,要做一个梯子,求梯子的最大台阶数 做梯子的木板分为两种,两边的两条木板和中间的若干条台阶木板 台阶数为 $k$ 的 ...

  5. Educational Codeforces Round 69 (Rated for Div. 2)D(DP,思维)

    #include<bits/stdc++.h>using namespace std;int a[300007];long long sum[300007],tmp[300007],mx[ ...

  6. Educational Codeforces Round 69 (Rated for Div. 2) C. Array Splitting (思维)

    题意:给你一个长度为\(n\)的升序序列,将这个序列分成\(k\)段,每一段的值为最大值和最小值的差,求\(k\)段值的最小和. 题解:其实每一段的最大值和最小值的差,其实就是这段元素的差分和,因为是 ...

  7. Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 【数学+分块】

    一.题目 D. Yet Another Subarray Problem 二.分析 公式的推导时参考的洛谷聚聚们的推导 重点是公式的推导,推导出公式后,分块是很容易想的.但是很容易写炸. 1 有些地方 ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

随机推荐

  1. word 文档导出 (freemaker+jacob)--java开发

    工作中终于遇到了 需要导出word文旦的需求了.由于以前没有操作过,所以就先百度下了,基本上是:博客园,简书,CDSN,这几大机构的相关帖子比较多,然后花了2周时间 才初步弄懂.  学习顺序: 第一阶 ...

  2. java常见面试题目(一)

    在大四实习阶段,秋招的时候,面试了很多家公司,总结常见的java面试题目:(答案可以自己百度) 1.你所用oracle的版本号是多少? 2.tomcat修改8080端口号的配置文件是哪个? 3.myb ...

  3. 微信公众号接入服务器验证(Go实现)

    1 基本流程 将token.timestamp.nonce三个参数进行字典序排序 将三个参数字符串拼接成一个字符串进行sha1加密 开发者获得加密后的字符串可与signature对比,标识该请求来源于 ...

  4. 物流运输trans「ZJOI2006」

    [题目描述] 物流公司要把一批货物从码头\(A\)运到码头\(B\).由于货物量比较大,需要\(n\)天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运 ...

  5. vscode中配置git

    vscode中配置git vscode 报错 未找到Git.请安装Git,或在"git.path" 设置中配置 第一步安装git git安装方法自行解决,提供git下载连接! gi ...

  6. Python模拟登录淘宝

    最近想爬取淘宝的一些商品,但是发现如果要使用搜索等一些功能时基本都需要登录,所以就想出一篇模拟登录淘宝的文章!看了下网上有很多关于模拟登录淘宝,但是基本都是使用scrapy.pyppeteer.sel ...

  7. JavaWeb无框架,借助反射采用精巧设计模式实现放微信PC聊天页面

    本周开始在写仿写一个微信PC端的聊天页面,没有使用ssh.ssm等框架,采用JavaWeb.反射.MySQL.C3P0等技术.这里把其中和核心技术列出来请大家指教. 与传统JavaWeb项目的区别 传 ...

  8. Java反射Reflect的使用详解

    目录 一. 什么是反射 二. 反射的基础Class 2.1 Class类概述 2.2 Class类对象获取的三种方式 三. 反射-构造函数 3.1 getDeclaredConstructor(Cla ...

  9. springboot + jedisCluster

    如果使用的是redis2.x,在项目中使用客户端分片(Shard)机制. 如果使用的是redis3.x中的集群,在项目中使用jedisCluster. 1.项目结构 2.pom.xml 1 <? ...

  10. 一个接口多个实现类的Spring注入方式

    1. 首先, Interface1 接口有两个实现类 Interface1Impl1 和 Interface1Impl2 Interface1 接口: package com.example.serv ...