2023csp-j复赛试题及其讲解
P9748 [CSP-J 2023] 小苹果
题目讲解:https://www.bilibili.com/video/BV1uc411Z7kp/?spm_id_from=333.999.0.0
1 #include<iostream> 2 using namespace std; 3 4 int main() { 5 int n; 6 cin>>n; 7 int k=n,d=0,ans=0; 8 while(k>0) { 9 if(k%3==1&&ans==0) ans=d+1; 10 int t=(k-1)/3+1;//计算每一天拿走多少个 11 k=k-t;//还剩下的个数 12 d++; 13 } 14 cout<<d<<" "<<ans; 15 return 0; 16 }
P9749 [CSP-J 2023] 公路
题目讲解:https://www.bilibili.com/video/BV1TB4y1d7id/?spm_id_from=333.999.0.0
1 #include <iostream> 2 #include<cstdio> 3 #include<cmath> 4 using namespace std; 5 const int N=100010; 6 int n,d; 7 int v[N],a[N]; 8 int main() { 9 scanf("%d %d",&n,&d); 10 for(int i=1; i<n; i++) scanf("%d",&v[i]); 11 for(int i=1; i<=n; i++) scanf("%d",&a[i]); 12 long long ans=0; 13 int m=a[1],res=0; 14 for(int i=1; i<n; i++) { 15 m=min(m,a[i]); 16 if(res>=v[i]) res-=v[i]; 17 else { 18 int t=(v[i]-res)/d; 19 if((v[i]-res)%d!=0) t++;//计算需要多少升油 20 ans+=m*t; 21 res=t*d+res-v[i]; 22 } 23 } 24 printf("%lld",ans); 25 return 0; 26 }
P9750 [CSP-J 2023] 一元二次方程【民间数据】
视频讲解:https://www.bilibili.com/video/BV1VG411y7qJ/?spm_id_from=333.999.0.0&vd_source=fa5102b0272af775d3bc4ab69fc72737
1 #include <iostream> 2 #include<cstdio> 3 #include<cmath> 4 using namespace std; 5 int t,m,a,b,c; 6 7 int gcd(int p,int q) { 8 int u; 9 while(q!=0) { 10 u=p%q; 11 p=q; 12 q=u; 13 } 14 return p; 15 } 16 17 void work1(int p,int q) { 18 if(p*q<0) cout<<'-'; 19 if(p<0) p=0-p; 20 if(q<0) q=0-q; 21 int g=gcd(p,q);//求出最大公约数 22 if(g==q) cout<<p/g; 23 else cout<<p/g<<'/'<<q/g; 24 return; 25 } 26 void work2(int p,int q) { //sqrt部分输出 27 if(p<0) p=-p; 28 if(q<0) q=-q; 29 int u=1; 30 for(int i=sqrt(p); i>=2; i--) { 31 if(p%(i*i)==0) { 32 p=p/(i*i); 33 u=u*i; 34 break; 35 } 36 } 37 int g=gcd(u,q); 38 u=u/g; 39 q=q/g; 40 if(u!=1) cout<<u<<"*"; 41 if(p!=1) cout<<"sqrt("<<p<<")"; 42 if(q!=1) cout<<"/"<<q; 43 } 44 int main() { 45 // freopen("uqe2.in","r",stdin); 46 // freopen("uqe2.ans","w",stdout); 47 cin>>t>>m; 48 while(t--) { 49 cin>>a>>b>>c; 50 int d=b*b-4*a*c; 51 if(d<0) {//无解 52 cout<<"NO"; 53 } else { //有解 54 int q=sqrt(d); 55 if(q*q==d) { //x有理数 56 double ans1=1.0*(-b+q)/(2*a); 57 double ans2=1.0*(-b-q)/(2*a); 58 if(ans1>ans2) { 59 if(-b+q==0) cout<<0; 60 else work1(-b+q,2*a); 61 } else { 62 if(-b-q==0) cout<<0; 63 else work1(-b-q,2*a); 64 } 65 } else { //x无理数 66 if(b!=0) work1(-b,2*a); 67 if(b!=0) cout<<"+"; 68 work2(d,2*a); 69 } 70 71 } 72 cout<<endl; 73 } 74 return 0; 75 }
P9751 [CSP-J 2023] 旅游巴士【民间数据】
视频讲解:https://www.bilibili.com/video/BV1s94y157xa/?spm_id_from=333.999.0.0&vd_source=fa5102b0272af775d3bc4ab69fc72737
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 const int N=10010,M=20010,inf=0x3f3f3f3f; 7 int n,m,k; 8 int h[N],e[M],ne[M],w[M],idx=0; 9 int dis[N][105]; 10 void add(int a,int b,int c) { 11 e[idx]=b; 12 w[idx]=c; 13 ne[idx]=h[a]; 14 h[a]=idx++; 15 } 16 struct node { 17 int v,d; 18 } t; 19 struct cmp { 20 bool operator()(const node a,const node b) { 21 return a.d>b.d; 22 } 23 }; 24 int dijkstra() { 25 memset(dis,0x3f,sizeof dis); 26 priority_queue<node,vector<node>,cmp> heap; 27 28 heap.push({1,0}); 29 while(!heap.empty()) { 30 t=heap.top(); 31 heap.pop(); 32 if(dis[t.v][t.d%k]<t.d) continue; 33 for(int i=h[t.v]; i!=-1; i=ne[i]) { 34 int j=e[i]; 35 int tt=t.d; 36 if(tt+1<w[i]) { 37 tt+=((w[i]-1-tt+k)/k*k); 38 } 39 if(tt+1<dis[j][(tt+1)%k]) { 40 dis[j][(tt+1)%k]=tt+1; 41 heap.push({j,dis[j][(tt+1)%k]}); 42 } 43 } 44 } 45 if(dis[n][0]>=inf) return -1; 46 else return dis[n][0]; 47 } 48 49 50 int main() { 51 memset(h,-1,sizeof h); 52 cin>>n>>m>>k; 53 for(int i=1; i<=m; i++) { 54 int u,v,a; 55 cin>>u>>v>>a; 56 add(u,v,a); 57 } 58 printf("%d",dijkstra()); 59 return 0; 60 }