A. Square Counting
题意:给你一个长度为 n n n 的序列,给出总和 s s s ,问其中最多能含多少个 n 2 n^2 n2
做法:直接输出 s / n 2 s / n ^2 s/n2 即可,因为序列元素可以是0
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
const int mod = 1e9 + 7;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--)
{
LL n, s;
cin >> n >> s;
cout << s / (n * n) << '\n';
}
return 0;
}
B. Quality vs Quantity
题意:给一个序列染色,问是否存在红的个数大于蓝的但是蓝的总和大于红的
做法:排序+贪心,从两端开始往中间加,如果存在合法情况就输出
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
const int mod = 1e9 + 7;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--)
{
int n;;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a.begin(), a.end());
LL sum1 = a[1], sum2 = 0;
int l = 2, r = n;
bool flag = false;
while (l < r)
{
sum1 += a[l];
sum2 += a[r];
l++, r--;
if (sum2 > sum1)
{
flag = true;
break;
}
}
if (flag) puts("YES");
else puts("NO");
}
return 0;
}
C. Factorials and Powers of Two
题意:2的次方数和阶乘数都被看做好数,给你一个数问最少能拆分出多少个不同的好数
做法:先用二进制枚举阶乘的所有组合方案,然后再用2的次方数去补即可
解释:首先不存在不可能的方案,因为每一个数都可以用二进制表示,所以大不了就统计二进制中的1的个数,为什么要枚举阶乘的组合方案呢,我们通过二进制来看,一个2的次方数可以消掉一个1,但是一个阶乘数可以消掉好多1,所以我们枚举阶乘的组合方案,找到减去阶乘组合后能最少剩下多少个1,再用2的次方数去补,这样的方法拆分必然是最少的
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 1e5 + 10;
const LL Max = 1e12 + 10;
LL n, t, len, temp, cnt;
LL ans = 999;
inline LL lowbit(LL a)
{
return a & -a;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
vector<LL> a;
LL x = 1;
for(LL i = 2; x < Max; i++)
{
x *= i;
a.push_back(x);
}
len = a.size();
cin >> t;
while(t--) {
cin >> n;
ans = 999;
for(LL i = 0; i < 1ll << len; i ++)
{
temp = 0, cnt = 0;
for (int j = 0; j < len; j ++)
if(i >> j & 1)
temp += a[j], cnt ++;
temp = n - temp;
if(0 > temp) continue;
while(temp) {
temp -= lowbit(temp);
cnt ++;
}
ans = min(ans, cnt);
}
cout << ans << '\n';
}
return 0;
}