A题:秒杀题没什么好说的
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long,long long> pll;
template<class T>inline void rd(T &x){x=0;char o,f=1;while(o=getchar(),o<48)if(o==45)f=-f;do x=(x<<3)+(x<<1)+(o^48);while(o=getchar(),o>47);x*=f;}
const int inf=~0u>>2; //1073741823
const ll INF=~0ull>>2;//4611686018427387903
const int maxn=1e3+10;
int n,x,a[maxn],y;
void solve()
{
rd(n),rd(x);
for(int i=0;i<=100;i++) a[i]=0;
for(int i=0;i<n;i++)
{
rd(y);
a[y]=1;
}
for(int i=1;i<=300;i++)
{
if(a[i]==0)
{
if(x>0)
{
x--;
}
else
{
cout<<i-1<<endl;
return;
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("hzh.in","r",stdin);
//freopen("hzh.out","w",stdout);
#endif
ll T;
rd(T);
while(T--)
solve();
return 0;
}
B题:
1.注意sum开long long,因为cf几乎不会卡内存限制,
所以最好全部开ll,以防止出现这种难以找到错误的bug
2.注重对stl的使用,这道题里熟练运用set能极大简化代码
思路就是暴力
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long,long long> pll;
template<class T>inline void rd(T &x){x=0;char o,f=1;while(o=getchar(),o<48)if(o==45)f=-f;do x=(x<<3)+(x<<1)+(o^48);while(o=getchar(),o>47);x*=f;}
const int inf=~0u>>2; //1073741823
const ll INF=~0ull>>2;//4611686018427387903
const int maxn=200000+10;
ll n,a[maxn];
bool judge(ll i,ll sum)
{
if(sum==(1+i)*i/2) return true;
else return false;
}
void solve()
{
rd(n);
map<int,int>m;
queue<int>ans;
set<int>s1,s2;
ll sum1=0,sum2=0;
for(int i=1;i<=n;i++)
{
rd(a[i]);
m[a[i]]++;
s2.insert(a[i]);
sum2+=a[i];
}
for(ll i=1;i<n;i++)
{
s1.insert(a[i]);
sum2-=a[i];
sum1+=a[i];
m[a[i]]--;
if(m[a[i]]==0) s2.erase(a[i]);
if(judge(i,sum1)&&judge(n-i,sum2)&&s1.size()==i&&s2.size()==n-i) ans.push(i);
}
cout<<ans.size()<<endl;
while(!ans.empty())
{
int i=ans.front();
ans.pop();
cout<<i<<' '<<n-i<<endl;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("hzh.in","r",stdin);
//freopen("hzh.out","w",stdout);
#endif
ll T;
rd(T);
while(T--)
solve();
return 0;
}
C题
一道想到思路就能秒杀的题
很容易发现,第i个数至少是在第i格,那么他染色的终点至少是在第i+l[i]-1,如果这个数超出n,不符题意,输出-1;
用一个sum来保存到目前这个数为止,接下来要染的总格数(包含重复的)
如果sum[1]小于n,代表无论如何都无法染色到底,所以,输出-1;
接着就是这道题的核心公式max(i,n-sum[i]+1);
当n-sum[i]+1>=i时,(其实看成n-(i-1)>=sum[i]更好理解)
后面的每一个都只要刷到最大长度即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long,long long> pll;
template<class T>inline void rd(T &x){x=0;char o,f=1;while(o=getchar(),o<48)if(o==45)f=-f;do x=(x<<3)+(x<<1)+(o^48);while(o=getchar(),o>47);x*=f;}
const int inf=~0u>>2; //1073741823
const ll INF=~0ull>>2;//4611686018427387903
const int maxn=100000+10;
ll n,m,l[maxn],sum[maxn];
void solve()
{
rd(n),rd(m);
for(ll i=1;i<=m;i++)
{
rd(l[i]);
if(l[i]+i-1>n) {cout<<-1;return;}
}
for(ll i=m;i>=1;i--) sum[i]=sum[i+1]+l[i];
if(sum[1]<n){cout<<-1;return;}
for(ll i=1;i<=m;i++) cout<<max(i,n-sum[i]+1)<<' ';
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("hzh.in","r",stdin);
//freopen("hzh.out","w",stdout);
#endif
solve();
return 0;
}
D题
bi比bi-1大,ai比ai-1大,
所以a和b两个数组都满足后一个元素比前一个元素二进制数多一位1
以d=3为例
d=3的时候,3包含20,21,对于20,可以取这一位为1或者不取共两种,
对于21,你可以取010,011,或者不取,最后再减去全部不取的情况;
所以共有ans=(20+1)*(21+1)-1=5。
以此类推即可得出答案。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long,long long> pll;
template<class T>inline void rd(T &x){x=0;char o,f=1;while(o=getchar(),o<48)if(o==45)f=-f;do x=(x<<3)+(x<<1)+(o^48);while(o=getchar(),o>47);x*=f;}
const int inf=~0u>>2; //1073741823
const ll INF=~0ull>>2;//4611686018427387903
const int maxn=100000+10;
ll d,m;
void solve()
{
rd(d),rd(m);
ll ans=1,t=1;
while(t<=d)
{
ans=ans*(min(t,d-t+1)+1)%m;
t<<=1;
}
cout<<(ans+m-1)%m<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("hzh.in","r",stdin);
//freopen("hzh.out","w",stdout);
#endif
int t;
rd(t);
while(t--)
solve();
return 0;
}
本文深入解析了四道ACM竞赛题目,涵盖了秒杀题、暴力解法、染色问题和二进制位操作,提供了详细的代码实现和解题思路,适合算法初学者和竞赛选手学习。
1626





