답안 #95946

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
95946 2019-02-04T15:29:04 Z Retro3014 Skyscraper (JOI16_skyscraper) C++17
5 / 100
4 ms 376 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(){
	dp[0][0][0] = 1;
	in[0][0][0] = true;
	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.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 'void solve2()':
skyscraper.cpp:77:17: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
    if((two&now.x==0)){
            ~~~~~^~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:101: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:103: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);
          ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 248 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 256 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 256 KB Output is correct
10 Correct 4 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 248 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 256 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 256 KB Output is correct
10 Correct 4 ms 256 KB Output is correct
11 Incorrect 2 ms 376 KB Output isn't correct
12 Halted 0 ms 0 KB -