Educational Codeforces Round 69 (Rated for Div. 2) A~D Sloution
题意:有一些不能切的木板,每个都有一个长度,要做一个梯子,求梯子的最大台阶数
做梯子的木板分为两种,两边的两条木板和中间的若干条台阶木板
台阶数为 $k$ 的梯子要求两边的木板长度大于等于 $k+1$ ,中间的木板数等于 $k$。
直接找到最大和次大的木板放两边,剩下的做台阶,设次大的木板长度为 $x$ ,台阶木板数为 $k$ 则答案就是 $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=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=2e6+;
int T,n,a[N];
int main()
{
T=read();
while(T--)
{
n=read();
for(int i=;i<=n;i++) a[i]=read();
sort(a+,a+n+);
printf("%d\n",min(a[n-]-,n-));
}
return ;
}
A. DIY Wooden Ladder
题意:有一排支柱,每个支柱恰好有一个圆盘,每个圆盘的大小各不相同,要求判断是否能把圆盘全部移到一个支柱上
移动的要求:$1.$ 只能移动到相邻圆盘. $2.$ 此支柱只有一个圆盘 $3.$ 要求移动到的支柱上的圆盘大小从下到上保持递减
由于条件 $2$ 和条件 $3$ 显然最终的支柱一定是初始时最大圆盘所在的支柱,然后容易发现最大支柱两边的圆盘大小一定要递减
不然就一定有位置移不动,否则一定可以移得完(每次把当前最大的圆盘一步步移动到最终位置即可)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=2e5+;
int n,a[N],pos;
int main()
{
n=read();
for(int i=;i<=n;i++)
{
a[i]=read();
if(a[i]>a[pos]) pos=i;
}
bool flag=;
for(int i=pos-;i;i--) if(a[i]>a[i+]) flag=;
for(int i=pos+;i<=n;i++) if(a[i]>a[i-]) flag=;
if(flag) printf("YES\n");
else printf("NO\n");
return ;
}
B. Pillars
题意:一个正整数非减数列 $A$,要分成恰好 $K$ 个非空子序列,最终的价值即为每个子序列最大值与最小值差的和
求最优的划分方案使得总价值最小,输出最小价值
考虑一次划分的贡献,对于第 $0$ 次划分,总价值为 $A[n]-A[1]$(数列非减)
对于第一次划分,设划分位置为 $i,i+1$,则总价值为 $A[i]-A[1]\ +\ A[n]-A[i+1]$
第二次划分,位置为 $j$,不妨设 $j>i$,总价值为 $A[i]-A[1]\ +\ A[j]-A[i+1]\ +\ A[n]-A[j+1]$
发现对于每一次划分的位置 $k$ ,增加的价值比上一次划分多 $A[k]-A[k-1]$(为负数)
所以把所有 $A[i]-A[i+1]$ 排序取前 $K-1$ 小与初始的 $A[n]-A[1]$ 累加即可
#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=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); 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+;
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=;i<=n;i++) a[i]=read();
for(int i=;i<n;i++) b[i]=a[i]-a[i+];
sort(b+,b+n); ans=a[n]-a[];
for(int i=;i<K;i++) ans+=b[i];
printf("%d\n",ans);
return ;
}
C. Array Splitting
D. Yet Another Subarray Problem
题意,给定一个整数列 $A$,要取出一个 '切片' $[l,r]$,切片的价值为 $(\sum_{i=l}^{r}A[i])-k\left \lceil \frac{r-l+1}{m} \right \rceil$
求最大价值
考虑 $m=1$ 时怎么做,显然可以贪心,维护右端点 $i$ 和当前数列的和 $now$ ,每次移动右端点 $i$ ,把 $A[i]-k$ 加入 $now$,如果 $now<0$ 则说明左端点到 $i$ 这一段没有贡献了,直接扔掉,然后 $now=0$,并在每次更新 $now$ 的时候更新全局答案 $ans$
发现考虑把数列每 $m$ 个看成一个块,用同样的方法贪心,发现这样只考虑了左端点在模 $m$ 意义下为 $1$ 的情况,但是因为 $m$ 不大,所以可以直接枚举模 $m$ 意义下左端点的位置,然后分别贪心
注意到右端点也只有考虑到与左端点同余的情况,所以枚举每一个块的时候都要考虑枚举当前右端点在块中的位置,同样更新就好了
看代码可能比较好理解吧...,注意 $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=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=1e6+;
int n,m,K,a[N];
vector <ll> V;//存每个块的和
ll ans;
int main()
{
n=read(),m=read(),K=read();
for(int i=;i<=n;i++) a[i]=read();
for(int i=;i<=m;i++)//枚举模m意义下的左端点
{
V.clear();
for(int j=i;j<=n;j+=m)//把块的值扔到vector里
{
ll t=;
for(int k=;k<m;k++) t+=a[j+k];
V.push_back(t-K);//记得'-K'
}
ll now=;//当前区间的值
for(int j=;j<V.size();j++)//枚举块
{
int pos=i+j*m; ll nnow=-K;//枚举右端点在当前块中的位置
for(int k=;k<m;k++) { nnow+=a[pos+k]; ans=max(ans,now+nnow); }//移动右端点
now+=V[j]; if(now<) now=;
ans=max(ans,now);
}
}
cout<<ans<<endl;
return ;
}
D. Yet Another Subarray Problem
怎么好像前 $4$ 题都是贪心...
Educational Codeforces Round 69 (Rated for Div. 2) A~D Sloution的更多相关文章
- Educational Codeforces Round 69 (Rated for Div. 2) E. Culture Code
Educational Codeforces Round 69 (Rated for Div. 2) E. Culture Code 题目链接 题意: 给出\(n\)个俄罗斯套娃,每个套娃都有一个\( ...
- Educational Codeforces Round 69 (Rated for Div. 2)
A. DIY ...
- 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 \( ...
- Educational Codeforces Round 69 (Rated for Div. 2) C. Array Splitting 水题
C. Array Splitting You are given a sorted array
- 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[ ...
- Educational Codeforces Round 69 (Rated for Div. 2) C. Array Splitting (思维)
题意:给你一个长度为\(n\)的升序序列,将这个序列分成\(k\)段,每一段的值为最大值和最小值的差,求\(k\)段值的最小和. 题解:其实每一段的最大值和最小值的差,其实就是这段元素的差分和,因为是 ...
- Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 【数学+分块】
一.题目 D. Yet Another Subarray Problem 二.分析 公式的推导时参考的洛谷聚聚们的推导 重点是公式的推导,推导出公式后,分块是很容易想的.但是很容易写炸. 1 有些地方 ...
- 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 ...
- 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 ...
随机推荐
- this与super的区别
调用super()的语句必须要写在子类构造方法的第一行. super()是在子类中调用父类的构造方法:this()是在同一类中调用其它方法. super()和this()都需要放在构造函数的第一行. ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 占卜DIY
题目地址 Code #include<iostream> #include<vector> #include<map> using namespace std; s ...
- 共享OrCAD9.2pSpice9.2+multisim下载地址
http://pan.baidu.com/s/1dDcfiH7ewb9绿色版,解压后即可用!http://pan.baidu.com/s/1kTG43WFMultisim v11绿色版.7zhttp: ...
- 对Kubernetes的研究
服务发现和负载平衡 自动包装 存储编排 自愈 自动部署和回滚 秘密和配置管理 批量执行 水平缩放 是一个对docer进行管理的平台.
- Linux CentOS7 安装docker
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口 ...
- redhat 修改yum源
问题现象: 现有的yum安装git失败,提示yum源连接失败 Error Downloading Packages: git--.el6_4..x86_64: failure: Packages/gi ...
- windows下使用pycharm开发基于ansible api的python程序
Window下python安装ansible,基于ansible api开发python程序 在windows下使用pycharm开发基于ansible api的python程序时,发现ansible ...
- day39—JavaScript缓冲运动
转行学开发,代码100天!——2018-04-24 今天继续学习JavaScript运动之缓冲运动.相对于匀速运动,缓冲运动的不同之处在于其速度值是不断变化的,越靠近目标点,速度越小. 即可以表示为: ...
- linux常用命令之文档
不常用,经常就会遗忘,mygod,不用则退化... 目录管理命令 ls:列出指定目录下的内容格式:ls [OPTION]... [FILE]... -a:显示所有文件包括隐藏文件 -A:显示除.和. ...