QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#944527 | #10158. Forming Groups | ElevenX | WA | 163ms | 3712kb | C++20 | 2.8kb | 2025-03-20 14:16:30 | 2025-03-20 14:16:31 |
Judging History
answer
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <climits>
#include <cmath>
using namespace std;
// Function to compute the greatest common divisor (GCD)
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// Function to generate all divisors of n greater than 1
vector<int> getDivisors(int n) {
vector<int> divisors;
for (int i = 2; i <= sqrt(n); ++i) {
if (n % i == 0) {
divisors.push_back(i);
if (i != n / i) {
divisors.push_back(n / i);
}
}
}
divisors.push_back(n);
sort(divisors.begin(), divisors.end());
return divisors;
}
void solve() {
int t;
cin >> t;
while (t--) {
int n, a1;
cin >> n >> a1;
vector<int> a(n - 1);
for (int i = 0; i < n - 1; ++i) {
cin >> a[i];
}
// Generate all possible k values (divisors of n)
vector<int> divisors = getDivisors(n);
// Initialize the minimum ratio to a large value
int min_p = INT_MAX, min_q = 1;
// Simulate each possible position for the captain
for (int pos = 0; pos <= n; ++pos) {
// Create the full skill array with a1 in the chosen position
vector<int> full_a;
for (int i = 0; i < pos; ++i) {
full_a.push_back(a[i]);
}
full_a.push_back(a1);
for (int i = pos; i < n - 1; ++i) {
full_a.push_back(a[i]);
}
// For each divisor k
for (int k : divisors) {
// Calculate the sum of skills for each group
vector<int> group_sums(k, 0);
for (int i = 0; i < n; ++i) {
group_sums[i % k] += full_a[i];
}
// Find the maximum and minimum group sums
int x_max = *max_element(group_sums.begin(), group_sums.end());
int x_min = *min_element(group_sums.begin(), group_sums.end());
// Calculate the ratio
int p = x_max, q = x_min;
int common_divisor = gcd(p, q);
p /= common_divisor;
q /= common_divisor;
// Update the minimum ratio
if ((long long)p * min_q < (long long)q * min_p) {
min_p = p;
min_q = q;
}
}
}
// Output the result
cout << min_p << " " << min_q << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3712kb
input:
2 4 1 2 1 2 3 10 4 3
output:
1 1 10 3
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 163ms
memory: 3456kb
input:
100000 2 1 1 2 1 2 2 1 500 2 1 999 2 1 1000 2 1 549 2 2 1 2 2 2 2 2 500 2 2 999 2 2 1000 2 2 593 2 500 1 2 500 2 2 500 500 2 500 999 2 500 1000 2 500 715 2 999 1 2 999 2 2 999 500 2 999 999 2 999 1000 2 999 843 2 1000 1 2 1000 2 2 1000 500 2 1000 999 2 1000 1000 2 1000 603 2 857 1 2 857 2 2 857 500 ...
output:
1 1 2 1 25037 500 25037 999 25037 1000 25037 549 2 1 1 1 25037 500 25037 999 25037 1000 25037 593 500 1 250 1 1 1 999 500 2 1 143 100 999 1 999 2 999 500 1 1 1000 999 333 281 1000 1 500 1 2 1 1000 999 1 1 1000 603 857 1 857 2 857 500 999 857 1000 857 857 545 1 1 2 1 500 1 999 1 1000 1 846 1 2 1 2 1 ...
result:
wrong answer 3rd lines differ - expected: '500 1', found: '25037 500'