第九届“图灵杯”NEUQ-ACM程序设计竞赛个人赛

第九届“图灵杯”NEUQ-ACM程序设计竞赛个人赛

  • A - 大学期末现状

思路:语法题

Code:

#include <bits/stdc++.h>
using namespace std;
const int N=1e3+10;
typedef long long ll;
int x;
int main(){
    cin>>x;
    if(x>=60) cout<<"jige,haoye!"<<endl;
    else cout<<"laoshi,caicai,laolao"<<endl;
	return 0;
}
  • B - G1024

思路:语法题

Code:

#include <bits/stdc++.h>
using namespace std;
const int N=1e3+10;
typedef long long ll;
int n,k;
int main(){
    cin>>n>>k;
    int tt=0,flag=0;
    while(k--){
        int x,m;
        cin>>x>>m;
        int ans=m-x;
        tt++;
        if(ans>=n)flag=1;
        if(flag)break;
    }
    if(flag) cout<<tt<<endl;
    else cout<<"G!"<<endl;

	return 0;
}

  • C - NEUQ

思路:将字符串与 N E U Q NEUQ NEUQ比对,对应时放入栈中,最后看栈中 N E U Q NEUQ NEUQ的个数

Code:

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long ll;
int n;
char s[5],str[N];
stack<char>q;
int main(){
    cin>>n;
    for(int i=0;i<n;i++)cin>>str[i];
    s[0]='N',s[1]='E',s[2]='U',s[3]='Q';
    int tt=0;
    for(int i=0;i<n;i++){
        if(str[i]==s[tt]){
            q.push(str[i]);
            if(tt==3)tt=0;
            else tt++;
        }
    }
    int cnt=q.size();
	cnt=n-(cnt/4)*4;
    cout<<cnt<<endl;
	return 0;
}

  • D - 小G的任务

思路:暴力算位数和

Code:

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long ll;
int add(int x){
    int ans=0;
    while(x){
        int y=x%10;
        ans+=y;
        x/=10;
    }
    return ans;
}
int n;
int main(){
    cin>>n;
    ll ans=0;
    for(int i=1;i<=n;i++) ans+=add(i);
    cout<<ans<<endl;
	return 0;
}

  • E - nn与游戏

思路: B F S BFS BFS

Code:

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
const int N=1e3+10,mod=998244353;
typedef long long ll;
typedef pair<int,int> PII;

PII a[N],b[N];
int n,m,mp[N][N],d[N][N];
int dx[]={-1,1,0,0};
int dy[]={0,0,1,-1};

void bfs(int x,int y){
    queue<PII> q;
    memset(d,-1,sizeof(d));
    q.push({x,y});
    d[x][y]=0;
    while(!q.empty()){
            PII t=q.front();
            q.pop();
            for(int i=0;i<4;i++){
                int tx=t.first+dx[i],ty=t.second+dy[i];
                if(tx>=0&&tx<n&&ty>=0&&ty<n&&mp[tx][ty]==0&&d[tx][ty]==-1){
                    d[tx][ty]=d[t.first][t.second]+1;
                    q.push({tx,ty});
                }
            }
    }
}

int main(){
    IOS;

    cin>>n>>m;
    while(m--){
        int x,y;
        cin>>x>>y;
        mp[x][y]=1;
    }
    int t;
    cin>>t;
    for(int i=1;i<=t;i++){
        int x1,y1,x2,y2;
        cin>>x1>>y1>>x2>>y2;
        a[i].first=x1,a[i].second=y1;
        b[i].first=x2,b[i].second=y2;
        mp[x1][y1]=2;
    }
    for(int i=1;i<=t;i++){
        bfs(a[i].first,a[i].second);
        cout<<d[b[i].first][b[i].second]<<endl;
    }
    return 0;
}

  • F - 第二大数

思路:题意就是计算排列的所有元素个数大于等于 2 2 2的子排列中第二大的数之和
由于 N < = 1 0 4 N<=10^4 N<=104,完全可以暴力,两重循环,每次取出前两个数记作 t 1   t 2 t_1\ t_2 t1 t2,我们记下一个数为 t t t,显然有三种情况考虑:

t > m a x ( t 1 , t 2 ) t>max(t_1,t_2) t>max(t1,t2) 则: a n s + = m a x ( t 1 , t 2 ) ans+=max(t_1,t_2) ans+=max(t1,t2), t t t 替换 m i n ( t 1 , t 2 ) min(t_1,t_2) min(t1,t2)
m i n ( t 1 , t 2 ) < t < m a x ( t 1 , t 2 ) min(t_1,t_2)<t<max(t_1,t_2) min(t1,t2)<t<max(t1,t2) 则: a n s + = t ans+=t ans+=t, t t t 替换 m i n ( t 1 , t 2 ) min(t_1,t_2) min(t1,t2)
t < m i n ( t 1 , t 2 ) t<min(t_1,t_2) t<min(t1,t2) 则: a n s + = m i n ( t 1 , t 2 ) ans+=min(t_1,t_2) ans+=min(t1,t2)

Code:

#include <bits/stdc++.h>
using namespace std;
const int N=1e4+10;
typedef long long ll;
int a[N],n;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    ll tmp,ans=0,tmp1;
    for(int i=1;i<n;i++){
        tmp=a[i],tmp1=a[i+1];
        ans+=min(tmp,tmp1);
        for(int j=i+2;j<=n;j++){
            if(a[j]>max(tmp,tmp1)){
                ans+=max(tmp,tmp1);
                int tt=max(tmp1,tmp);
                tmp=tt;
                tmp1=a[j];
            }
            if(a[j]<max(tmp,tmp1)&&a[j]>min(tmp,tmp1)){
                    ans+=a[j];
                    int tt=max(tmp1,tmp);
                    tmp=tt;
                    tmp1=a[j];
            }
            if(a[j]<min(tmp,tmp1)){
                ans+=min(tmp,tmp1);
            }
        }
    }
    cout<<ans<<endl;
	return 0;
}


  • G - Num

思路: a ∗ b + a + b = N = > a = N − b b + 1 a*b+a+b=N => a=\frac{N-b}{b+1} ab+a+b=N=>a=b+1Nb
因此判断是否存在整除即可
注意: 1 < = i < = n + 1 1<=i<=\sqrt{n}+1 1<=i<=n +1,判断 n − i > 0 n-i>0 ni>0

Code:

#include <bits/stdc++.h>
using namespace std;
const int N=1e3+10;
typedef long long ll;
ll n;
int main(){
    cin>>n;
    if(n==1) cout<<"No"<<endl;
    else{
        int flag=0;
        for(int i=1;i<=sqrt(n)+1;i++){
            if((n-i)%(i+1)==0&&n>i){
                cout<<"Yes"<<endl;
                flag=1;
                break;
            }
        }
        if(flag==0) cout<<"No"<<endl;
    }
	return 0;
}
  • H - 特征值

思路:手动模拟进位

Code:

#include <bits/stdc++.h>
using namespace std;
const int N=5e5+5;
char s[N];int a[N];
int main(){
	scanf("%s",s+1);
    int n=strlen(s+1);
	for(int i=n;i;i--) a[i]=a[i+1]+s[n-i+1]-'0';
    int j;
	for(int i=1,f=0;i<=n||f;i++)a[i]+=f,f=a[i]/10,a[i]%=10,j=i;
	for(;j;j--)cout<<a[j];
    return 0;
}
  • I - 最大公约数

思路:若 x ∣ a 1 , x ∣ a 2 , . . . , x ∣ a n x|a_1,x|a_2,...,x|a_n xa1,xa2,...,xan,则 x ∣ ∑ i = 1 n a i x|\sum_{i=1}^n a_i xi=1nai
因此不论选 l , r l,r l,r为何值,序列和不变,最大公因数个数为 ∑ i = 1 n a i \sum_{i=1}^n a_i i=1nai的因数个数

Code:

#include <bits/stdc++.h>
using namespace std;
const int N=1e3+10;
typedef long long ll;
int n,a[N];
int main(){
    cin>>n;
    ll sum=0;
    for(int i=1;i<=n;i++) {
        cin>>a[i];
        sum+=a[i];
    }
    int tt=0;
    for(int i=1;i<=sum/i;i++){
        if(sum%i==0){
            if(i*i==sum) tt++;
            else tt+=2;
        }
    }
    cout<<tt<<endl;
	return 0;
}

  • J - 玄神的字符串

思路:先统计字符串的 0 / 1 0/1 0/1个数

  • 如果其中最小值 k k k是一个奇数,那么说明我们要剩下一对,这一对就要做先变化再删除,或者直接删除,对于剩下的全部相同的值,我们要么进行反转一半全部用不同的方式删除,或者直接使用相同的删除
  • 如果是一个偶数,那么直接删除即可(注意这里的删除一种是不同删除,一种是相同删除),对于剩下的全部相同的值,我们要么进行反转一半全部用不同的方式删除,或者直接使用相同的删除

Code:

#include <bits/stdc++.h>
using namespace std;
const int N=5e4+10;
typedef long long ll;
string str;
ll a,b,c;
int main(){
    cin>>str;
    cin>>a>>b>>c;
    ll l=0,r=0;
    for(int i=0;i<str.size();i++){
       if(str[i]=='0') l++;
       else r++;
    }
    ll ans=0;
    ll k=min(l,r);
    if(k%2==0){
        ans+=k*min(a,b);
        l-=k,r-=k;
        l=max(l,r);
        ans+=min(l/2*b,l/2*(c+a));
    }
    else{
        ans+=(k-1)*min(a,b);
        l-=k-1,r-=k-1;
        ans+=min(a,b+c);
        l=max(l,r);
        ans+=min(l/2*b,l/2*(c+a));
    }
    cout<<ans<<endl;
	return 0;
}
  • K - 金牌厨师

思路:二分答案+差分维护

Code:

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
const int N=3e5+10,mod=998244353;
typedef long long ll;
typedef pair<int,int> PII;

PII a[N];
int sum[N],m,n;
bool check(int mid){
    memset(sum,0,sizeof(sum));
    for(int i=1;i<=m;i++){
        int len=a[i].second-a[i].first+1;
        if(len>=mid){
            sum[a[i].first+mid-1]++;
            sum[a[i].second+1]--;
        }
    }
    for(int i=1;i<=n;i++){
        sum[i]+=sum[i-1];
        if(sum[i]>=mid) return true;
    }
    return false;
}

int main(){
    IOS;
    cin>>n>>m;
    for(int i=1;i<=m;i++) cin>>a[i].first>>a[i].second;
    int l=1,r=n;
    while(l<r){
        int mid=(l+r+1)>>1;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    cout<<l<<endl;
  
    return 0;
}

  • L - WireConnection

思路:每两点间距离求出来,发现满足题意的就是这张三维图的最小生成树, n < = 2000 n<=2000 n<=2000,朴素 p r i m prim prim即可

Code:

#include <bits/stdc++.h>
using namespace std;
const int N=1e4+10;
typedef long long ll;
ll dis[N],n,st[N],ans;
struct node{
    ll x,y,z;
}a[N];
ll cal(int p,int q){
    ll tt=ceil(sqrt((a[p].x-a[q].x)*(a[p].x-a[q].x)+(a[p].y-a[q].y)*(a[p].y-a[q].y)+(a[p].z-a[q].z)*(a[p].z-a[q].z)));
    return tt;
}
void prim(){
    memset(dis,0x3f,sizeof(dis));
    dis[1]=0;
    for(int i=1;i<=n;i++){
        int t=-1;
        for(int j=1;j<=n;j++){
            if(!st[j]&&(t==-1||dis[t]>dis[j])) t=j;
        }
        ans+=dis[t];
        st[t]=1;
        for(int j=1;j<=n;j++){
            if(!st[j]){
                dis[j]=min(dis[j],cal(j,t));
            }
        }
    }
}
int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        ll aa,bb,cc;
        cin>>aa>>bb>>cc;
        a[i].x=aa,a[i].y=bb,a[i].z=cc;
    }
    prim();
    cout<<ans<<endl;
	return 0;
}

  • M - NuclearReactor

思路:大模拟(就不补了…

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学不会数据库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值