杭州电子科技大学2018新生编程大赛题解
Posted myx12345
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了杭州电子科技大学2018新生编程大赛题解相关的知识,希望对你有一定的参考价值。
01:
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 typedef long long ll; 13 typedef unsigned int uint; 14 typedef unsigned long long ull; 15 typedef pair<int,int> PII; 16 typedef vector<int> VI; 17 #define fi first 18 #define se second 19 #define MP make_pair 20 #define N 11000 21 #define MOD 19260817 22 #define eps 1e-8 23 #define pi acos(-1) 24 25 26 27 int main() 28 { 29 int cas; 30 scanf("%d",&cas); 31 while(cas--) 32 { 33 int n; 34 scanf("%d",&n); 35 ll ans=1; 36 for(int i=1;i<=n;i++) 37 { 38 ll x; 39 scanf("%I64d",&x); 40 ans=ans*x%MOD; 41 } 42 printf("%I64d ",ans); 43 } 44 return 0; 45 }
02:
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 typedef long long ll; 13 typedef unsigned int uint; 14 typedef unsigned long long ull; 15 typedef pair<int,int> PII; 16 typedef vector<int> VI; 17 #define fi first 18 #define se second 19 #define MP make_pair 20 #define N 110000 21 #define MOD 1000000007 22 #define eps 1e-8 23 #define pi acos(-1) 24 25 ll s[N],a[N]; 26 27 int main() 28 { 29 int cas; 30 scanf("%d",&cas); 31 while(cas--) 32 { 33 int n,m; 34 scanf("%d%d",&n,&m); 35 s[0]=0; 36 for(int i=1;i<=n;i++) 37 { 38 scanf("%I64d",&a[i]); 39 s[i]=s[i-1]+a[i]; 40 } 41 for(int i=1;i<=m;i++) 42 { 43 ll x; 44 scanf("%I64d",&x); 45 int l=0; 46 int r=n; 47 int last=0; 48 while(l<=r) 49 { 50 int mid=(l+r)>>1; 51 if(s[mid]<x){last=mid; l=mid+1;} 52 else r=mid-1; 53 } 54 printf("%d ",last); 55 } 56 } 57 return 0; 58 }
03:贪心,每次塞到人最多的能塞下的车里,不行就开新车
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 typedef long long ll; 13 typedef unsigned int uint; 14 typedef unsigned long long ull; 15 typedef pair<int,int> PII; 16 typedef vector<int> VI; 17 #define fi first 18 #define se second 19 #define MP make_pair 20 #define N 1100000 21 #define MOD 1000000007 22 #define eps 1e-8 23 #define pi acos(-1) 24 25 int a[N],b[N]; 26 27 bool cmp(int a,int b) 28 { 29 return a>b; 30 } 31 32 int main() 33 { 34 int cas; 35 scanf("%d",&cas); 36 while(cas--) 37 { 38 int n,m; 39 scanf("%d%d",&n,&m); 40 for(int i=1;i<=m;i++) scanf("%d",&a[i]); 41 sort(a+1,a+m+1,cmp); 42 for(int i=0;i<=4;i++) b[i]=0; 43 int ans=0; 44 for(int i=1;i<=m;i++) 45 { 46 int flag=1; 47 for(int j=3;j>=1;j--) 48 if(j+a[i]<=4&&b[j]>0) 49 { 50 b[j]--; b[j+a[i]]++; 51 flag=0; break; 52 } 53 if(flag){ans++; b[a[i]]++;} 54 } 55 printf("%d ",ans); 56 } 57 return 0; 58 } 59 60
04:按增加的攻击力贪心
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 typedef long long ll; 13 typedef unsigned int uint; 14 typedef unsigned long long ull; 15 typedef pair<int,int> PII; 16 typedef vector<int> VI; 17 #define fi first 18 #define se second 19 #define MP make_pair 20 #define N 11000 21 #define MOD 1000000007 22 #define eps 1e-8 23 #define pi acos(-1) 24 25 struct node 26 { 27 ll x,y,z; 28 }a[N]; 29 30 bool cmp(node a,node b) 31 { 32 return a.z>b.z; 33 } 34 35 int main() 36 { 37 int cas; 38 scanf("%d",&cas); 39 while(cas--) 40 { 41 int n,m; 42 scanf("%d%d",&n,&m); 43 for(int i=1;i<=n;i++) 44 { 45 scanf("%I64d%I64d",&a[i].x,&a[i].y); 46 a[i].z=a[i].y-a[i].x; 47 } 48 ll ans=0; 49 sort(a+1,a+n+1,cmp); 50 for(int i=1;i<=n;i++) 51 { 52 ans+=a[i].x; 53 if(m>0&&a[i].z>0){m--; ans+=a[i].z;} 54 } 55 printf("%I64d ",ans); 56 } 57 return 0; 58 }
05:
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 typedef long long ll; 13 typedef unsigned int uint; 14 typedef unsigned long long ull; 15 typedef pair<int,int> PII; 16 typedef vector<int> VI; 17 #define fi first 18 #define se second 19 #define MP make_pair 20 #define N 11000 21 #define MOD 1000000007 22 #define eps 1e-8 23 #define pi acos(-1) 24 25 int a[N]; 26 27 int main() 28 { 29 int cas; 30 scanf("%d",&cas); 31 while(cas--) 32 { 33 int n; 34 scanf("%d",&n); 35 int mx=0; 36 for(int i=1;i<=n;i++) 37 { 38 scanf("%d",&a[i]); 39 mx=max(mx,a[i]); 40 } 41 for(int i=1;i<=mx;i++) 42 { 43 for(int j=1;j<=n;j++) 44 if(a[j]>=mx-i+1) printf("#"); 45 else printf("."); 46 printf(" "); 47 } 48 for(int i=1;i<=n;i++) printf("@"); 49 printf(" "); 50 } 51 return 0; 52 }
06:
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 typedef long long ll; 13 typedef unsigned int uint; 14 typedef unsigned long long ull; 15 typedef pair<int,int> PII; 16 typedef vector<int> VI; 17 #define fi first 18 #define se second 19 #define MP make_pair 20 #define N 11000 21 #define MOD 1000000007 22 #define eps 1e-8 23 #define pi acos(-1) 24 25 char a[N],b[N]; 26 27 int main() 28 { 29 int cas; 30 scanf("%d",&cas); 31 while(cas--) 32 { 33 scanf("%s",a+1); 34 int n=strlen(a+1); 35 int m=0; 36 for(int i=1;i<=n;i++) 37 { 38 if(a[i]>=‘0‘&&a[i]<=‘9‘) continue; 39 b[++m]=a[i]; 40 } 41 sort(b+1,b+m+1); 42 for(int i=1;i<=m;i++) printf("%c",b[i]); 43 printf(" "); 44 } 45 return 0; 46 }
07:每个询问是连续的一段,二分起点和终点
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 typedef long long ll; 13 typedef unsigned int uint; 14 typedef unsigned long long ull; 15 typedef pair<int,int> PII; 16 typedef vector<int> VI; 17 #define fi first 18 #define se second 19 #define MP make_pair 20 #define N 510000 21 #define MOD 1000000007 22 #define eps 1e-8 23 #define pi acos(-1) 24 #define oo 1000000000 25 26 ll a[N]; 27 28 int main() 29 { 30 int cas; 31 scanf("%d",&cas); 32 while(cas--) 33 { 34 int n,m; 35 scanf("%d%d",&n,&m); 36 for(int i=1;i<=n;i++) scanf("%I64d",&a[i]); 37 sort(a+1,a+n+1); 38 for(int i=1;i<=m;i++) 39 { 40 ll x,y; 41 scanf("%I64d%I64d",&x,&y); 42 //printf("%d %d ",x,y); 43 //ll t=ll(sqrt(x)); 44 //if(t*t<x) x=(t+1)*(t+1); 45 //t=ll(sqrt(y)); 46 //if(t*t<y) y=(t-1)*(t-1); 47 //printf("%d %d ",x,y); 48 int l=1; 49 int r=n; 50 int last1=n+1; 51 while(l<=r) 52 { 53 int mid=(l+r)>>1; 54 if(a[mid]*a[mid]>=x){last1=mid; r=mid-1;} 55 else l=mid+1; 56 } 57 l=1; 58 r=n; 59 int last2=0; 60 while(l<=r) 61 { 62 int mid=(l+r)>>1; 63 if(a[mid]*a[mid]<=y){last2=mid; l=mid+1;} 64 else r=mid-1; 65 } 66 int ans=last2-last1+1; 67 ans=max(ans,0); 68 //p rintf("%d %d %d ",last1,last2,ans); 69 printf("%d ",ans); 70 } 71 } 72 return 0; 73 }
08:dp[i][j]表示积分为i,当前j朝上的最小步数
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 typedef long long ll; 13 typedef unsigned int uint; 14 typedef unsigned long long ull; 15 typedef pair<int,int> PII; 16 typedef vector<int> VI; 17 #define fi first 18 #define se second 19 #define MP make_pair 20 #define N 50000 21 #define MOD 1000000007 22 #define eps 1e-8 23 #define pi acos(-1) 24 #define oo 1000000000 25 26 int dp[N][10]; 27 28 int main() 29 { 30 int cas; 31 scanf("%d",&cas); 32 for(int i=0;i<=10000;i++) 33 for(int j=1;j<=6;j++) dp[i][j]=oo; 34 dp[0][1]=0; 35 for(int i=0;i<=10000;i++) 36 for(int j=1;j<=6;j++) 37 for(int k=1;k<=6;k++) 38 if(j+k!=7&&j!=k&&dp[i][j]!=oo&&i+k<=10000) dp[i+k][k]=min(dp[i+k][k],dp[i][j]+1); 39 while(cas--) 40 { 41 int n; 42 scanf("%d",&n); 43 if(n==1){printf("-1 "); continue;} 44 int ans=oo; 45 for(int i=1;i<=6;i++) ans=min(ans,dp[n][i]); 46 if(ans==oo) printf("-1 "); 47 else printf("%d ",ans); 48 } 49 50 51 return 0; 52 }
09:完全背包转多重背包O(n^3),用调和级数把一个O(n^2)优化成O(n log n)
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <cmath> 5 #include <iostream> 6 #include <algorithm> 7 #include <map> 8 #include <set> 9 #include <queue> 10 #include <vector> 11 using namespace std; 12 typedef long long ll; 13 typedef unsigned int uint; 14 typedef unsigned long long ull; 15 typedef pair<int, int> PII; 16 #define fi first 17 #define se second 18 #define MP make_pair 19 20 21 int n,v; 22 int a[1010]; 23 int f[1010][2010]; 24 25 int main() 26 { 27 int cas; 28 scanf("%d",&cas); 29 while(cas--) 30 { 31 int n,v; 32 scanf("%d %d",&n,&v); 33 for (int i = 1; i <= n;i++) a[i]=read(); 34 for (int i = 0; i <= n;i++) 35 for (int j = 0; j<=v;j++) 36 f[i][j]=-2000000000; 37 f[0][0]=0; 38 for (int i=n;i>=1;i--) 39 for (int j = 0;j<min(v/i,n);j++) 40 for (int k=0;k<=v-i;k++) 41 f[j+1][k+i]=max(f[j+1][k+i],f[j][k]+a[i]); 42 printf("%d ",f[n][v]); 43 } 44 }
这场当了一把演员……
以上是关于杭州电子科技大学2018新生编程大赛题解的主要内容,如果未能解决你的问题,请参考以下文章