- P5657 [CSP-S2019] 格雷码
- P7076 [CSP-S2020] 动物园
- P7913 [CSP-S 2021] 廊桥分配
- P8817 [CSP-S 2022] 假期计划
暴力50分
cpp
复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define int long long
#define PII pair<int,int>
#define pb push_back
using namespace std;
const int N = 1e6+5;
int n,k;
signed main()
{
IOS;
cin>>n>>k;
vector<string> v;
v.pb("0");
v.pb("1");
for(int j=1;j<=n-1;j++)
{
vector<string> vt;
for(int i=0;i<(int)v.size();i++)
{
vt.pb("0"+v[i]);
}
for(int i=(int)v.size()-1;i>=0;i--)
{
vt.pb("1"+v[i]);
}
v = vt;
}
cout<<v[k];
}
正解
cpp
复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define int long long
#define PII pair<int,int>
#define pb push_back
using namespace std;
const int N = 1e6+5;
unsigned long long n,k;
signed main()
{
IOS;
cin>>n>>k;
__int128 sum = (__int128)pow(2,n);
int pre = -1;
while(1)
{
if(pre==-1)
{
if(k<=sum/2-1)
{
cout<<0;
pre = -1;
}
else
{
cout<<1;
pre = 1;
k -= sum/2;
}
}
else
{
if(k<=sum/2-1)//k下标从1开始
{
cout<<1;
pre = -1;
}
else
{
cout<<0;
pre = 1;
k -= sum/2;
}
}
sum /= 2;
if(sum==1) break;
}
}
暴力
cpp
复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define int long long
#define PII pair<int,int>
#define pb push_back
using namespace std;
const int N = 1e6+5;
int n,m,c,k;
int limit[N],va[N];
signed main()
{
IOS;
cin>>n>>m>>c>>k;
for(int i=1;i<=n;i++) cin>>va[i];
while(m--)
{
int a,b;
cin>>a>>b;
limit[a] = b;//动物编号第a为1就必须有b号饲料
}
set<int> s;
for(int i=1;i<=n;i++)//由已有动物来倒推我有哪些饲料
{
for(int j=0;j<=k-1;j++)
{
if((va[i]>>j)&1)//动物编号的二进制第j位为1
{
s.insert(limit[j]);//动物编号的二进制第j位为1,即需要limit[j]号饲料
}
}
}
int sum = 0;
for(int i=0;i<=pow(2,k)-1;i++)
{
int suc = 1;
for(int j=0;j<=k-1;j++)
{
if((i>>j)&1)
{
if(limit[j]==0) continue;
if(s.count(limit[j])) continue;
suc = 0;
}
}
if(suc==1) sum++;
}
cout<<sum-n;
}
正解
cpp
复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define int long long
#define PII pair<int,int>
#define pb push_back
using namespace std;
const int N = 1e6+5;
int n,m,c,k;
int limit[N],va[N];
bool vis[N];
signed main()
{
IOS;
cin>>n>>m>>c>>k;
for(int i=1;i<=n;i++) cin>>va[i];
while(m--)
{
int a,b;
cin>>a>>b;
limit[a] = b;//动物编号第a为1就必须有b号饲料
}
for(int j=0;j<=k-1;j++)
{
if(limit[j]==0) vis[j] = 1;
for(int i=1;i<=n;i++)
{
if((va[i]>>j)&1) vis[j] = 1;//由已有动物来倒推我哪些位置有哪些饲料
}
}
int sum = 0;
for(int i=0;i<=k-1;i++)
{
if(vis[i]==1) sum++;
}
if(sum==64)
{
if(n==0) cout<<"18446744073709551616";
else cout<<(1ull<<(sum-1))-n+(1ull<<(sum-1));
}
else cout<<(1ull<<sum)-n;
}
暴力
cpp
复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define int long long
#define PII pair<int,int>
#define pb push_back
using namespace std;
const int N = 1e6+5;
int n,m1,m2;
struct node{
int in;
int out;
}va[N],vb[N];
bool cmp(node a,node b)
{
return a.in<b.in;
}
int solve(int sum1,int sum2)
{
int ans = 0;
priority_queue <int,vector<int>,greater<int> > q1,q2;
for(int i=1;i<=m1;i++)
{
while(q1.size()&&q1.top()<=va[i].in) q1.pop();
if(q1.size()<sum1)
{
ans++;
q1.push(va[i].out);
}
}
for(int i=1;i<=m2;i++)
{
while(q2.size()&&q2.top()<=vb[i].in) q2.pop();
if(q2.size()<sum2)
{
ans++;
q2.push(vb[i].out);
}
}
return ans;
}
signed main()
{
IOS;
cin>>n>>m1>>m2;
int maxn = 0;
for(int i=1;i<=m1;i++)
{
cin>>va[i].in>>va[i].out;
}
for(int i=1;i<=m2;i++)
{
cin>>vb[i].in>>vb[i].out;
}
sort(va+1,va+1+m1,cmp);
sort(vb+1,vb+1+m2,cmp);
for(int i=0;i<=n;i++)
{
maxn = max(maxn,solve(i,n-i));
}
cout<<maxn;
}
正解
cpp
复制代码
#include<bits/stdc++.h>
#define PII pair<int,int>
#define fi first
#define se second
#define int long long
using namespace std;
const int N = 1e5+10;
int n,m1,m2;
PII va[N],vb[N];
int cnt1[N],cnt2[N];
priority_queue<PII,vector<PII>,greater<PII>> q1,q2;
int solve1()
{
for(int i=1;i<=n;i++) q1.push({0,i});
for(int i=1;i<=m1;i++)
{
int min_id = 1e9;
vector<PII > v;
while(q1.size()&&q1.top().fi<=va[i].fi)
{
v.push_back(q1.top());
if(min_id>q1.top().se) min_id = q1.top().se;
q1.pop();
}
for(auto t:v)
{
if(t.se==min_id) q1.push({va[i].se,t.se});
else q1.push({0,t.se});
}
if(min_id!=1e9) cnt1[min_id]++;
}
for(int i=1;i<=n;i++) cnt1[i] = cnt1[i]+cnt1[i-1];
}
int solve2()
{
for(int i=1;i<=n;i++) q2.push({0,i});
for(int i=1;i<=m2;i++)
{
int min_id = 1e9;
vector<PII > v;
while(q2.size()&&q2.top().fi<=va[i].fi)
{
v.push_back(q2.top());
if(min_id>q2.top().se) min_id = q2.top().se;
q2.pop();
}
for(auto t:v)
{
if(t.se==min_id) q2.push({va[i].se,t.se});
else q2.push({0,t.se});
}
if(min_id!=1e9) cnt2[min_id]++;
}
for(int i=1;i<=n;i++) cnt2[i] = cnt2[i]+cnt2[i-1];
}
signed main()
{
// freopen("w.in","r",stdin);
// freopen("ho.out","w",stdout);
cin>>n>>m1>>m2;
for(int i=1;i<=m1;i++) cin>>va[i].fi>>va[i].se;
for(int i=1;i<=m2;i++) cin>>vb[i].fi>>vb[i].se;
sort(va+1,va+1+m1);
sort(vb+1,vb+1+m2);
solve1();solve2();
int anw = 0;
for(int i=0;i<=n;i++) anw = max(anw,cnt1[i]+cnt2[n-i]);
cout<<anw;
return 0;
}