题目链接:
2022年江西理工大学软件工程学院程序设计竞赛_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ (nowcoder.com)
A.Hello World!
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
#include<map>
using namespace std;
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
cout<<"Hello World!";
return 0;
}
B.数学问题,数据量变小了好多
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
#include<map>
using namespace std;
const int mod=1e9+7;
int a[200200000];
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
int n;
cin>>n;
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
for(int i=4;i<=n;i++)
a[i]=(a[i-1]+a[i-3])%mod;
cout<<a[n]%mod<<endl;
return 0;
}
C.找找规律,心里画个图,就有了
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
#include<map>
using namespace std;
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
int n;
cin>>n;
if(n==2||n==3||n==4) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
return 0;
}
D.哪场比赛最先AK,记录一下
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
int n;
cin>>n;
string a;
bool flag=false;
for(int i=1;i<=n;i++)
{
string s;
cin>>s;
int len=s.size();
if(s[len-2]=='A'&&s[len-1]=='K'&&flag==false)
flag=true,a=s;
s.clear();
}
for(int i=0;i<a.size()-2;i++)
cout<<a[i];
return 0;
}
E.斩杀敌人,我用bfs写了,一开始太暴力了让它全跑一下结果tle了,想了一下,只需要记录它没走过的点就行,因为只要是走过的都已经算过了
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef pair<int,int> PII;
int n,m;
char a[2002][2002];
int dx[]={-1,0,0,1},dy[]={0,1,-1,0};
int vis[2002][2002];
int bfs(int x,int y)
{
queue<PII> q;
vis[x][y]=1;
int res=1;
q.push({x,y});
while(q.size())
{
auto t=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int xx=dx[i]+t.first,yy=dy[i]+t.second;
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&vis[xx][yy]==0&&a[xx][yy]=='*')
{
res++;
vis[xx][yy]=1;
q.push({xx,yy});
}
}
}
return res;
}
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
memset(vis,0,sizeof vis);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
int maxn=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]=='*'&&vis[i][j]==0)
{
maxn=max(bfs(i,j),maxn);
}
}
}
cout<<maxn<<endl;
return 0;
}
F.去重。to_string
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
#include<map>
using namespace std;
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
int t;
cin>>t;
map<string,int> mp;
int sum=0;
while(t--)
{
int n;
cin>>n;
string s=to_string(n);
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
s+=to_string(x);
}
mp[s]++;
if(mp[s]==1) sum++;
s.clear();
}
cout<<sum<<endl;
return 0;
}
G .二分
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
int a[200200];
int n,k;
int sum;
bool check(int x)
{
LL res=0;
for(int i=1;i<=n;i++)
if(a[i]<x) res+=x-a[i];
if(res<=k) return true;
else return false;
}
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
int l=1,r=1e9;
while(l<=r)
{
LL mid=l+r>>1;
if(check(mid))
{
l=mid+1;
sum=mid;
}
else r=mid-1;
}
cout<<sum<<endl;
return 0;
}
H.日期差,除了闰年平年判断一下,时间长的直接压缩就行了
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
typedef long long LL;
int main()
{
//cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
LL run=393,ping=392;
map<LL,LL> mp;
mp[1]=40;
mp[2]=28;
mp[3]=31;
mp[4]=30;
mp[5]=31;
mp[6]=30;
mp[7]=40;
mp[8]=31;
mp[9]=30;
mp[10]=40;
mp[11]=30;
mp[12]=31;
LL a,b,c,i,j,k;
char s,m,n,o;
//cin>>a>>s>>b>>m>>c; cin>>i>>n>>j>>o>>k;
scanf("%lld %c %lld %c %lld",&a,&s,&b,&m,&c);
scanf("%lld %c %lld %c %lld",&i,&n,&j,&o,&k);
//cout<<a<<s<<b<<m<<c<<" "<<i<<n<<j<<o<<k<<endl;
if(a>i||(a==i&&b>j)||(a==i&&b==j&&c>k)) swap(a,i),swap(b,j),swap(c,k);
LL sum=0;
if(a!=i)
{
for(LL t=b;t<=12;t++)
{
if(t!=b)
{
sum+=mp[t];
if(t==2&&(a%400==0||(a%100!=0&&a%4==0))) sum++;
}
else
{
sum+=mp[t]-c;
if(t==2&&(a%400==0||(a%100!=0&&a%4==0))) sum++;
}
}
//cout<<sum<<endl;
for(LL t=1;t<=j;t++)
{
if(t!=j)
{
sum+=mp[t];
if(t==2&&(i%400==0||(i%100!=0&&i%4==0))) sum++;
}
else sum+=k;
}
//cout<<sum<<endl;
/*for(LL t=a+1;t<=i-1;t++)//求两个日期之间有多少个闰年?
{
if(t%400==0||(t%100!=0&&t%4==0))
{
flag++;
}
}*/
LL be=a+1,eg=i-1;
LL be4=(be-1)/4;
LL be100=(be-1)/100;
LL be400=(be-1)/400;
LL eg4=eg/4;
LL eg100=eg/100;
LL eg400=eg/400;
LL flag=eg4-eg100+eg400-(be4-be100+be400);
sum+=392*((i-1)-(a+1)+1)+flag;
}
else
{
if(b==j) sum=abs(k-c);
else {
for(LL t=b;t<=j;t++)
{
if(t==b)
{
sum+=mp[t]-c;
if(t==2&&(i%400==0||(i%100!=0&&i%4==0))) sum++;
}
else if(t==j) sum+=k;
else
{
sum+=mp[t];
if(t==2&&(i%400==0||(i%100!=0&&i%4==0))) sum++;
}
}
}
}
printf("%lld\n",sum);
return 0;
}
I.看毒气跑的时间跟C跑的时间,拉出边缘来对比一下
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
#include<queue>
using namespace std;
typedef pair<int,int> PII;
int n,m;
char a[2002][2002];
int x,y;
int dist[2002][2002];
int dist2[2002][2002];
int dx[]={-1,0,0,1},dy[]={0,1,-1,0};
queue<PII> q;
int bfs()
{
while(q.size())
{
auto t=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int xx=dx[i]+t.first,yy=dy[i]+t.second;
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&dist[xx][yy]==-1&&a[xx][yy]!='#')
{
dist[xx][yy]=dist[t.first][t.second]+1;
q.push({xx,yy});
}
}
}
return dist[n][m];
}
int bfs2()
{
memset(dist2,-1,sizeof dist2);
queue<PII> qq;
dist2[x][y]=0;
qq.push({x,y});
while(qq.size())
{
auto t=qq.front();
qq.pop();
for(int i=0;i<4;i++)
{
int xx=dx[i]+t.first,yy=dy[i]+t.second;
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&dist2[xx][yy]==-1&&a[xx][yy]!='#')
{
dist2[xx][yy]=dist2[t.first][t.second]+1;
qq.push({xx,yy});
}
}
}
return dist2[n][m];
}
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
cin>>n>>m;
memset(dist,-1,sizeof dist);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
if(a[i][j]=='C') x=i,y=j;
else if(a[i][j]=='*')
{
dist[i][j]=0;
q.push({i,j});
}
}
}
bfs();
/*for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cout<<dist[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;*/
bfs2();
/*for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cout<<dist2[i][j]<<" ";
}
cout<<endl;
}*/
int minn=0x3f3f3f3f;
for(int i=1;i<=n;i++)
{
if(dist2[i][1]!=-1&&dist2[i][1]<dist[i][1]) minn=min(minn,dist2[i][1]);
if(dist2[i][m]!=-1&&dist2[i][m]<dist[i][m]) minn=min(minn,dist2[i][m]);
}
for(int i=1;i<=m;i++)
{
if(dist2[n][i]!=-1&&dist2[n][i]<dist[n][i]) minn=min(minn,dist2[n][i]);
if(dist2[1][i]!=-1&&dist2[1][i]<dist[1][i]) minn=min(minn,dist2[1][i]);
}
if(minn==0x3f3f3f3f) cout<<"NO"<<endl;
else cout<<minn+1<<endl;
return 0;
}
J.密码问题。不少于3种字符
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
map<int,int> mp;
int check(char op)
{
if(op>='A'&&op<='Z') return 1;
else if(op>='a'&&op<='z') return 2;
else if(op>='0'&&op<='9') return 3;
else return 4;
}
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
int n,l,r;
cin>>n>>l>>r;
string s;
cin>>s;
int sum=0;
for(int i=0,j=0;j<s.size();j++)
{
mp[check(s[j])]++;
if(j-i+1<l) continue;
if(j-i+1>r) mp[check(s[i++])]--;
if((mp[1]&&mp[2]&&mp[3])||(mp[1]&&mp[3]&&mp[4])||(mp[1]&&mp[2]&&mp[4])||(mp[2]&&mp[3]&&mp[4]))
{
sum+=min(r-l+1,min(r+i,n)-j);
mp[check(s[i++])]--;
mp[check(s[j--])]--;
}
}
cout<<sum<<endl;
return 0;
}
K .蓄意轰拳
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
#include<queue>
using namespace std;
typedef long long LL;
LL n;
LL a[2002000],b[2002000];
LL aa[2002000],bb[2002000];
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
LL n;
cin>>n;
for(LL i=1;i<=n;i++)
cin>>a[i];
memcpy(b,a,sizeof b);
/*for(LL i=1;i<=n;i++)
cout<<b[i]<<" ";
cout<<endl;*/
for(LL i=1;i<=n;i++)
{
if(a[i]>0){
aa[i]=aa[i-1]+a[i];
a[i+1]-=a[i];
a[i+2]-=a[i];
}
else aa[i]=aa[i-1];
//cout<<aa[i]<<" ";
}
//cout<<endl;
for(LL i=n;i>=1;i--)
{
if(b[i]>0){
bb[i]=bb[i+1]+b[i];
b[i-1]-=b[i];
b[i-2]-=b[i];
}
else bb[i]=bb[i+1];
//cout<<bb[i]<<" ";
}
//cout<<endl;
LL sum=1e18;
for(LL i=1;i<n;i++)
sum=min(sum,aa[i-1]+bb[i+2]);
cout<<sum<<endl;
return 0;
}