A. DIY Wooden Ladder

题意:有一些不能切的木板,每个都有一个长度,要做一个梯子,求梯子的最大台阶数

做梯子的木板分为两种,两边的两条木板和中间的若干条台阶木板

台阶数为 $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

B. Pillars

题意:有一排支柱,每个支柱恰好有一个圆盘,每个圆盘的大小各不相同,要求判断是否能把圆盘全部移到一个支柱上

移动的要求:$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

C. Array Splitting

题意:一个正整数非减数列 $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的更多相关文章

  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)

                                                                                                  A. DIY ...

  3. 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 \( ...

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

    C. Array Splitting You are given a sorted array

  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. node.js从入门到放弃《模块》

    在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很 ...

  2. php object

    一.访问控制 <?php class Computer{ public $cpu = 880; private $name = 'xiaomi'; public function getname ...

  3. W3C 事件切换 颜色变化

    颜色变化代码: HTML: <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  4. 详解zabbix中文版安装部署

    一.zabbix简介(摘自百度百科) zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供柔软 ...

  5. Oracle 11g 详细安装步骤

    一.Oracle 下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可. 路径名称中,最好不要出现中文,也不要出现空格等不规则字符. 官方下地址: http://www.ora ...

  6. netty模型简介

    Netty工作原理图 netty抽象出了两组线程池,BossGroup专门负责客户端 的连接,WorkerGroup专门负责网络读写. BossGroup和WorkerGroup 类型都是NioEve ...

  7. linux运维、架构之路-Nginx提高

    一.虚拟主机搭建 1.基于域名的虚拟主机 [root@web01 html]# cat nginx.conf worker_processes ; events { worker_connection ...

  8. 如何添加筛选器 (Reporting Services)

    如果您希望在计算或显示时包含或排除特定值,可向数据集.数据区域或组添加筛选器.在运行时应用筛选器的顺序为:先对数据集,再对数据区域,最后对组,并按照组层次结构自上而下的顺序.在表.矩阵或列表中,对行组 ...

  9. 【2019 Multi-University Training Contest 7】

    01:https://www.cnblogs.com/myx12345/p/11653845.html 02: 03: 04: 05: 06:https://www.cnblogs.com/myx12 ...

  10. HDU 6024 Building Shops (简单dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6024 题意:有n个room在一条直线上,需要这这些room里面建造商店,如果第i个room建造,则要总 ...