This problem is worth 50 points.
Let �A denote a permutation of length �N.
Define �(�)=∑�=1�min(��,��+1)f(A)=∑i=1Nmin(Ai,Ai+1), where ��+1=�1AN+1=A1.
Find the minimum value of �(�)f(A) over all permutations of length �N.
Note that a permutation of length �N consists of all integers from 11 to �N exactly once.
Input Format
- The first line of input will contain a single integer �T, denoting the number of test cases.
- Each test case consists of a single line of input containing �N, the size of the permutation.
Output Format
For each test case, output on a new line, the minimum value of �(�)f(A).
Note : The answer may not fit in a 32-bit integer. You are recommended to use 64-bit integers.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long long minFunction(vector<int>& perm) {
int N = perm.size();
long long result = 0;
for (int i = 0; i < N; ++i) {
result += min(perm[i], perm[(i + 1) % N]);
}
return result;
}
int main() {
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
vector<int> permutation(N);
for (int i = 0; i < N; ++i) {
permutation[i] = i + 1;
}
long long minVal = minFunction(permutation);
cout << minVal << endl;
}
return 0;
}
Leave a Reply