和第22轮教育 D题有关系https://blog.csdn.net/qq_40675883/article/details/101702946
题意:找四个子序列满足相邻元素 要么相差1要么模7相等
解:
1、最大费用最大流;
(压缩图的地方目前还有些困惑)但是代码是对的
#include<bits/stdc++.h>
#define en '\n'
#define ll long long
using namespace std;
const int N=3010,M=2*(4*N+(N*(N-1))/2);
const int inf=0x3f3f3f3f;
int ver[M],edge[M],cost[M],Next[M],head[M];
int d[N<<1],incf[N<<1],pre[N<<1],v[N<<1];
int n,k,tot,s,t,maxflow,ans;
void add(int x,int y,int z,int c){
ver[++tot]=y,edge[tot]=z,cost[tot]=c;
Next[tot]=head[x],head[x]=tot;
ver[++tot]=x,edge[tot]=0,cost[tot]=-c;
Next[tot]=head[y],head[y]=tot;
}
queue<int>q;
#define mem(a,b) memset(a,b,sizeof(a))
inline bool spfa(){
while(!q.empty())q.pop();
for(int i=1;i<=2*n+20;i++)d[i]=0xcfcfcfcf,v[i]=0;
q.push(s);d[s]=0;v[s]=1;
incf[s]=1<<30;
while(!q.empty()){
int x=q.front();q.pop();v[x]=0;
for(int i=head[x];i;i=Next[i]){
if(!edge[i])continue;
int y=ver[i];
if(d[y]<d[x]+cost[i]){
d[y]=d[x]+cost[i];
incf[y]=min(incf[x],edge[i]);
pre[y]=i;
if(!v[y])v[y]=1,q.push(y);
}
}
}
if(d[t]==0xcfcfcfcf)return false;
return 1;
}
inline void update(){
int x=t;
while(x!=s){
int i=pre[x];
edge[i]-=incf[t];
edge[i^1]+=incf[t];
x=ver[i^1];
}
maxflow+=incf[t];
ans+=d[t]*incf[t];
}
int a[N];
int rd(){
int tem;scanf("%d",&tem);return tem;
}
signed main()
{
#ifdef local
freopen("input2.txt","r",stdin);
#endif
tot=1;
n=rd();
s=2*n+3;
int ss=2*n+1;
t=2*n+2;
add(s,ss,4,0);
for(int i=1;i<=n;i++){
a[i]=rd();
add(ss,i,1,0);
add(i,i+n,1,1);
add(i+n,t,1,0);
//add(i,i+n,inf,0);
}
for(int i=1;i<=n;i++){
int tem=0;
for(int j=i+1;j<=n;j++){
if(a[j]%7==a[i]%7 ){
add(i+n,j,1,0);tem+=1;if(tem>4){tem=0;break;}
}
}
for(int j=i+1;j<=n;j++){
if(a[j]-1==a[i]){
add(i+n,j,1,0);tem+=1;if(tem>4){tem=0;break;}
}
}
for(int j=i+1;j<=n;j++){
if(a[j]+1==a[i]){
add(i+n,j,1,0);tem+=1;if(tem>4){tem=0;break;}
}
}
}
while(spfa()){
update();
}
//cout<<maxflow<<' ';
cout<<ans<<en;
return 0;
}