2021年CSP-S复赛总结
2021年CSP-S复赛总结
T1(廊桥分配):
题意解析:\ 给出飞机的到达、离开时间以及廊桥数量,在保证“先到先得”的方式下求出最多能使多少架飞机停在廊桥。\ 思路:\ 枚举国内、外航班廊桥的数量,然后用类似于“桶”的做法标记每个时间有多少架飞机停在廊桥,若小于该廊桥总数则可以让飞机停在廊桥。\ 代码如下(25分):
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int n,mx,my,ans,bmax;
struct Plane{
int a,b;
bool in;
}pla[100005];
bool cmp(Plane X,Plane Y){
return X.a<Y.a;
}
int main(){
cin>>n>>mx>>my;
for(int i=1;i<=mx+my;i++){
cin>>pla[i].a>>pla[i].b;
bmax=max(pla[i].b,bmax);
pla[i].in=(i<=mx);
}
sort(pla+1,pla+mx+my+1,cmp);
for(int t1=0;t1<=n;t1++){
int t2=n-t1,maxn=0;
int t_in[bmax+5];
int t_out[bmax+5];
fill(t_in+1,t_in+bmax+5,0);
fill(t_out+1,t_out+bmax+5,0);
for(int i=1;i<=mx+my;i++){
if(pla[i].in){
if(t_in[pla[i].a]<t1){
maxn++;
for(int j=pla[i].a;j<=pla[i].b;j++){
t_in[j]++;
}
}
}else{
if(t_out[pla[i].a]<t2){
maxn++;
for(int j=pla[i].a;j<=pla[i].b;j++){
t_out[j]++;
}
}
}
}
if(maxn>ans)ans=maxn;
}
cout<<ans;
return 0;
}
T2(括号序列):
题意解析:\
给出一个含有问号的字符串,问在将所有问号变为 ( , ) ,* 之一后,有多少种方案使得最后的字符串是一个“超级字符串”。\
思路:\
枚举所有变换后的字符串,然后判定是否是“超级字符串”。\
但是最后好像对于字符串的判定有点问题。\
代码如下(0分):
#include<iostream>
#include<stack>
using namespace std;
#define int long long
int n,k,ans;
string s;
bool judge(string a){
if(a.size()<n)return false;
stack<char>st1;
stack<char>st2;
for(int i=a.size()-1;i>=0;i--){
st1.push(a[i]);
}
bool flag=false;
int num=0;
do{
flag=false;
while(!st1.empty()){
if(st1.top()=='*'){
if(st2.empty()||(st2.top()!='('&&st2.top()!=')'))return false;
num++;
if(num>k)return false;
flag=true;
st1.pop();
}else{
num=0;
if(!st1.empty()&&!st2.empty()&&st1.top()==')'&&st2.top()=='('){
st1.pop();
st2.pop();
}
if(!st1.empty())st2.push(st1.top()),st1.pop();
}
}
while(!st2.empty()){
st1.push(st2.top());
st2.pop();
}
}while(flag);
if(!st1.size())return true;
return false;
}
void dfs(string str,int num){
if(num>=n){
if(judge(str))ans++;
return;
}
if(s[num]=='?'){
dfs(str+'(',num+1);
dfs(str+')',num+1);
dfs(str+'*',num+1);
}else dfs(str+s[num],num+1);
return;
}
signed main(){
cin>>n>>k;
cin>>s;
dfs("",0);
cout<<ans;
return 0;
}
T3(回文):
题意解析:\
对一个字符串
#include<iostream>
#include<cstring>
#include<deque>
using namespace std;
int t,n;
bool flag;
bool judge(string s){
for(int i=0,j=2*n-1;i<n;i++,j--){
if(s[i]!=s[j])return false;
}
return true;
}
void dfs(int num,deque<char>a,deque<char>b,string ans){
if(flag)return;
if(num==2*n){
string s="";
deque<char>c=b;
while(!c.empty()){
s.push_back(c.front());
c.pop_front();
}
if(judge(s)){
cout<<ans<<"\n";
flag=true;
}
return;
}
b.push_back(a.front());
a.pop_front();
dfs(num+1,a,b,ans+'L');
a.push_front(b.back());
b.pop_back();
b.push_back(a.back());
a.pop_back();
dfs(num+1,a,b,ans+'R');
a.push_back(b.back());
b.pop_back();
return;
}
int main(){
cin>>t;
while(t--){
cin>>n;
deque<char>a;
deque<char>b;
while(!a.empty())a.pop_front();
while(!b.empty())b.pop_front();
flag=false;
for(int i=1;i<=2*n;i++){
char x;cin>>x;
a.push_back(x);
}
dfs(0,a,b,"");
if(!flag)cout<<"-1\n";
}
}
T4(交通规划):
没写(直接 cout )。