File tree Expand file tree Collapse file tree 1 file changed +72
-0
lines changed
Expand file tree Collapse file tree 1 file changed +72
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ title : 【精進7】AGC008-D
3+ date : 2025-1-09 15:29:00 0900
4+ categories : [競技プログラミング]
5+ tags : [AtCoder,AGC,精進,構築]
6+ math : true
7+ ---
8+ 貪欲に決めていく
9+
10+ ## 問題
11+ [ D - K-th K] ( https://atcoder.jp/contests/agc008/tasks/agc008_d )
12+ 長さ$N$列の数列$x$が与えられる。$1\le i \le N$を満たす各$i$をちょうど$N$回ずつ含む長さ$N^2$の数列であって、任意の$1\le i\le N$について$i$が$i$番目に現れるインデックスが$x_i$となるようなものが存在するならば$1$つ出力せよ。
13+
14+ ## 解法
15+ 答えとなる配列を用意しておき、配列の値がまだ決まっていないインデックスを``` set ``` で持っておく。$x_i$の昇順に見ていく。$x_i$までに未定のインデックスが$i-1$個なければ構築は不可能である。存在する場合、先頭の$i-1$個のインデックスの値を$i$に決定してしまってよい。また$x_i$のインデックスは$i$に決定する。これを一通り行う。これで$x_i$以前に$i$を$i$個配置できたので次に$x_i$より後ろに$i$を$N-i$個配置していく。これも同様に$x_i$の昇順に決めていけば良い。未定の最小のインデックスが$x_i$以下になった場合構築は不可能である。
16+ ``` cpp
17+ #include " template.hpp"
18+ void solve () {
19+ LL (n);
20+ vl x(n);
21+ rep(i, n) {
22+ input(x[ i] );
23+ --x[ i] ;
24+ }
25+ vl ord(n);
26+ iota(all(ord), 0);
27+ sort(all(ord), [ &x] (ll i, ll j) { return x[ i] < x[ j] ; });
28+ vl ans(n * n, -1);
29+ set<ll > unused;
30+ rep(i, n * n) unused.insert(i);
31+ vl sf;
32+ for(auto &i : ord) {
33+ rep(_ , i) {
34+ auto itr = unused.begin();
35+ ans[ * itr] = i;
36+ unused.erase(unused.begin());
37+ }
38+ if(unused.count(x[ i] ) == 0) {
39+ print("No");
40+ return;
41+ }
42+ ans[ x[ i]] = i;
43+ unused.erase(x[ i] );
44+ rep(_ , n - i - 1) sf.push_back(i);
45+ }
46+ for(auto &i : sf) {
47+ auto itr = unused.begin();
48+ if(* itr < x[ i] ) {
49+ print("No");
50+ return;
51+ }
52+ ans[ * itr] = i;
53+ unused.erase(itr);
54+ }
55+ assert(unused.empty());
56+ print("Yes");
57+ for(auto &i : ans)
58+ cout << i + 1 << " ";
59+ print();
60+ }
61+
62+ int main () {
63+ ios::sync_with_stdio (false);
64+ std::cin.tie(nullptr);
65+ cout << std::setprecision(16);
66+ int t = 1;
67+ rep(_ , t) {
68+ solve();
69+ }
70+ }
71+
72+ ```
You can’t perform that action at this time.
0 commit comments