A. Pretty Permutations
There are n n n cats in a line, labeled from 1 1 1 to n n n, with the i i i-th cat at position i i i. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.
For example, if there are 3 3 3 cats, this is a valid reordering: [ 3 , 1 , 2 ] [3, 1, 2] [3,1,2]. No cat is in its original position. The total distance the cats move is 1 + 1 + 2 = 4 1 + 1 + 2 = 4 1+1+2=4 as cat 1 1 1 moves one place to the right, cat 2 2 2 moves one place to the right, and cat 3 3 3 moves two places to the left.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 1≤t≤100) — the number of test cases. Then t t t test cases follow.
The first and only line of each test case contains one integer n n n ( 2 ≤ n ≤ 100 2 \leq n \leq 100 2≤n≤100) — the number of cats.
It can be proven that under the constraints of the problem, an answer always exist.
Output
Output t t t answers, one for each test case. Each answer consists of n n n integers — a permutation with the minimum total distance. If there are multiple answers, print any.
Example
input
2
2
3
output
2 1
3 1 2
Note
For the first test case, there is only one possible permutation that satisfies the conditions: [ 2 , 1 ] [2, 1] [2,1].
The second test case was described in the statement. Another possible answer is [ 2 , 3 , 1 ] [2, 3, 1] [2,3,1].
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
if(n%2==0){
for(int i=1; i<n; ){
cout << i+1 <<" " << i <<" ";
i += 2;
}
cout << endl;
}
else{
for(int i=1; i<=n-3; ){
cout << i+1 <<" " << i <<" ";
i += 2;
}
cout << n << " " << n-2 << " " << n-1 << " " ;
cout << endl;
}
}
return 0;
}
B. Pleasant Pairs
You are given an array a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,…,an consisting of n n n distinct integers. Count the number of pairs of indices ( i , j ) (i, j) (i,j) such that i < j i < j i<j and a i ⋅ a j = i + j a_i \cdot a_j = i + j ai⋅aj=i+j.
Input
The first line contains one integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1≤t≤104) — the number of test cases. Then t t t cases follow.
The first line of each test case contains one integer n n n ( 2 ≤ n ≤ 1 0 5 2 \leq n \leq 10^5 2≤n≤105) — the length of array a a a.
The second line of each test case contains n n n space separated integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 1 ≤ a i ≤ 2 ⋅ n 1 \leq a_i \leq 2 \cdot n 1≤ai≤2⋅n) — the array a a a. It is guaranteed that all elements are distinct.
It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105.
Output
For each test case, output the number of pairs of indices ( i , j ) (i, j) (i,j) such that i < j i < j i<j and a i ⋅ a j = i + j a_i \cdot a_j = i + j ai⋅aj=i+j.
Example
input
3
2
3 1
3
6 1 5
5
3 1 5 9 2
output
1
1
3
Note
For the first test case, the only pair that satisfies the constraints is ( 1 , 2 ) (1, 2) (1,2), as a 1 ⋅ a 2 = 1 + 2 = 3 a_1 \cdot a_2 = 1 + 2 = 3 a1⋅a2=1+2=3
For the second test case, the only pair that satisfies the constraints is ( 2 , 3 ) (2, 3) (2,3).
For the third test case, the pairs that satisfy the constraints are ( 1 , 2 ) (1, 2) (1,2), ( 1 , 5 ) (1, 5) (1,5), and ( 2 , 3 ) (2, 3) (2,3).
分析
a[i] * a[j] ,根据 a[i] 来找 a[j] , i + j 一定是 a[i] 的倍数,i 是不变的,所以 j 的增加量一定是a[i] 的倍数。
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
ll a[N];
int main(){
int T;
cin >> T;
while(T--){
int n;
cin >> n;
ll ans = 0;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 1; i <= n; i++)
for(int j = a[i] - i; j <= n; j += a[i]){
if(j <= i)
continue;
if(a[i] * a[j] == i + j)
ans++;
}
cout << ans << endl;
}
return 0;
}
C. Great Graphs
Farmer John has a farm that consists of n n n pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.
Unfortunately, Farmer John lost the map of the farm. All he remembers is an array d d d, where d i d_i di is the smallest amount of time it took the cows to reach the i i i-th pasture from pasture 1 1 1 using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory.
Input
The first line contains one integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1≤t≤104) — the number of test cases. Then t t t cases follow.
The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1≤n≤105) — the number of pastures.
The second line of each test case contains n n n space separated integers d 1 , d 2 , … , d n d_1, d_2, \ldots, d_n d1,d2,…,dn ( 0 ≤ d i ≤ 1 0 9 0 \leq d_i \leq 10^9 0≤di≤109) — the array d d d. It is guaranteed that d 1 = 0 d_1 = 0 d1=0.
It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 5 10^5 105.
Output
For each test case, output the minimum possible cost of a farm that is consistent with Farmer John’s memory.
Example
input
3
3
0 2 3
2
0 1000000000
1
0
output
-3
0
0
Note
In the first test case, you can add roads
- from pasture 1 1 1 to pasture 2 2 2 with a time of 2 2 2,
- from pasture 2 2 2 to pasture 3 3 3 with a time of 1 1 1,
- from pasture 3 3 3 to pasture 1 1 1 with a time of − 3 -3 −3,
- from pasture 3 3 3 to pasture 2 2 2 with a time of − 1 -1 −1,
- from pasture 2 2 2 to pasture 1 1 1 with a time of − 2 -2 −2.
The total cost is 2 + 1 + − 3 + − 1 + − 2 = − 3 2 + 1 + -3 + -1 + -2 = -3 2+1+−3+−1+−2=−3.
In the second test case, you can add a road from pasture 1 1 1 to pasture 2 2 2 with cost 1000000000 1000000000 1000000000 and a road from pasture 2 2 2 to pasture 1 1 1 with cost − 1000000000 -1000000000 −1000000000. The total cost is 1000000000 + − 1000000000 = 0 1000000000 + -1000000000 = 0 1000000000+−1000000000=0.
In the third test case, you can’t add any roads. The total cost is 0 0 0.
分析
相当于负向的边全部加上,正向只在相邻pasture加边
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1E5 + 10;
int a[N];
ll sum,ans;
int main(){
int T;
cin >> T;
while(T--){
int n;
cin >> n;
sum = ans = 0;
for(int i=0; i<n; i++){
cin >> a[i];
sum += a[i];
}
sort(a, a+n); // 由小到大排序
ans = sum;
for(int i=1; i<n-1; i++){
sum -= 1ll*(n-i)*(a[i]-a[i-1]);
ans += sum;
}
ans -= a[n-1];
cout << -ans <<endl;
}
return 0;
}