1023
Song_stok_SZH · · 闲话
前言
分析
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
见原题 Digital Path
一条路径合法当且仅当:
- 路径无法拓展
- 路径相邻元素差为
2 ,且具有单调性 - 路径长度大于等于
4 - 相邻元素有公共边
solution
从图论角度讨论,一条路径的起点和终点一定为入度等于
0 和出度等于0 的点。
考虑拓扑排序,发现主要约束条件为长度大于等于
#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 ,挂
T4
statement
给定序列
- 选择
i ,a_i+=2 - 选择
i ,a_i-=1
求将
特殊等差数列:至少有
solution
1
特殊等差数列,即为在等差数列上可以存在一个断点。
前后两个等差数列(长度可为
发现这个二元(公差,首项)问题中无法确定任何一元。
考虑枚举其中一元,因为整体的公差一定,整体的首项不定,自然要枚举公差。
为表述方便,下文统称公差为
2
枚举
根据两种操作的性质,考虑两种极端等差数列,一种是每一项都尽量比
而让
此时带入题目中所给的数据范围,粗略估算:
下界计算方法类似,最终得到:
就可以根据数据的
3
确定
因为想求
想让这个代价最小,即为求
设
根据推导结果,
注意,代码中
4
由于有两个等差数列,需要分别求出它们的代价,考虑进行前后缀维护,动态取最小值。
先看前缀维护的过程。
结合前面的推导,显然需要动态维护前缀的
因为首项是动态的,代价计算必然也是动态的。
回顾代价计算公式,考虑如何动态维护代价,发现等价于维护大根堆的奇数数量(
不难发现涉及奇偶关系,会有精度问题,所以分别计算
后缀的维护类似,但注意
最后回顾一下时间复杂度,枚举
卡常考虑只枚举
#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=1e6+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;
}