2025团体程序设计天梯赛个人向题解(L1+L2)
2025年4月20日大约 5 分钟
2025团体程序设计天梯赛个人向题解(L1+L2)
L1-1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
cout<<"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.";
return 0;
}L1-2
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int a,b,c;
cin>>a>>b>>c;
cout<<a+b+c<<'\n';
return 0;
}L1-3
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int T,s,t;
cin>>T>>s>>t;
if(s){
if(T>=35&&t>=33) cout<<"Bu Tie\n"<<T<<'\n';
else cout<<"Bu Re\n"<<t<<'\n';
}
else{
if(T>=35&&t>=33) cout<<"Shi Nei\n"<<T<<'\n';
else cout<<"Shu Shi\n"<<t<<'\n';
}
return 0;
}L1-4
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll n;
cin>>n;
ll ans=1;
while(ans*2ll<=n) ans*=2ll;
cout<<ans<<'\n';
return 0;
}L1-5
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[500];
int sum[500];
int main(){
string s;
cin>>s;
for(int i='a';i<='z';i++){
cin>>a[i];
}
for(int i=0;i<s.length();i++){
sum[s[i]]++;
}
int ans=0;
for(int i='a';i<'z';i++){
cout<<sum[i]<<' ';
ans+=sum[i]*a[i];
}
cout<<sum['z']<<'\n';
ans+=sum['z']*a['z'];
cout<<ans<<'\n';
return 0;
}L1-6
直接用数组模拟的,写的好麻烦
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[100005];
int L1[100005],L2[100005];
int temp[100005];
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
while(m--){
int op;
cin>>op;
if(op==1){
int l1;
cin>>l1;
for(int i=1;i<=l1;i++){
cin>>L1[i];
}
int l2;
cin>>l2;
for(int i=1;i<=l2;i++){
cin>>L2[i];
}
for(int i=1;i<=n-l1+1;i++){
int flag=1;
for(int j=1;j<=l1;j++){
if(a[i+j-1]!=L1[j]){
flag=0;
break;
}
}
if(flag){
for(int j=1;j<i;j++){
temp[j]=a[j];
}
for(int j=1;j<=l2;j++){
temp[i+j-1]=L2[j];
}
for(int j=1;j<=n-(i+l1-1);j++){
temp[i+l2+j-1]=a[i+l1+j-1];
}
n+=l2-l1;
for(int j=1;j<=n;j++){
a[j]=temp[j];
}
break;
}
}
}
else if(op==2){
temp[1]=a[1];
int cnt=0;
for(int i=2;i<=n;i++){
if((a[i]&1)==(a[i-1]&1)){
temp[i+cnt]=(a[i]+a[i-1])/2;
cnt++;
}
temp[i+cnt]=a[i];
}
n+=cnt;
for(int i=1;i<=n;i++){
a[i]=temp[i];
}
}
else{
int l,r;
cin>>l>>r;
for(int i=l;i<=r;i++){
temp[i-l]=a[i];
}
for(int i=r;i>=l;i--){
a[i]=temp[r-i];
}
}
}
for(int i=1;i<n;i++){
cout<<a[i]<<' ';
}
cout<<a[n]<<'\n';
return 0;
}L1-7
倒序遍历指数
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll fpow(ll a,ll b){
ll res=1;
while(b){
if(b&1) res*=a;
b>>=1;
a*=a;
}
return res;
}
int main(){
ll n;
cin>>n;
for(ll i=32;i>=1;i--){
ll temp=0;
for(ll j=1;temp<=n;j++){
temp+=fpow(j,i);
if(temp==n){
cout<<1<<"^"<<i;
for(ll k=2;k<=j;k++){
cout<<'+'<<k<<'^'<<i;
}
return 0;
}
}
}
cout<<"Impossible for "<<n<<".\n";
return 0;
}L1-8
排个序每次操作最大的所在的行和列,每轮操作完不用真的合并,输出的时候判断一下当前位置需不需要输出就行
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[1005][1005];
struct Point{
Point(int x=0,int y=0,ll num=0):x(x),y(y),num(num) {}
int x,y;
ll num;
}poi[1000006];
bool cmp(Point A,Point B){
return A.num>B.num;
}
int main(){
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
poi[(i-1)*n+j]=Point(i,j,a[i][j]);
}
}
sort(poi+1,poi+1+n*m,cmp);
int id=1;
for(int i=1;i<=k;i++){
while(a[poi[id].x][poi[id].y]==1e10) id++;
for(int j=1;j<=n;j++){
a[j][poi[id].y]=1e10;
}
for(int j=1;j<=m;j++){
a[poi[id].x][j]=1e10;
}
}
for(int i=1;i<=n;i++){
int sum=0;
for(int j=1;j<=m;j++){
if(a[i][j]!=1e10){
if(sum) cout<<' ';
cout<<a[i][j];
sum++;
}
}
if(sum) cout<<'\n';
}
return 0;
}L2-1
用栈维护,遇到别的字符就 push 进栈;遇到 ) 就 pop 到最近的一个 ( 然后倒序输出
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
string s;
cin>>s;
stack<char>st;
for(int i=0;i<s.length();i++){
if(s[i]!=')') st.push(s[i]);
else{
string temp="";
while(st.top()!='('){
temp+=st.top();
st.pop();
}
st.pop();
for(int j=temp.length()-1;j>=0;j--){
cout<<temp[j];
}
cout<<'\n';
}
}
return 0;
}L2-2
排个序遍历 然后直接判断与之对应的 存不存在
需要注意在判断 存不存在的时候: x1[i]+x1[i]-x0[j] 这里可能会出现 的情况,所以转为正数的时候记得 +3000000
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int x0[50005],x1[50005];
int x2[6000005];
int main(){
int n;
cin>>n;
int x,y;
int y0=0,y1=0;
for(int i=1;i<=n;i++){
cin>>x>>y;
if(y==0){
x0[++y0]=x;
}
else if(y==1){
x1[++y1]=x;
}
else{
x2[x+3000000]=1;
}
}
x1[0]=x0[0]=2000000;
sort(x0+1,x0+1+y0);
sort(x1+1,x1+1+y1);
int flag=0;
for(int i=1;i<=y1;i++){
if(x1[i]==x1[i-1]) continue;
for(int j=1;j<=y0;j++){
if(x0[j]==x0[j-1]) continue;
if(x2[x1[i]+x1[i]-x0[j]+3000000]){
flag=1;
cout<<"["<<x0[j]<<", 0] ["<<x1[i]<<", 1] ["<<x1[i]+x1[i]-x0[j]<<", 2]\n";
}
}
}
if(!flag) cout<<-1<<'\n';
return 0;
}L2-3
差分前缀和
这题第一发没加 ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); 超时了😓
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t[100000];
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
int a,b,c;
char ch1,ch2;
cin>>a>>ch1>>b>>ch2>>c;
t[a*3600+b*60+c]++;
cin>>a>>ch1>>b>>ch2>>c;
t[a*3600+b*60+c+1]--;
}
int ans=t[0];
for(int i=1;i<=86400;i++){
t[i]+=t[i-1];
ans=max(ans,t[i]);
}
cout<<ans<<'\n';
return 0;
}L2-4
就按提示写的,判断一下每一位可以是哪些数字,然后直接搜
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll a,b;
vector<int>vec[20];
int cnt=0;
void dfs(int idx,ll num){
if(idx==n){
if(num>=a&&num<=b&&num%idx==0){
cout<<num<<'\n';
cnt++;
}
return;
}
if(idx&&num%idx) return;
for(auto i:vec[idx+1]){
dfs(idx+1,num*10ll+i);
}
}
int main(){
cin>>n;
cin>>a>>b;
vec[1]={1,2,3,4,5,6,7,8,9};
vec[2]={0,2,4,6,8};
vec[3]={0,1,2,3,4,5,6,7,8,9};
vec[4]={0,2,4,6,8};
vec[5]={0,5};
vec[6]={0,2,4,6,8};
vec[7]={0,1,2,3,4,5,6,7,8,9};
vec[8]={0,2,4,6,8};
vec[9]={0,1,2,3,4,5,6,7,8,9};
vec[10]={0};
vec[11]={0,1,2,3,4,5,6,7,8,9};
vec[12]={0,2,4,6,8};
vec[13]={0,1,2,3,4,5,6,7,8,9};
vec[14]={0,2,4,6,8};
vec[15]={0,5};
dfs(0,0);
if(cnt==0) cout<<"No Solution\n";
return 0;
}