标签:code stdin i+1 表示 txt == ++i int for
做法:
实现时,一定要注意细节,一些地方的特判不足都会wa,我这里单独计算区间长度为2,3的答案才能ac
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
const int N = 300 + 17;
const int inf = 0x3f3f3f3f;
typedef long long ll;
using namespace std;
int n, m, f[N];
ll a[N], d;
bool can[N][N];
map<ll,bool> M;
int fd(int l,int r,ll x) {
rep(i,l,r)if(a[i]==x)return i;
return -1;
}
void init_can() {
memset(can,0,sizeof(can));
rep(i,1,n-1) if(M.find((a[i+1]-a[i]))!=M.end())can[i][i+1]=1;
rep(L,3,n) rep(l,1,n-L+1) {
int r = l + L - 1;
if(L == 3) { //***特判!!!
if(a[l+1]*2==a[r]+a[l]&&M.find(a[l+1]-a[l])!=M.end()) can[l][r] = 1;
else can[l][r] = 0;
continue;
}
if(M.find((a[r]-a[l]))!=M.end()) {
if(l+1==r) can[l][r] = 1;
else can[l][r] |= can[l+1][r-1];
}
int p = fd(l+1,r-1,(a[r]+a[l])/2);
if(abs(a[r]-a[l])%2==0&&p!=-1&&M.find((a[r]-a[l])/2)!=M.end()){
if(l+1==p&&p+1==r) can[l][r] = 1;
else if(l+1==p) can[l][r] |= can[p+1][r-1];
else if(p+1==r) can[l][r] |= can[l+1][p-1];
else can[l][r] |= (can[p+1][r-1]&can[l+1][p-1]);
}
rep(k,l+1,r) can[l][r] |= (can[l][k-1]&can[k][r]);
}
}
int main() {
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--) {
M.clear();
scanf("%d%d",&n,&m);
rep(i,1,n) scanf("%I64d",&a[i]);
rep(i,1,m) scanf("%I64d",&d),M[d]=1;
init_can();
memset(f,0,sizeof(f));
f[0] = f[1] = 0;
rep(i,2,n) {
f[i] = f[i-1];
rep(j,1,i-1) if(can[j][i]) f[i] = max(f[i],f[j-1]+(i-j+1));
}
printf("%d\n",f[n]);
}
return 0;
}
标签:code stdin i+1 表示 txt == ++i int for
原文地址:https://www.cnblogs.com/RRRR-wys/p/9438746.html