Two progressions CodeForces - 125D (暴力)

大意: 给定序列, 求划分为两个非空等差序列.

 

暴搜, 加个记忆化剪枝.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



#ifdef ONLINE_JUDGE
const int N = 3e4+10;
#else
const int N = 111;
#endif

int n, d1, d2, c[N], *f = c;
map<int,bool> v1[N], v2[N];
vector<int> a, b;

int dfs() {
	if (f==c+n) return !b.empty();
	if (a.size()<2||*f-a.back()==d1&&!v1[f-c+1].count(d1)) {
		a.pb(*f++);
		if (a.size()>=2) d1=a[1]-a[0], v1[f-c][d1]=1;
		if (dfs()) return 1;
		--f, a.pop_back();
	}
	if (b.size()<2||*f-b.back()==d2&&!v2[f-c+1].count(d2)) {
		b.pb(*f++);
		if (b.size()>=2) d2=b[1]-b[0], v2[f-c][d2]=1;
		if (dfs()) return 1;
		--f, b.pop_back();
	}
	return 0;
}

int main() {
	scanf("%d", &n);
	REP(i,0,n-1) scanf("%d", c+i);
	if (dfs()) {
		for (auto &&t:a) printf("%d ", t);hr;
		for (auto &&t:b) printf("%d ", t);hr;
	}
	else puts("No solution");
}

 

posted @ 2019-05-04 23:47  uid001  阅读(174)  评论(0编辑  收藏  举报