Submission #486256


Source Code Expand

Copy
// Enjoy your stay.

#include <bits/stdc++.h>

#define long long long
#define LOOPVAR_TYPE long

#define all(x) (x).begin(), (x).end()
#define sz(x) ((LOOPVAR_TYPE)(x).size())
#define foreach(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i, n) _rep2(i, 0, n)
#define _rep2(i, a, b) for(LOOPVAR_TYPE i = (LOOPVAR_TYPE)(a); i < (LOOPVAR_TYPE)(b); i++)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)

#define fir first
#define sec second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back

const double EPS = 1e-9;
const double PI = acos(-1.0);
const long INF = 1070000000LL;
const long MOD = 1000000007LL;

using namespace std;

typedef istringstream iss;
typedef stringstream sst;
typedef pair<LOOPVAR_TYPE, LOOPVAR_TYPE> pi;
typedef vector<LOOPVAR_TYPE> vi;

int W, H;
int a[100010], b[8];

int ID[10000000];
int getState[10000000];
int p10[8];

int dp[103][5044];

int conv(int p[8]){
	int res = 0;
	rep(i, W) res += p[i] * p10[i];
	return ID[res];
}

int f(int cur, int stateID){
	int& res = dp[cur][stateID];
	if(res != -1) return res;
	int state = getState[stateID];
	int p[8];
	rep(i, W){
		p[i] = state / p10[i] % 10;
	}
	//cout<<cur<<" ";
	//rep(i, W) cout << p[i]<<" ";cout<<endl;

	if(cur == H+1){
		int ok = 1;
		rep(i, W) ok &= p[i] == b[i];
		res = ok ? 0 : INF;
	}else{
		res = INF;
		// go to next
		if(cur == H){
			res = min(res, f(cur+1, stateID));
		}else{
			swap(p[a[cur]], p[a[cur]+1]);
			int nID = conv(p);
			res = min(res, f(cur+1, nID));
			swap(p[a[cur]], p[a[cur]+1]);
		}

		// draw a line
		rep(i, W-1){
			//cout<<i<<endl;
			swap(p[i], p[i+1]);
			//rep(i,W)cout<<p[i]<<" ";cout<<endl;
			int nID = conv(p);
			//cout<<nID<<endl;
			res = min(res, 1 + f(cur, nID));
			swap(p[i], p[i+1]);
		}
	}

	return res;
}

int main(){
	cin.tie(NULL);
	ios_base::sync_with_stdio(false);
	
	cin >> W >> H;
	assert(H <= 100);
	rep(i, H) cin >> a[i];
	rep(i, W) cin >> b[i];

	p10[0] = 1;
	rep(i, W){
		p10[i+1] = p10[i] * 10;
	}
	int perm[8];
	rep(i, W) perm[i] = i;
	int counter = 0;
	do{
		int code = 0;
		rep(i, W){
			code += p10[i] * perm[i];
		}
		ID[code] = counter;
		getState[counter] = code;
		counter++;
		//cout<<code<<" "<<counter<<endl;
	}while(next_permutation(perm, perm + W));

	memset(dp, -1, sizeof(dp));
	cout<<f(0, 0)<<endl;
}

Submission Info

Submission Time
Task C - 天下一不正
User evima
Language C++11 (GCC 4.9.2)
Score 45
Code Size 2531 Byte
Status
Exec Time 287 ms
Memory 7456 KB

Test Cases

Set Name Score / Max Score Test Cases
small 45 / 45 00_sample_1.txt, 00_sample_2.txt, 01_manual00.txt, 01_manual01.txt, 01_manual02.txt, 01_manual03.txt, 01_manual04.txt, 05_small100.txt, 05_small101.txt, 05_small102.txt, 05_small103.txt, 05_small104.txt, 05_small105.txt, 05_small106.txt, 05_small107.txt, 05_small108.txt, 05_small109.txt, 05_small110.txt, 05_small111.txt, 05_small112.txt, 05_small113.txt, 05_small114.txt, 05_small115.txt, 05_small116.txt, 05_small117.txt, 05_small118.txt, 05_small119.txt, 05_small120.txt, 05_small121.txt, 05_small122.txt, 05_small123.txt, 05_small124.txt, 05_small125.txt, 05_small126.txt, 05_small127.txt, 05_small128.txt, 05_small129.txt, 05_small130.txt, 05_small131.txt, 05_small132.txt, 05_small133.txt, 05_small134.txt
All 0 / 105 00_sample_1.txt, 00_sample_2.txt, 01_manual00.txt, 01_manual01.txt, 01_manual02.txt, 01_manual03.txt, 01_manual04.txt, 05_small100.txt, 05_small101.txt, 05_small102.txt, 05_small103.txt, 05_small104.txt, 05_small105.txt, 05_small106.txt, 05_small107.txt, 05_small108.txt, 05_small109.txt, 05_small110.txt, 05_small111.txt, 05_small112.txt, 05_small113.txt, 05_small114.txt, 05_small115.txt, 05_small116.txt, 05_small117.txt, 05_small118.txt, 05_small119.txt, 05_small120.txt, 05_small121.txt, 05_small122.txt, 05_small123.txt, 05_small124.txt, 05_small125.txt, 05_small126.txt, 05_small127.txt, 05_small128.txt, 05_small129.txt, 05_small130.txt, 05_small131.txt, 05_small132.txt, 05_small133.txt, 05_small134.txt, 10_large100.txt, 10_large101.txt, 10_large102.txt, 10_large103.txt, 10_large104.txt, 10_large105.txt, 10_large106.txt, 10_large107.txt, 10_large108.txt, 10_large109.txt, 10_large110.txt, 10_large111.txt, 10_large112.txt, 10_large113.txt, 10_large114.txt, 10_large115.txt, 10_large116.txt, 10_large117.txt, 10_large118.txt, 10_large119.txt, 10_large120.txt, 10_large121.txt, 10_large122.txt, 10_large123.txt, 10_large124.txt, 10_large125.txt, 10_large126.txt, 10_large127.txt, 10_large128.txt, 10_large129.txt, 10_large130.txt, 10_large131.txt, 10_large132.txt, 10_large133.txt, 10_large134.txt, 10_large135.txt, 11_manual00.txt
Case Name Status Exec Time Memory
00_sample_1.txt 30 ms 2848 KB
00_sample_2.txt 29 ms 2976 KB
01_manual00.txt 30 ms 2980 KB
01_manual01.txt 29 ms 2860 KB
01_manual02.txt 38 ms 7200 KB
01_manual03.txt 45 ms 7200 KB
01_manual04.txt 159 ms 7208 KB
05_small100.txt 30 ms 2856 KB
05_small101.txt 28 ms 2848 KB
05_small102.txt 32 ms 2844 KB
05_small103.txt 31 ms 3368 KB
05_small104.txt 42 ms 7284 KB
05_small105.txt 29 ms 2852 KB
05_small106.txt 29 ms 2852 KB
05_small107.txt 31 ms 2964 KB
05_small108.txt 43 ms 3480 KB
05_small109.txt 158 ms 7332 KB
05_small110.txt 96 ms 7200 KB
05_small111.txt 51 ms 7320 KB
05_small112.txt 30 ms 2848 KB
05_small113.txt 30 ms 2844 KB
05_small114.txt 32 ms 2848 KB
05_small115.txt 43 ms 3488 KB
05_small116.txt 161 ms 7328 KB
05_small117.txt 51 ms 7200 KB
05_small118.txt 62 ms 7200 KB
05_small119.txt 68 ms 7196 KB
05_small120.txt 114 ms 7328 KB
05_small121.txt 160 ms 7208 KB
05_small122.txt 159 ms 7208 KB
05_small123.txt 161 ms 7332 KB
05_small124.txt 160 ms 7456 KB
05_small125.txt 161 ms 7336 KB
05_small126.txt 160 ms 7332 KB
05_small127.txt 160 ms 7232 KB
05_small128.txt 159 ms 7336 KB
05_small129.txt 159 ms 7328 KB
05_small130.txt 161 ms 7336 KB
05_small131.txt 161 ms 7332 KB
05_small132.txt 159 ms 7328 KB
05_small133.txt 159 ms 7324 KB
05_small134.txt 157 ms 7324 KB
10_large100.txt 276 ms 796 KB
10_large101.txt 279 ms 796 KB
10_large102.txt 276 ms 916 KB
10_large103.txt 278 ms 788 KB
10_large104.txt 273 ms 804 KB
10_large105.txt 278 ms 920 KB
10_large106.txt 275 ms 916 KB
10_large107.txt 276 ms 920 KB
10_large108.txt 276 ms 804 KB
10_large109.txt 280 ms 920 KB
10_large110.txt 287 ms 920 KB
10_large111.txt 279 ms 796 KB
10_large112.txt 279 ms 808 KB
10_large113.txt 282 ms 808 KB
10_large114.txt 272 ms 796 KB
10_large115.txt 280 ms 808 KB
10_large116.txt 275 ms 804 KB
10_large117.txt 276 ms 800 KB
10_large118.txt 274 ms 920 KB
10_large119.txt 279 ms 796 KB
10_large120.txt 275 ms 804 KB
10_large121.txt 279 ms 804 KB
10_large122.txt 280 ms 808 KB
10_large123.txt 274 ms 812 KB
10_large124.txt 280 ms 800 KB
10_large125.txt 281 ms 800 KB
10_large126.txt 279 ms 804 KB
10_large127.txt 279 ms 800 KB
10_large128.txt 276 ms 808 KB
10_large129.txt 280 ms 804 KB
10_large130.txt 277 ms 804 KB
10_large131.txt 269 ms 928 KB
10_large132.txt 274 ms 804 KB
10_large133.txt 271 ms 792 KB
10_large134.txt 286 ms 808 KB
10_large135.txt 276 ms 924 KB
11_manual00.txt 275 ms 804 KB