๋ฌธ์
ํ์ด
์กฐ๊ฑด์ ์ฐพ์๋๋ฐ ์์ด์ ์ถ๋ ฅ์ ๋ชปํด์ ํ๋ ธ๋ค....
์ฐ์ identity permutation ์์ ์์ ์์น๋ฅผ ๋ฐ๊ฟ ๋์ฌ ์ ์๋ ์ต๋ ์ฐจ์ ํฉ์ ์ญ์์ด๋ค.
๊ทธ๋ฆฌ๊ณ ๊ฒฐ๊ตญ ํน์ x์ y์ ๋ํ ์์๋ฅผ ๋ฐ๊พธ๊ฒ ๋๋ค๋ฉด, abs(x-y) + abs(x-y)์ด๋ค. ๊ทธ๋ฌ๋ฏ๋ก ๋ ์์ ๋์ค๊ฒ ๋๋ฏ๋ก ํ์๊ฐ ๋ต์ด ๋ ์ ์๋ค.
์์ด์ ๊ตฌํ๊ธฐ ์ํด์๋ ์ํ์ ์ธ ๊ท์น์ ์ฐพ์์ผ๋๋ค.
[1, 2, 3, 4, 5, 6 ~~~~ xn] ๊ฐ ์๋ค๊ณ ํ์.
1์ ์์น๋ฅผ ํน์ ํ xi์ ๋ฐ๊พผ๋ค๊ณ ํ๋ฉด, ๊ทธ๋ก ์ธํด ๋ฐ์ํ๋ ๊ฐ์ 1 - xi, xi - 1์ด๋ค. ์ฐจ์ ์ ๋๊ฐ์ ํฉ์ผ๋ก ๋ํ๋ด์ผ ํ๋ฏ๋ก ๊ฒฐ๊ตญ (xi - 1) * 2 ( xi > 1 ) ์ด ๋๋ค.
1์ ์์น์ xi ์ ์์น๋ฅผ ๋ฐ๊พธ๋ ๊ฒฝ์ฐ๊ฐ ์์น๋ฅผ ๋ฐ๊พธ๋ ๊ฒฝ์ฐ์ ์ ๋ค ์ค์์ ๊ฐ์ฅ ์ฐจ์ ์ ๋๊ฐ์ด ํฌ๋ค.
K๊ฐ 0์ด ๋ ๋๊น์ง, 1๊ณผ xi๋ฅผ ๋ฐ๊พธ๊ณ , 2์ xj๋ฅผ ๋ฐ๊พธ๊ณ ~~~ ์ด๋ ๊ฒ ์์ง์ด๋ฉด์ ์์ด์ ๊ตฌํ๋ฉด ๋๋ค.
๊ทธ๋ฌ๋ฏ๋ก 1๊ณผ ๋์ ์ lo, hi ๋ก ์ก๊ณ , ์ด๋ฅผ ํฌํฌ์ธํฐ๋ก ์์ง์ด๋ฉด์ ์ด๋ค ์์ ์ด๋ค ์๊ฐ ๋ฐ๋๋์ง ๊ตฌํ๋ฉด ๋๋ค.
#include <iostream>
// util
#include <algorithm>
#include <cmath>
#include <stdlib.h>
#include <stdint.h>
// data structure
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <deque>
#define INTMAX 987654321
#define ll long long
#define pii std::pair<int,int>
#define vi std::vector<int>
#define pll std::pair<long long , long long>
#define vl std::vector<long long>
#define for_N(N) for(int i=0; i<N; i++)
using namespace std;
int T;
int main() {
cin>>T;
while(T--) {
ll n, k; cin>>n>>k;
ll arr[200002]={0};
for(int i=1; i<=n; i++) arr[i] = i;
ll max_s = 0;
for(int i=1; i<=n; i++) {
max_s += abs(arr[i] - arr[n-i+1]);
}
if(k > max_s || k%2 == 1) {
cout<<"NO\n";
}
else {
int lo = 1, hi = n;
while(lo < hi) {
if(k >= abs(arr[lo] - arr[hi]) * 2) {
k -= abs(arr[lo] - arr[hi]) * 2;
swap(arr[lo], arr[hi]);
lo++;
hi--;
} else {
hi --;
}
}
cout<<"YES\n";
for(int i=1; i<=n; i++) cout<<arr[i]<<" ";
cout<<'\n';
}
}
}
'algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
27. Remove Element (0) | 2025.03.21 |
---|---|
DP - ์์ดํ ์ ์ ์ ํ ๊ณ ๋ฅด๋ ๋ฌธ์ (0) | 2025.03.21 |
๋ฐฑ์ค 2671 (0) | 2024.08.02 |
๋ฐฑ์ค 14848 (0) | 2024.08.02 |
๋ฐฑ์ค 1953 (0) | 2024.08.01 |