A题:Juggling Letters
题意:给定n个字符串,可任意交换不同字符串的任意位置,问是否可能使得n个字符串相等
Solution
将所有字母出现次数记录起来,当某个字母无法整除n时,即不可能存在操作使得n个字符串相等
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
ios::sync_with_stdio(0); cout.tie(0); cin.tie(0);
int t; cin >> t;
while (t--)
{
unordered_map<char, int>a;
int n; cin >> n;
for(int i=1;i<=n;i++)
{
string s; cin >> s;
for (auto c : s)a[c]++;
}
int p = 0;
for(auto x:a)
if (x.second % n)
{
p = 1;
break;
}
if (p == 1)cout << "NO" << endl;
else cout << "YES" << endl;
}
return 0;
}
B题:Power Sequence
题意:给定一个序列,问将该序列转化为一个a[1]=1,q为正整数的等比数列所需的最小代价
Solution
想了很久这个题,非常有意思的题目,首先将序列排序,可以证明这是最优的
同时考虑到随着q增大,花费一定是先减小后增大,若ans一旦变大,则break,同时考虑到n过大导致答案直接爆掉,所以当花费大于1e15时直接break(这是不可能用到的取值,且后面的底数可以不用考虑,因为随着底数增大,答案更加会超过1e15
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
int ans = 1e18;
int a[N];
signed main()
{
ios::sync_with_stdio(0); cout.tie(0); cin.tie(0);
int sum = 0;
int n; cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + 1 + n);
int sum0 = 0;
for (int i = 1; i <= n; i++)
{
sum0 += abs(a[i] - 1);
}
ans = sum0;
for (int i = 2; ; i++)
{
int sum1 = 0, p = 1;
for (int j = 1; j <= n; j++)
{
sum1 += abs(a[j] - p);
p *= i;
if (sum1 > 1e15)break;
}
if (sum1 > 1e15)break;
if (ans < sum1)
{
break;
}
else ans = sum1;
}
cout << ans << endl;
return 0;
}
C题:Multiples of Length
题意:给定一个序列,每次选择一个区间,对该区间内的数进行加减,每个数每次加减必须的该区间的倍数,可选择三次,可以证明三次操作一定能使得该序列全部为0,问每次选择的区间和对应加减的数
Solution
若n==1,则一次即可,后面加0就行
若n!=0,先选择前n-1个数,将前n-1个数加上(n-1)*a[i],再选择最后一个数加上(n-1)*a[n],使得序列的所有数字全部变为原先的n倍,最后选择整个区间,所有数字减去n*(a[i]),这样每个数字都成为了0
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e5 + 10;
int a[N];
signed main()
{
ios::sync_with_stdio(0); cout.tie(0); cin.tie(0);
int n; cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
if (n == 1)
{
cout << 1 << " " << 1 << endl << -a[1] << endl << 1 << " " << 1 << endl << 0 << " "
<< 1 << " " << 1 << endl << 0;
return 0;
}
cout << 1 << " " << n - 1 << endl;
for (int i = 1; i <= n - 1; i++)cout << (n - 1) * a[i] << " ";
cout << endl;
cout << n << " " << n << endl;
cout << (n - 1) * a[n] << endl;
cout << 1 << " " << n << endl;
for (int i = 1; i <= n; i++)cout << -n * a[i] << " ";
return 0;
}
D题:Stoned Game
题意:n堆石子,每次只能取一个且不能和上一次取的重叠,HL先取,T后取,问谁能赢
Solution
可以知道,当某一堆石子超过另外所有之和时,HL先手必胜,当没有时,后面也必然不会有了,此时当石子为偶数个时,HL必输,否则必赢
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e5 + 10;
signed main()
{
ios::sync_with_stdio(0); cout.tie(0); cin.tie(0);
int t; cin >> t;
while (t--)
{
int n; cin >> n;
int sum = 0;
int max1 = 0;
for (int i = 1; i <= n; i++)
{
int x; cin >> x;
max1 = max(max1, x);
sum += x;
}
if (max1 > sum - max1)
cout << "T" << endl;
else if (sum % 2)cout << "T" << endl;
else cout << "HL" << endl;
}
return 0;
}