Submission #95948

# Submission time Handle Problem Language Result Execution time Memory
95948 2019-02-04T15:31:28 Z Retro3014 Skyscraper (JOI16_skyscraper) C++17
20 / 100
1034 ms 219928 KB
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <deque>

using namespace std;
typedef long long ll;
#define MAX_N 100
#define DIV 1000000007LL
int N;
vector<int> v;
ll ans;

bool chk[MAX_N+1];
int arr[MAX_N+1];
ll L, L2;

int zero(int x){
	return (x>0?x:-x);
}

int cost(int x, int y){
	if(x==-1 || y==-1)	return 0;
	return zero(v[x]-v[y]);
}

void dfs(int x){
	if(x==N){
		if(L>=L2){
			ans++;
			return;
		}
	}else{
		for(int i=0; i<N; i++){
			if(!chk[i]){
				arr[x] = i;
				chk[i] = true;
				if(x!=0){
					L2+=(ll)zero(v[arr[x]]-v[arr[x-1]]);
				}
				dfs(x+1);
				chk[i] = false;
				if(x!=0){
					L2-=(ll)zero(v[arr[x]]-v[arr[x-1]]);
				}
				arr[x] = 0;
			}
		}
	}}
void solve1(){
	dfs(0);
	return;}

#define MAX_K 20000
#define MAX_N2 14
#define MAX_L 100

ll dp[MAX_K+1][MAX_N2+1][MAX_L+1];
bool in[MAX_K+1][MAX_N2+1][MAX_L+1];

struct S{
	S (int x, int y, int z) : x(x), y(y), z(z) {}
	int x, y, z;
};

deque<S> dq;

void solve2(){
	dq.push_back({0, -1, 0});
	while(!dq.empty()){
		S now = dq.front(); dq.pop_front();
		int two = 1;
		for(int i=0; i<N; i++){
			if(((two&now.x)==0)){
				if(now.y==-1){
					S add(now.x+two, i, now.z);
					dp[add.x][add.y][add.z]+=1;
					dp[add.x][add.y][add.z]=dp[add.x][add.y][add.z]%DIV;
					if(!in[add.x][add.y][add.z]){
						dq.push_back(add); in[add.x][add.y][add.z] = true;
					}
				}
				else if(now.z+cost(i, now.y)<=L){
					S add(now.x+two, i, now.z+cost(i, now.y));
					dp[add.x][add.y][add.z]+=dp[now.x][now.y][now.z];
					dp[add.x][add.y][add.z]=dp[add.x][add.y][add.z]%DIV;
					if(!in[add.x][add.y][add.z]){
						dq.push_back(add); in[add.x][add.y][add.z] = true;
					}
				}
			}
			two*=2;
		}
	}
	int idx = (1<<N)-1;
	for(int i=0; i<N; i++){
		for(int j=0; j<=L; j++){
			ans+=dp[idx][i][j];
			ans=ans%DIV;
		}
	}
	return;
}

int main(){
	scanf("%d%lld", &N, &L);
	for(int i=0; i<N; i++){
		int x; scanf("%d", &x); v.push_back(x);
	}
	if(N<=8){
		solve1();
	}else{
		solve2();
	}
	printf("%lld", ans);
	return 0;
}

Compilation message

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:107:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%lld", &N, &L);
  ~~~~~^~~~~~~~~~~~~~~~~~
skyscraper.cpp:109:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x; scanf("%d", &x); v.push_back(x);
          ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 3 ms 256 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 4 ms 376 KB Output is correct
9 Correct 3 ms 256 KB Output is correct
10 Correct 4 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 52724 KB Output is correct
2 Correct 251 ms 191116 KB Output is correct
3 Correct 733 ms 215584 KB Output is correct
4 Correct 239 ms 184152 KB Output is correct
5 Correct 203 ms 166776 KB Output is correct
6 Correct 826 ms 219928 KB Output is correct
7 Correct 226 ms 177468 KB Output is correct
8 Correct 691 ms 214116 KB Output is correct
9 Correct 1034 ms 219660 KB Output is correct
10 Correct 246 ms 185764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 3 ms 256 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 4 ms 376 KB Output is correct
9 Correct 3 ms 256 KB Output is correct
10 Correct 4 ms 380 KB Output is correct
11 Correct 118 ms 52724 KB Output is correct
12 Correct 251 ms 191116 KB Output is correct
13 Correct 733 ms 215584 KB Output is correct
14 Correct 239 ms 184152 KB Output is correct
15 Correct 203 ms 166776 KB Output is correct
16 Correct 826 ms 219928 KB Output is correct
17 Correct 226 ms 177468 KB Output is correct
18 Correct 691 ms 214116 KB Output is correct
19 Correct 1034 ms 219660 KB Output is correct
20 Correct 246 ms 185764 KB Output is correct
21 Runtime error 8 ms 760 KB Execution killed with signal 11 (could be triggered by violating memory limits)
22 Halted 0 ms 0 KB -