답안 #725772

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
725772 2023-04-18T03:25:44 Z josanneo22 Alternating Heights (CCO22_day1problem1) C++17
25 / 25
830 ms 13744 KB
#include<bits/stdc++.h>
using namespace std;
inline int rd(){
	int x=0,w=1;
	char ch=getchar();
	for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
	for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	return x*w;
}
/*
    subtask 1 idea: since k=2, meaning that the array must have only 1 or 2
    so we can just check whether it is alternating 1 2 1 2 1 2 or 2 1 2 1 2 1
    this can be done in O(n^2)
    
    subtask 2&3 idea: let the array be something like:
    a>b b>c c>a
    we can define an edge from a to b if a>b
    a->b->c
    |     |
    <------
    which we can easily relate it to rock paper scissors
    meaning the answer is yes if and only if it doesn't form a cycle

    subtask 4: for each i in the array we just check the furthest r such that it doesn't
    form a cycle, just need to find cycles o(n) for each i
    overall O(n^2)
*/
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 3e5+10, MOD = 1e9+7; 

bool has_cycle(vector<vector<int>>& adj) {
    int n = sz(adj);
    vector<int> deg(n);
    for (int a = 0; a < n; a++)
        for (int b : adj[a])
            deg[b]++;
 
    vector<int> q; q.reserve(n);
    for (int i = 0; i < n; i++) if (deg[i] == 0) q.push_back(i);
    for (int rep = 0; rep < sz(q); rep++) {
        int c = q[rep];
        for (auto nxt : adj[c]) {
            deg[nxt]--;
            if (deg[nxt] == 0)
                q.push_back(nxt);
        }
    }
    return sz(q) < n;
}
void solve(){
	int n, k, q; cin >> n >> k >> q;
    vector<int> a(n); for (auto& x : a) cin >> x, --x;
 
    vector<int> can(n);
    for (int rep : {0, 1}) {
        for (int l = rep, r = l; l < n; l += 2) {
            while (r < n) {
                vector<vector<int>> adj(k);
                for (int i = l; i < r; i++) {
                    if (i % 2 == l % 2) adj[a[i]].push_back(a[i + 1]);
                    else adj[a[i + 1]].push_back(a[i]);
                }
                if (has_cycle(adj)) break;
                r++;
            }
            can[l] = r;
        }
    }
    while (q--) {
        int l, r; cin >> l >> r, --l, --r;
        if (can[l] <= r) {
            cout << "NO\n";
        } else {
            cout << "YES\n";
        }
    }
}
signed main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int tt=1; //cin>>tt;
	while(tt--){
		solve();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 168 ms 5956 KB Output is correct
2 Correct 152 ms 5116 KB Output is correct
3 Correct 156 ms 5088 KB Output is correct
4 Correct 116 ms 5068 KB Output is correct
5 Correct 131 ms 5212 KB Output is correct
6 Correct 160 ms 5068 KB Output is correct
7 Correct 156 ms 5088 KB Output is correct
8 Correct 180 ms 5156 KB Output is correct
9 Correct 176 ms 5236 KB Output is correct
10 Correct 240 ms 5628 KB Output is correct
11 Correct 280 ms 6192 KB Output is correct
12 Correct 333 ms 6132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 155 ms 5728 KB Output is correct
2 Correct 150 ms 10868 KB Output is correct
3 Correct 149 ms 10908 KB Output is correct
4 Correct 115 ms 8140 KB Output is correct
5 Correct 129 ms 9280 KB Output is correct
6 Correct 146 ms 11108 KB Output is correct
7 Correct 143 ms 10936 KB Output is correct
8 Correct 151 ms 11084 KB Output is correct
9 Correct 147 ms 11308 KB Output is correct
10 Correct 157 ms 11916 KB Output is correct
11 Correct 157 ms 11800 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 422 ms 476 KB Output is correct
3 Correct 496 ms 596 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 338 ms 340 KB Output is correct
8 Correct 538 ms 472 KB Output is correct
9 Correct 740 ms 468 KB Output is correct
10 Correct 565 ms 556 KB Output is correct
11 Correct 649 ms 732 KB Output is correct
12 Correct 491 ms 432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 168 ms 5956 KB Output is correct
2 Correct 152 ms 5116 KB Output is correct
3 Correct 156 ms 5088 KB Output is correct
4 Correct 116 ms 5068 KB Output is correct
5 Correct 131 ms 5212 KB Output is correct
6 Correct 160 ms 5068 KB Output is correct
7 Correct 156 ms 5088 KB Output is correct
8 Correct 180 ms 5156 KB Output is correct
9 Correct 176 ms 5236 KB Output is correct
10 Correct 240 ms 5628 KB Output is correct
11 Correct 280 ms 6192 KB Output is correct
12 Correct 333 ms 6132 KB Output is correct
13 Correct 155 ms 5728 KB Output is correct
14 Correct 150 ms 10868 KB Output is correct
15 Correct 149 ms 10908 KB Output is correct
16 Correct 115 ms 8140 KB Output is correct
17 Correct 129 ms 9280 KB Output is correct
18 Correct 146 ms 11108 KB Output is correct
19 Correct 143 ms 10936 KB Output is correct
20 Correct 151 ms 11084 KB Output is correct
21 Correct 147 ms 11308 KB Output is correct
22 Correct 157 ms 11916 KB Output is correct
23 Correct 157 ms 11800 KB Output is correct
24 Correct 1 ms 320 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 422 ms 476 KB Output is correct
27 Correct 496 ms 596 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 338 ms 340 KB Output is correct
32 Correct 538 ms 472 KB Output is correct
33 Correct 740 ms 468 KB Output is correct
34 Correct 565 ms 556 KB Output is correct
35 Correct 649 ms 732 KB Output is correct
36 Correct 491 ms 432 KB Output is correct
37 Correct 717 ms 13344 KB Output is correct
38 Correct 624 ms 13224 KB Output is correct
39 Correct 174 ms 12612 KB Output is correct
40 Correct 116 ms 8204 KB Output is correct
41 Correct 141 ms 9620 KB Output is correct
42 Correct 555 ms 13328 KB Output is correct
43 Correct 721 ms 13584 KB Output is correct
44 Correct 502 ms 13336 KB Output is correct
45 Correct 793 ms 13744 KB Output is correct
46 Correct 752 ms 13696 KB Output is correct
47 Correct 830 ms 13744 KB Output is correct