2023 CSP-J 第二轮 解析(转载)

原创链接:

https://www.cnblogs.com/mantou20210331/p/17781865.htmlicon-default.png?t=N7T8https://www.cnblogs.com/mantou20210331/p/17781865.html

T1 小苹果

链接:出错了 - 洛谷icon-default.png?t=N7T8https://www.luogu.com.cn/problem/P9748?contestId=140858

代码:

#include<iostream>
using namespace std;

int main() {
    int n;
    cin>>n;
    int k=n,d=0,ans=0;
    while(k>0) {
        if(k%3==1&&ans==0) ans=d+1;
        int t=(k-1)/3+1;//计算每一天拿走多少个
        k=k-t;//还剩下的个数
        d++;
    }
    cout<<d<<" "<<ans;
    return 0;
}

T2 公路

链接:出错了 - 洛谷icon-default.png?t=N7T8https://www.luogu.com.cn/problem/P9749?contestId=140858

代码:

#include <iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int N=100010;
int n,d;
int v[N],a[N];
int main() {
    scanf("%d %d",&n,&d);
    for(int i=1; i<n; i++) scanf("%d",&v[i]);
    for(int i=1; i<=n; i++) scanf("%d",&a[i]);
    long long ans=0;
    int m=a[1],res=0;
    for(int i=1; i<n; i++) {
        m=min(m,a[i]);
        if(res>=v[i]) res-=v[i];
        else {
            int t=(v[i]-res)/d;
            if((v[i]-res)%d!=0) t++;//计算需要多少升油
            ans+=m*t;
            res=t*d+res-v[i];
        }
    }
    printf("%lld",ans);
    return 0;
}

T3 一元二次方程

链接:出错了 - 洛谷icon-default.png?t=N7T8https://www.luogu.com.cn/problem/P9750?contestId=140858

代码 :

#include <iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int t,m,a,b,c;

int gcd(int p,int q) {
    int u;
    while(q!=0) {
        u=p%q;
        p=q;
        q=u;
    }
    return p;
}

void work1(int p,int q) {
    if(p*q<0) cout<<'-';
    if(p<0) p=0-p;
    if(q<0) q=0-q;
    int g=gcd(p,q);//求出最大公约数
    if(g==q) cout<<p/g;
    else cout<<p/g<<'/'<<q/g;
    return;
}
void work2(int p,int q) { //sqrt部分输出
    if(p<0) p=-p;
    if(q<0) q=-q;
    int u=1;
    for(int i=sqrt(p); i>=2; i--) {
        if(p%(i*i)==0) {
            p=p/(i*i);
            u=u*i;
            break;
        }
    }
    int g=gcd(u,q);
    u=u/g;
    q=q/g;
    if(u!=1) cout<<u<<"*";
    if(p!=1) cout<<"sqrt("<<p<<")";
    if(q!=1) cout<<"/"<<q;
}
int main() {
//    freopen("uqe2.in","r",stdin);
//    freopen("uqe2.ans","w",stdout);
    cin>>t>>m;
    while(t--) {
        cin>>a>>b>>c;
        int d=b*b-4*a*c;
        if(d<0) {//无解
            cout<<"NO";
        } else { //有解
            int q=sqrt(d);
            if(q*q==d) { //x有理数
                double ans1=1.0*(-b+q)/(2*a);
                double ans2=1.0*(-b-q)/(2*a);
                if(ans1>ans2) {
                    if(-b+q==0) cout<<0;
                    else work1(-b+q,2*a);
                } else {
                    if(-b-q==0) cout<<0;
                    else work1(-b-q,2*a);
                }
            } else { //x无理数
                if(b!=0) work1(-b,2*a);
                if(b!=0) cout<<"+";
                work2(d,2*a);
            }

        }
        cout<<endl;
    }
    return 0;
}

T4 旅游巴士 

链接:出错了 - 洛谷icon-default.png?t=N7T8https://www.luogu.com.cn/problem/P9751?contestId=140858

代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=10010,M=20010,inf=0x3f3f3f3f;
int n,m,k;
int h[N],e[M],ne[M],w[M],idx=0;
int dis[N][105],path[N];
void add(int a,int b,int c) {
    e[idx]=b;
    w[idx]=c;
    ne[idx]=h[a];
    h[a]=idx++;
}
struct node {
    int v,d;
} t;
struct cmp {
    bool operator()(const node a,const node b) {
        return a.d>b.d;
    }
};

int dijkstra() {
    memset(dis,0x3f,sizeof dis);
    priority_queue<node,vector<node>,cmp> heap;
//    dis[1]=0;
    path[1]=0;
    heap.push({1,0});
    while(!heap.empty()) {
        t=heap.top();
        heap.pop();
        if(dis[t.v][t.d%k]<t.d) continue;
        for(int i=h[t.v]; i!=-1; i=ne[i]) {
            int j=e[i];
            int tt=t.d;
            if(tt+1<w[i]) {
                tt+=((w[i]-1-tt+k)/k*k);
            }
            if(tt+1<dis[j][(tt+1)%k]) {
                dis[j][(tt+1)%k]=tt+1;
                heap.push({j,dis[j][(tt+1)%k]});
            }
        }
    }
    if(dis[n][0]>=inf) return -1;
    else return dis[n][0];
}


int main() {
    memset(h,-1,sizeof h);
    cin>>n>>m>>k;
    for(int i=1; i<=m; i++) {
        int u,v,a;
        cin>>u>>v>>a;
        add(u,v,a);
    }
    printf("%d",dijkstra());
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值