前言
分析
T1
statement
给定长度 , 的序列 ,求对于每一个包含 的子序列和中的最大值。
solution
显而易见,可维护后缀最大前缀和和前缀最小前缀和,二者之差即为答案。
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using db=double;
const ll N=2e5+9,INF=0x3f3f3f3f3f3f3f3f;
ll n,a[N],pre[N],g[N];
signed main(){
// freopen("sum.in","r",stdin);
// freopen("sum.out","w",stdout);
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
pre[i]=pre[i-1]+a[i];
}
g[n+1]=-INF;
for(int i=n;i>=1;i--){
g[i]=max(g[i+1],pre[i]);
}
ll lst=0;
for(int i=1;i<=n;i++){
cout<<(g[i]-lst)<<" ";
lst=min(lst,pre[i]);
}
return 0;
}evaluation
简单题
T2
statement
一条路径合法当且仅当:
- 路径无法拓展
- 路径相邻元素差为 ,且具有单调性
- 路径长度大于等于
- 相邻元素有公共边
solution
从图论角度讨论,一条路径的起点和终点一定为入度等于 和出度等于 的点。
考虑拓扑排序,发现主要约束条件为长度大于等于 ,而将长度写入 状态又会爆炸,便可以忽略大于 的具体数值,因为在当前节点大于 的路径在下一个节点不会改变其路径性质。
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using db=double;
const ll N=1009,MOD=1e9+7;
ll n,m,a[N][N],rd[N*N],cd[N*N],f[N*N][5],ans;
vector<ll> G[N*N];
queue<ll> q;
ll calc(ll x,ll y){
return (x-1)*m+y;
}
signed main(){
// freopen("maze.in","r",stdin);
// freopen("maze.out","w",stdout);
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(i>1&&a[i-1][j]==a[i][j]+2){
G[calc(i,j)].emplace_back(calc(i-1,j));
rd[calc(i-1,j)]++;
cd[calc(i,j)]++;
}
if(i<n&&a[i+1][j]==a[i][j]+2){
G[calc(i,j)].emplace_back(calc(i+1,j));
rd[calc(i+1,j)]++;
cd[calc(i,j)]++;
}
if(j>1&&a[i][j-1]==a[i][j]+2){
G[calc(i,j)].emplace_back(calc(i,j-1));
rd[calc(i,j-1)]++;
cd[calc(i,j)]++;
}
if(j<m&&a[i][j+1]==a[i][j]+2){
// cout<<calc(i,j)<<" "<<calc(i,j+1)<<endl;
G[calc(i,j)].emplace_back(calc(i,j+1));
rd[calc(i,j+1)]++;
cd[calc(i,j)]++;
}
}
}
ll cnt=n*m;
for(int i=1;i<=cnt;i++){
if(!rd[i]){
q.push(i);
f[i][1]=1;
}
}
while(q.size()){
ll ls=q.front();
q.pop();
for(auto i:G[ls]){
rd[i]--;
for(int j=1;j<=3;j++){
f[i][j+1]=(f[i][j+1]+f[ls][j])%MOD;
}
f[i][4]=(f[i][4]+f[ls][4])%MOD;
if(!rd[i]) q.push(i);
}
}
for(int i=1;i<=cnt;i++){
if(!cd[i]){
// cout<<i<<" "<<f[i][4]<<endl;
ans=(ans+f[i][4])%MOD;
}
}
cout<<ans;
return 0;
}evaluation
赛时 行 j<m 打成了 j<n ,挂 ,说要检查又没检查,绷。
T3
statement
给定 的矩形,要求用若干个 的小矩形恰好填充大矩形,但要求每个格点不能被 个小矩形的顶点覆盖。
solution
想到Mondriaan’s Dream,但 断送想法。
用状压 加特判解决 。
数据有性质 和 可以模拟发现都是两种情况,注意 在 时成立。
发现在约束条件下,我们的填充非常严格:
我们将它作为公理,考虑在确定边缘的情况下进行填充:
evaluation
T4
statement
给定序列 和两种操作:
- 选择 ,
- 选择 ,
求将 变为特殊等差数列的最小操作次数。
特殊等差数列:至少有 个整数 满足 , 为定值
solution
特殊等差数列,即为在等差数列上可以存在一个断点。
前后两个等差数列(长度可为 )公差相同,但首项不同。
发现这个二元(公差,首项)问题中无法确定任何一元。
考虑枚举其中一元,因为整体的公差一定,整体的首项不定,自然要枚举公差。
为表述方便,下文统称公差为 ,首项为 。
枚举 时,采用一种全新方法,从数据范围上判定。
根据两种操作的性质,考虑两种极端等差数列,一种是每一项都比 大,一种是每一项都比 小。
而让 增加的速度一定比让 降低的速度快,故让 变为第一种等差数列的操作次数一定比变成第二种等差数列少:
此时带入题目中所给的数据范围,粗略估算:
下界计算方法类似,最终得到:
就可以根据数据的 来枚举 ,时间复杂度按下不表。
确定 之后,考虑将 变为等差数列的代价:
因为想求 ,考虑把 提出来,不妨设 ,可得:
想让这个代价最小,即为求 取何值时该式取最小值。
设 ,则 求该式的斜率:
根据推导结果,,即为有 个 小于 , 个 大于 ,可得 ,下标取整 。
注意,代码中 ,与推导结果显然相等。
由于有两个等差数列,需要分别求出它们的代价,考虑进行前后缀维护,动态取最小值。
先看前缀维护的过程。
结合前面的推导,显然需要动态维护前缀的 ,这里可以使用对顶堆,大根堆里存前 的 ,小根堆里存后 的 , 即为小根堆堆顶。
因为首项是动态的,代价计算必然也是动态的。
回顾代价计算公式,考虑如何动态维护代价,发现等价于维护小根堆的奇数数量(),奇数和(),偶数数量(),偶数和(),大根堆的数量(),和()。
不难发现涉及奇偶关系,会有精度问题,所以分别计算 和 。
后缀的维护类似,但注意 ,读者可自行推导。
最后回顾一下时间复杂度,枚举 , 维护前后缀等差数列,又因为 的枚举和 相关, 。
卡常考虑只枚举 。
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using db=double;
#define f_Getchar() getchar()
#define f_Putchar() putchar()
const ll N=1e5+9,M=8e5+9,Mod=1e9+7,mod=998244353,INF=0x3f3f3f3f3f3f3f3f;
const db eps=1e-6;
ll t=1;
ll n,a[N],c[N],pre[N][2],ans=INF;
priority_queue<ll,vector<ll>,less<ll>> qq;
priority_queue<ll,vector<ll>,greater<ll>> hq;
template <typename T> void read(T &x){
x=0;int f=1;char c=getchar();
for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
x*=f;
}
template <typename T> void print(T x){
if(x<0)putchar('-'),x*=-1;
if(x>9)print(x/10);
putchar(x%10+48);
}
inline void solve(){
read(n);
for(ll i=1;i<=n;i++) read(a[i]);
//O(2e6)
for(ll d=-M/n;d<=2;d++){
ll hnt=0,hum=0,qjum=0,qoum=0,qjnt=0,qont=0;
for(ll i=1;i<=n;i++){
c[i]=a[i]-(i-1)*d;
ll k=i-ll(ceil(db(i)/3.0))+1;
if(qq.size()<k-1){
if(c[i]>hq.top()){
if(hq.top()%2) qjnt++,qjum+=hq.top();
else qont++,qoum+=hq.top();
qq.push(hq.top());
hum-=hq.top();
hnt--;
hq.pop();
hum+=c[i];
hnt++;
hq.push(c[i]);
}
else{
if(c[i]%2) qjnt++,qjum+=c[i];
else qont++,qoum+=c[i];
qq.push(c[i]);
}
}
else if(qq.size()&&c[i]<qq.top()){
if(qq.top()%2) qjnt--,qjum-=qq.top();
else qont--,qoum-=qq.top();
hum+=qq.top();
hnt++;
hq.push(qq.top());
qq.pop();
if(c[i]%2) qjnt++,qjum+=c[i];
else qont++,qoum+=c[i];
qq.push(c[i]);
}
else{
hum+=c[i];
hnt++;
hq.push(c[i]);
}
ll x=hq.top();
if(x%2) pre[i][0]=hum-x-x*(hnt-1)+(qjnt*x-qjum)/2+(qont*x-qoum)/2+ll(ceil(db(qont)/2.0))+qont;
else pre[i][0]=hum-x-x*(hnt-1)+(qont*x-qoum)/2+(qjnt*x-qjum)/2+ll(ceil(db(qjnt)/2.0))+qjnt;
x--;
if(x%2) pre[i][0]=min(pre[i][0],hum-x-x*(hnt-1)+(qjnt*x-qjum)/2+(qont*x-qoum)/2+ll(ceil(db(qont)/2.0))+qont);
else pre[i][0]=min(pre[i][0],hum-x-x*(hnt-1)+(qont*x-qoum)/2+(qjnt*x-qjum)/2+ll(ceil(db(qjnt)/2.0))+qjnt);
}
priority_queue<ll,vector<ll>,less<ll>>().swap(qq);
priority_queue<ll,vector<ll>,greater<ll>>().swap(hq);
hnt=0,hum=0,qjum=0,qoum=0,qjnt=0,qont=0;
for(ll i=n;i>=1;i--){
c[i]=a[i]+(n-i)*d;
ll k=n-i-ll(ceil(db(n-i+1)/3.0))+2;
if(qq.size()<k-1){
if(c[i]>hq.top()){
if(hq.top()%2) qjnt++,qjum+=hq.top();
else qont++,qoum+=hq.top();
qq.push(hq.top());
hum-=hq.top();
hnt--;
hq.pop();
hum+=c[i];
hnt++;
hq.push(c[i]);
}
else{
if(c[i]%2) qjnt++,qjum+=c[i];
else qont++,qoum+=c[i];
qq.push(c[i]);
}
}
else if(qq.size()&&c[i]<qq.top()){
if(qq.top()%2) qjnt--,qjum-=qq.top();
else qont--,qoum-=qq.top();
hum+=qq.top();
hnt++;
hq.push(qq.top());
qq.pop();
if(c[i]%2) qjnt++,qjum+=c[i];
else qont++,qoum+=c[i];
qq.push(c[i]);
}
else{
hum+=c[i];
hnt++;
hq.push(c[i]);
}
ll x=hq.top();
if(x%2) pre[i][1]=hum-x-x*(hnt-1)+(qjnt*x-qjum)/2+(qont*x-qoum)/2+ll(ceil(db(qont)/2.0))+qont;
else pre[i][1]=hum-x-x*(hnt-1)+(qont*x-qoum)/2+(qjnt*x-qjum)/2+ll(ceil(db(qjnt)/2.0))+qjnt;
x--;
if(x%2) pre[i][1]=min(pre[i][1],hum-x-x*(hnt-1)+(qjnt*x-qjum)/2+(qont*x-qoum)/2+ll(ceil(db(qont)/2.0))+qont);
else pre[i][1]=min(pre[i][1],hum-x-x*(hnt-1)+(qont*x-qoum)/2+(qjnt*x-qjum)/2+ll(ceil(db(qjnt)/2.0))+qjnt);
ans=min(ans,pre[i+1][1]+pre[i][0]);
}
ans=min(ans,pre[1][1]);
priority_queue<ll,vector<ll>,less<ll>>().swap(qq);
priority_queue<ll,vector<ll>,greater<ll>>().swap(hq);
}
// cout<<ans;
print(ans);
}
signed main(){
// freopen("data.in","r",stdin);
// freopen("m.out","w",stdout);
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
while(t--) solve();
return 0;
}