2019百度之星初赛-1
Q1,比最高项,都非零的话,约分即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 2e3+7; 5 const ll mod = 1e9+7; 6 7 int n,m,k; 8 int ar[maxn], mid[maxn]; 9 int main() 10 { 11 int t;scanf("%d", &t); 12 while(t--){ 13 scanf("%d", &n); 14 for(int i=1;i<=n;i++)scanf("%d", ar+i); 15 for(int i=1;i<=n;i++)scanf("%d", mid+i); 16 int a,b; 17 for(int i=n;i>=1;i--){ 18 if(ar[i]!=0 || mid[i]!=0){ 19 if(ar[i]==0){ 20 a=0; 21 b=1; 22 } 23 else if(mid[i]==0){ 24 a=1; 25 b=0; 26 } 27 else{ 28 a=ar[i]; 29 b=mid[i]; 30 int c = __gcd(a,b); 31 a/=c; 32 b/=c; 33 } 34 break; 35 } 36 } 37 printf("%d/%d\n",a,b); 38 } 39 return 0; 40 }
Q2,维护当前的可取区间即可,两个区间不想交的话,可能会出现最后一步可走一步,可走两步的情况,设置区间范围为2即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 1e6+7; 5 const ll mod = 1e9+7; 6 7 int n,m,k; 8 int ar[maxn], mid[maxn]; 9 int main() 10 { 11 int t;scanf("%d", &t); 12 while(t--){ 13 scanf("%d", &n); 14 int x=1,y=int(1e7),ans=0; 15 for(int i=1;i<=n;i++){ 16 int a,b; 17 scanf("%d%d",&a,&b); 18 int xx = max(x, a); 19 int yy = min(y, b); 20 if(xx<=yy){ 21 x=xx; 22 y=yy; 23 } 24 else if(a>y){ 25 int dis = a-y; 26 ans += (dis+1)/2; 27 x = a; 28 y = a; 29 if((dis&1) && (b>a)){ 30 y = a+1; 31 } 32 } 33 else if(b<x){ 34 int dis = x-b; 35 ans += (dis+1)/2; 36 x = b; 37 y = b; 38 if((dis&1) && (b>a)){ 39 x = b-1; 40 } 41 } 42 else{ 43 assert(1==2); 44 } 45 } 46 printf("%d\n", ans); 47 } 48 return 0; 49 }
Q3,代码量巨大,交了一发wa了,还没补题
Q5,找规律喽
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 1e6+7; 5 const ll mod = 1e9+7; 6 7 int main() 8 { 9 ll n; 10 int t;scanf("%d", &t); 11 while(t--){ 12 scanf("%I64d", &n); 13 if(n&1){ 14 ll ans; 15 if(n%6==3 || n%6==5)ans = n/6; 16 else ans = n/6*4+1; 17 printf("%I64d\n", ans); 18 } 19 else{ 20 if(n%6==0 || n%6==2)printf("%I64d\n", n/2); 21 else{ 22 printf("%I64d\n", n-1); 23 } 24 } 25 } 26 27 return 0; 28 }