A. Pretty Permutations
题意:求1~n的一个序列,使得x[i]!=i,并且使得sum(abs(x[i] - i))的值最小。
思路:由于每个点到其相邻格的距离最短,所以可以直接交换相邻点,并且如果n为奇数,那么只能交换最后两个。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll mod = 1e9 + 7, N = 1e6 + 10;
ll t, n, m, x[N];
inline void solve() {
cin >> n;
for(int i = 1; i <= n; i++) x[i] = i;
for(int i = 2; i <= n; i+=2) swap(x[i], x[i - 1]);
if(n & 1) swap(x[n-1], x[n]);
for(int i = 1; i <= n; i++) cout << x[i] << " ";
cout << endl;
return ;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> t;
while(t--) {
solve();
}
return 0;
}
B. Pleasant Pairs
题意:给定一个数字序列x[n],其中1<=x[i]<=2n,并且对于任意的i!=j,x[i]!=x[j],求解满足i<j且x[i]*x[j]=i+j的{i,j}数量。
思路:由于i+j的值域为[3,2n - 1],并且数字序列两两互不相同,以此对于x[i] + x[j] = i + j,我们可以枚举x[j]的值。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll mod = 1e9 + 7, N = 1e6 + 10;
ll t, n, m, x[N];
inline void solve() {
cin >> n;
unordered_map<ll, ll>pos;
for(int i = 1; i <= n; i++) cin >> x[i], pos[x[i]] = i;
ll ans = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; x[i] * j < 2 * n; j++) {
ll k = x[i] * j;
if(i + pos[j] == k && i < pos[j]) ans++;
}
}
cout << ans << endl;
return ;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> t;
while(t--) {
solve();
}
return 0;
}
C. Great Graphs
后来补题额,忘了啥意思了这题,不补了