目录
A. Square Counting【贪心】
#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
typedef pair<int,int> PII;
const int N=1e5*2+10;
const int mod=1e9+7;
LL t;
LL a[N];
int main(void)
{
cin>>t;
while(t--)
{
LL n,s; cin>>n>>s;
cout<<s/(n*n)<<endl;
}
return 0;
}
B. Quality vs Quantity【贪心 / 枚举】
就是贪心,red选大的,blue选小的。蓝的始终比红的多选一个。
做的时候像歪了,以为是红蓝分割选完,实际上是可以不用选完的。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5*4+10;
typedef long long int LL;
LL t,n,a[N];
bool cmp(int a,int b){return a>b;}
int main(void)
{
cin>>t;
while(t--)
{
cin>>n;
LL s[N]={0};
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+n+1,cmp);
int flag=0;
for(int i=1;i<=n;i++)s[i]=s[i-1]+a[i];
int m=n/2;
if(n%2==0) m--;
for(int i=1;i<=m;i++)//最多选
{
LL s1=s[i],s2=s[n]-s[n-i-1];//选i个 另一个选i+1个
if(s1>s2) flag=1;
}
if(flag) puts("YES");
else puts("NO");
}
return 0;
}
C. Factorials and Powers of Two【爆搜 / 二进制枚举】
注意看清楚题目,题目上说选的数是不同的,即不会某一个数字选了多次。
2的幂次方的数+阶乘的数很多。
通过分析你会发现我们直接枚举阶乘的数
是选还是不选,剩余的数字必定可以由二的幂次方组成。
爆搜
#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
LL lowbit(LL x){return x&-x;}
vector<LL>ve;
void init()
{
for(LL i=1,j=1;i<=15;i++)
j=j*i,ve.push_back(j);
}
LL t,n,ans;
void dfs(int index,int cnt,LL u)
{
if(index==ve.size())
{
if(u>n) return;
LL t=0,temp=n-u;
while(temp) temp-=lowbit(temp),t++;//计算二进制1的个数
ans=min(ans,cnt+t);
return;
}
dfs(index+1,cnt+1,u+ve[index]);//选
dfs(index+1,cnt,u);//不选
}
int main(void)
{
cin>>t;
init();
while(t--)
{
cin>>n;
ans=1e9;
dfs(0,0,0);
cout<<ans<<endl;
}
return 0;
}
二进制枚举
#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
LL lowbit(LL x){return x&-x;}
vector<LL>ve;
void init(){for(LL i=1,j=1;i<=15;i++) j=j*i,ve.push_back(j);}
LL t,n,ans;
int main(void)
{
cin>>t;
init();
while(t--)
{
cin>>n;
ans=1e9;
for(int i=0;i<(1<<15);i++)
{
LL temp=0,cnt=0;
for(int j=0;j<15;j++)
if(i>>j&1) temp+=ve[j],cnt++;
if(temp>n) continue;
temp=n-temp;
while(temp) temp-=lowbit(temp),cnt++;
ans=min(ans,cnt);
}
cout<<ans<<endl;
}
return 0;
}