Submission #1013708

# Submission time Handle Problem Language Result Execution time Memory
1013708 2024-07-03T21:53:23 Z vjudge1 Xylophone (JOI18_xylophone) C++17
100 / 100
74 ms 2128 KB
#include "xylophone.h"
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
#define endl "\n"
const int mod=1e9+7;
#define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n";
template <typename T> istream& operator>>(istream& is, vector<T> &a) {
    copy_n(istream_iterator<T>(is), a.size(), a.begin()); return is;}
#ifdef IOI
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
 
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#else
#define dbg(...) 1337;
#endif
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
const int mxn=2e5+100;
static int A[5000];
#ifdef IOI
static const int N_MAX = 5000;
static const int Q_MAX = 10000;
static int N;
static bool used[N_MAX];
static int query_c;
static int answer_c;
int query(int s, int t) {
	if(query_c >= Q_MAX) {
		printf("Wrong Answer\n");
		exit(0);
	}
	query_c++;
	if(!(1 <= s && s <= t && t <= N)) {
		printf("Wrong Answer\n");
		exit(0);
	}
	int mx = 0, mn = N + 1;
	for(int i = s - 1; i < t; i++) {
		if(mx < A[i]) {
			mx = A[i];
		}
		if(mn > A[i]) {
			mn = A[i];
		}
	}
	return mx - mn;
}

void answer(int i, int a) {
	answer_c++;
	if(!(1 <= i && i <= N)) {
		printf("Wrong Answer\n");
		exit(0);
	}
	if(!(1 <= a && a <= N)) {
		printf("Wrong Answer\n");
		exit(0);
	}
	if(used[i - 1]) {
		printf("Wrong Answer\n");
		exit(0);
	}
	if(a != A[i - 1]) {
		printf("Wrong Answer\n");
		exit(0);
	}
	used[i - 1] = true;
}
#endif
map<pair<int,int>,int> mp;
int q(int l,int r){
	if(mp.count({l,r})) return mp[{l,r}];
	return mp[{l,r}]=query(l,r);
}
vector<int> solve(int pos,int n){
	bool visited[n+1];
	vector<int> ans(n+1);
	memset(visited,false,sizeof(visited));
	ans[pos]=1;
	visited[1]=1;
	auto check = [&](int a,int b,int c,int x,int y){
		vector<int> v={a,b,c};
		sort(all(v));
		vector<int> t={b,c};
		sort(all(t));
		if(v[2]-v[0]==x&&t[1]-t[0]==y){
			return 1;
		}else return 0;
	};
	if(pos!=n){
		ans[pos+1]=q(pos,pos+1)+1;
		visited[ans[pos+1]]=1;
		for(int i=pos+2;i<=n;i++){
			int a=ans[i-2];
			int b=ans[i-1];
			int x=q(i-2,i);
			int y=q(i-1,i);
			if(max(a,b)-min(a,b)==x){
				//donc min(a,b)<c<max(a,b)
				if(a>=b){
					ans[i]=y+b;
				}else{
					ans[i]=b-y;
				}
				if(ans[i]<0||ans[i]>n) return {-1};
				if(visited[ans[i]]){
					return {-1};
				}
				visited[ans[i]]=1;
			}else{
				//c is either min or max
				int c1=y+b;
				int c2=-y+b;
				// dbg(c1,c2)
				if(check(a,b,c1,x,y)&&c1>=1&&c1<=n){
					if(visited[c1]){
						return {-1};
					}
					visited[c1]=true;
					ans[i]=c1;
				}else if(check(a,b,c2,x,y)&&c2>=1&&c2<=n){
					if(visited[c2]){
						return {-1};
					}
					visited[c2]=true;
					ans[i]=c2;
				}else return {-1};
			}
		}
	}
	// dbg(ans)
	if(pos!=1){
		ans[pos-1]=q(pos-1,pos)+1;
		for(int i=pos-2;i>=1;i--){
			int a=ans[i+2];
			int b=ans[i+1];
			int x=q(i,i+2);
			int y=q(i,i+1);
			if(max(a,b)-min(a,b)==x){
				if(a>=b){
					ans[i]=y+b;
				}else{
					ans[i]=b-y;
				}
				if(ans[i]<0||ans[i]>n) return {-1};
				if(visited[ans[i]]){
					return {-1};
				}
				visited[ans[i]]=1;
			}else{
				// dbg(i)
				//c is either min or max
				int c1=b+y;
				int c2=b-y;
				if(check(a,b,c1,x,y)&&c1>=1&&c1<=n){
					if(visited[c1]){
						return {-1};
					}
					visited[c1]=true;
					ans[i]=c1;
				}else if(check(a,b,c2,x,y)&&c2>=1&&c2<=n){
					if(visited[c2]){
						return {-1};
					}
					visited[c2]=true;
					ans[i]=c2;
				}else return {-1};
			}
		}
	}
	return ans;
}
void solve(int N) {
	vector<int> ans;
	// ans=solve(4,N);
	// dbg(ans)
	// return;
	for(int i=1;i<=N;i++){
		ans=solve(i,N);
		// dbg(i,ans)
		if(ans.size()==1) continue;
		for(int i=1;i<=N;i++){
			answer(i,ans[i]);
		}
		return;
	}
	// dbg(ans)

}


#ifdef IOI
int main() {
	query_c = 0;
	answer_c = 0;
	if(scanf("%d", &N) != 1) {
		fprintf(stderr, "Error while reading input\n");
		exit(1);
	}
	for(int i = 0; i < N; i++) {
		if(scanf("%d", A + i) != 1) {
			fprintf(stderr, "Error while reading input\n");
			exit(1);
		}
		used[i] = false;
	}
	solve(N);
	if(!(answer_c == N)) {
		printf("Wrong Answer\n");
		exit(0);
	}
	printf("Accepted : %d\n", query_c);
}

#endif

Compilation message

xylophone.cpp:25:12: warning: 'A' defined but not used [-Wunused-variable]
   25 | static int A[5000];
      |            ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 440 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 436 KB Output is correct
14 Correct 1 ms 444 KB Output is correct
15 Correct 1 ms 436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 440 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 436 KB Output is correct
14 Correct 1 ms 444 KB Output is correct
15 Correct 1 ms 436 KB Output is correct
16 Correct 3 ms 856 KB Output is correct
17 Correct 6 ms 600 KB Output is correct
18 Correct 9 ms 696 KB Output is correct
19 Correct 10 ms 528 KB Output is correct
20 Correct 8 ms 344 KB Output is correct
21 Correct 10 ms 344 KB Output is correct
22 Correct 10 ms 492 KB Output is correct
23 Correct 10 ms 344 KB Output is correct
24 Correct 13 ms 488 KB Output is correct
25 Correct 9 ms 344 KB Output is correct
26 Correct 11 ms 616 KB Output is correct
27 Correct 10 ms 696 KB Output is correct
28 Correct 11 ms 520 KB Output is correct
29 Correct 10 ms 344 KB Output is correct
30 Correct 10 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 440 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 436 KB Output is correct
14 Correct 1 ms 444 KB Output is correct
15 Correct 1 ms 436 KB Output is correct
16 Correct 3 ms 856 KB Output is correct
17 Correct 6 ms 600 KB Output is correct
18 Correct 9 ms 696 KB Output is correct
19 Correct 10 ms 528 KB Output is correct
20 Correct 8 ms 344 KB Output is correct
21 Correct 10 ms 344 KB Output is correct
22 Correct 10 ms 492 KB Output is correct
23 Correct 10 ms 344 KB Output is correct
24 Correct 13 ms 488 KB Output is correct
25 Correct 9 ms 344 KB Output is correct
26 Correct 11 ms 616 KB Output is correct
27 Correct 10 ms 696 KB Output is correct
28 Correct 11 ms 520 KB Output is correct
29 Correct 10 ms 344 KB Output is correct
30 Correct 10 ms 344 KB Output is correct
31 Correct 24 ms 616 KB Output is correct
32 Correct 37 ms 700 KB Output is correct
33 Correct 41 ms 1064 KB Output is correct
34 Correct 68 ms 1796 KB Output is correct
35 Correct 55 ms 1076 KB Output is correct
36 Correct 65 ms 848 KB Output is correct
37 Correct 55 ms 964 KB Output is correct
38 Correct 61 ms 2128 KB Output is correct
39 Correct 56 ms 988 KB Output is correct
40 Correct 59 ms 960 KB Output is correct
41 Correct 67 ms 1124 KB Output is correct
42 Correct 66 ms 1408 KB Output is correct
43 Correct 58 ms 1092 KB Output is correct
44 Correct 57 ms 1392 KB Output is correct
45 Correct 55 ms 1164 KB Output is correct
46 Correct 55 ms 872 KB Output is correct
47 Correct 49 ms 992 KB Output is correct
48 Correct 55 ms 1360 KB Output is correct
49 Correct 54 ms 844 KB Output is correct
50 Correct 69 ms 1036 KB Output is correct
51 Correct 70 ms 1080 KB Output is correct
52 Correct 66 ms 1156 KB Output is correct
53 Correct 63 ms 952 KB Output is correct
54 Correct 62 ms 860 KB Output is correct
55 Correct 53 ms 848 KB Output is correct
56 Correct 70 ms 1024 KB Output is correct
57 Correct 54 ms 936 KB Output is correct
58 Correct 60 ms 840 KB Output is correct
59 Correct 74 ms 972 KB Output is correct
60 Correct 55 ms 912 KB Output is correct