只有A,B,C,大佬饶命
题意:给你 n + 1个数,每个数 ai 在 [0,n)之间或者 n 的平方,给了你 s 是这个序列的和,问最多可以包含几个 n 的平方
思路:显而易见答案就是 s / (n*n),但是要注意数据范围
AC代码
#include<iostream>
#include<algorithm>
#include<cmath>
#include<deque>
#include<string>
#include<cstring>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<cstdio>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PII pair<int,int>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
ll t,n,s;
int main(){
cin >> t;
while(t--){
cin >> n >> s;
cout << s/(n * n) << endl;
}
return 0;
}
题意:给你 n 个数,你可以对其中的数进行染色,但是只能染成红色和蓝色,也可以不染,问是否可以让染成红色的数的总和比染成蓝色的数的总和大,但是被染成红色的数量必须比蓝色小
思路:贪心的想法,让染成红色的数尽可能的是相对于最大的数,而蓝色的是相对于最小的数,然后红色选 x 个数,蓝色选 x + 1个数,然后不断比较两个的总和是否满足条件即可
AC代码
#include<iostream>
#include<algorithm>
#include<cmath>
#include<deque>
#include<string>
#include<cstring>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<cstdio>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PII pair<int,int>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int maxn = 2e5+4;
ll a[maxn],sum[maxn];
int t,n;
void solve()
{
memset(sum,0,sizeof sum);
sort(a,a + 1 + n); //先排序
for(int i = 1 ; i <= n ; i++) sum[i] = sum[i - 1] + a[i]; //方便计算,求前缀和
int pos = n / 2 + 1; //蓝色最多能涂的个数
for(int i = 2 ; i <= pos ; i++){
if(sum[i] < sum[n] - sum[n - i + 1]){ // 不断地比较
puts("YES");
return;
}
}
puts("NO");
}
int main(){
cin >> t;
while(t--){
cin >> n;
for(int i = 1 ; i <= n ; i++) cin >> a[i];
solve();
}
return 0;
}
C. Factorials and Powers of Two
题意:给你一个数,是否可以通过 x ! 或者 2的次方之和得到,但是每个数必须不同,问最小需要多少个数 ?
思路:假如没有阶乘的话,答案就是唯一的,就是这个数的二进制数为 1 的个数,那么我确定了阶乘的数剩下的2的次方数就可以直接求得,所以先提前打好阶乘的表,然后dfs暴力做即可
AC代码
#include<iostream>
#include<algorithm>
#include<cmath>
#include<deque>
#include<string>
#include<cstring>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<cstdio>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PII pair<int,int>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
ll a[20];
ll t,n,ans;
ll cul(ll x) //计算2的次方数的个数
{
ll cnt = 0;
while(x){
int tmp = x % 2;
if(tmp) cnt++;
x /= 2;
}
return cnt;
}
void dfs(ll nn,ll k , ll s) //当前需要筹的数 , k的阶乘 , 已经用了的阶乘数
{
ans = min(ans,cul(nn) + s); // 又阶乘数和2的次方数得到
if(k <= 13){ // 14 !已经超过题的范围
dfs(nn, k + 1,s); // 继续阶乘
if(nn >= a[k]) dfs(nn - a[k],k + 1,s + 1); //确定当前阶乘,求余下的需要的筹的数、
}
}
int main(){
ll i = 2;
ll s = 2; //跳过1和2的阶乘
while(s <= 1e12){
i++;
s *= i;
a[i - 2] = s;
}
cin >> t;
while(t--){
ans = 1e12;
cin >> n;
dfs(n,1,0);
if(ans == 1e12) puts("-1");
else cout << ans << "\n";
}
return 0;
}