2019.4.14浙大校赛
题目编号:4090~4099
外来题解1:戳这里
外来题解2:戳这里
B:李继朋
以下题解来源于鲁东大学,表示感谢!
网盘链接:👉戳这里
提取码:2nag
A:
AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cmath> 5 #include <cstdlib> 6 #include <cstring> 7 #include <map> 8 #include <stack> 9 #include <queue> 10 #include <vector> 11 #include <bitset> 12 #include <set> 13 #include <utility> 14 #include <sstream> 15 #include <iomanip> 16 using namespace std; 17 typedef long long ll; 18 typedef unsigned long long ull; 19 #define inf 0x3f3f3f3f 20 #define rep(i,l,r) for(int i=l;i<=r;i++) 21 #define lep(i,l,r) for(int i=l;i>=r;i--) 22 #define ms(arr) memset(arr,0,sizeof(arr)) 23 //priority_queue<int,vector<int> ,greater<int> >q; 24 const int maxn = (int)1e5 + 5; 25 const ll mod = 1e9+7; 26 vector<int> a0,a1,b0,b1; 27 int a[100100],b[100100]; 28 int main() 29 { 30 #ifndef ONLINE_JUDGE 31 freopen("in.txt", "r", stdin); 32 #endif 33 //freopen("out.txt", "w", stdout); 34 ios::sync_with_stdio(0),cin.tie(0); 35 int t; 36 scanf("%d",&t); 37 while(t--) { 38 a0.clear();a1.clear(); 39 b0.clear();b1.clear(); 40 int n,m; 41 scanf("%d %d",&n,&m); 42 rep(i,1,n) scanf("%d",&a[i]); 43 rep(i,1,m) scanf("%d",&b[i]); 44 int nape; 45 int len1=0,len2=0,len3=0,len4=0; 46 rep(i,1,n) { 47 scanf("%d",&nape); 48 if(nape==0) a0.push_back(a[i]),len1++; 49 if(nape==1) a1.push_back(a[i]),len2++; 50 } 51 rep(i,1,m) { 52 scanf("%d",&nape); 53 if(nape==0) b0.push_back(b[i]),len3++; 54 if(nape==1) b1.push_back(b[i]),len4++; 55 } 56 sort(a0.begin(),a0.end()); 57 sort(a1.begin(),a1.end()); 58 sort(b0.begin(),b0.end()); 59 sort(b1.begin(),b1.end()); 60 int ans=0; 61 for(int i=0,j=0;i<len1&&j<len4;) { 62 if(a0[i]>b1[j]) {ans++;i++;j++;} 63 else i++; 64 } 65 for(int i=0,j=0;i<len2&&j<len3;) { 66 if(b0[j]>a1[i]) {ans++;i++;j++;} 67 else j++; 68 } 69 printf("%d\n",ans); 70 } 71 return 0; 72 }
B
AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.math.BigInteger; 2 import java.util.Scanner; 3 4 public class Main { 5 6 static Scanner cin = new Scanner(System.in); 7 static BigInteger e,base; 8 static BigInteger zero=BigInteger.valueOf(0); 9 static BigInteger one=BigInteger.valueOf(1); 10 static BigInteger two=BigInteger.valueOf(2); 11 public static void main(String[] args) { 12 13 int test; 14 while(cin.hasNext()) { 15 test=cin.nextInt(); 16 for(int i=1;i <= test;++i) { 17 18 e=cin.nextBigInteger(); 19 Solve(); 20 } 21 } 22 } 23 private static void Solve() { 24 25 base=one; 26 while(base.multiply(two).compareTo(e) <= 0) 27 base=base.multiply(two); 28 // System.out.println(base); 29 BigInteger ans=base.subtract(one); 30 31 if(!base.equals(e)) 32 ans=ans.add(F()); 33 System.out.println(ans); 34 } 35 private static BigInteger F() { 36 37 BigInteger curBase=two; 38 BigInteger ans=zero; 39 BigInteger l=base.divide(curBase); 40 BigInteger r=e.divide(curBase); 41 for(int i=1;i < 4000;++i) { 42 43 if(l.compareTo(r) >= 0) 44 break; 45 BigInteger tmp=(r.subtract(l)).divide(two); 46 if(r.mod(two).intValue() != 0) 47 tmp=tmp.add(one); 48 ans=ans.add(tmp.multiply(BigInteger.valueOf(i))); 49 50 l=l.divide(two); 51 r=r.divide(two); 52 } 53 return ans; 54 } 55 }
C
AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 #define ll long long 3 #define inf 0x3f3f3f3f 4 #define rep(i,a,b) for(register int i=(a);i<=(b);i++) 5 using namespace std; 6 const int maxn=2010; 7 int n,m,A,B; 8 ll K; 9 int a[maxn],tt[maxn][maxn],sta,pos; 10 int ans; 11 char s[maxn][maxn]; 12 char ss[maxn]; 13 int fg; 14 int cal(int i,int j) 15 { 16 return 81*tt[i][j]+27*tt[i-1][j]+9*tt[i+1][j]+3*tt[i][j-1]+tt[i][j+1]; 17 } 18 int ok[maxn][maxn]; 19 void bfs() 20 { 21 ans=0; 22 ll t=1; 23 int num=0; 24 int x=A,y=B; 25 while(t<=K) 26 { 27 t++; 28 if(tt[x][y]==1) break; 29 int pos=cal(x,y); 30 if(ss[pos]=='I') continue; 31 else if(ss[pos]=='P') 32 { 33 if(tt[x][y]==2) 34 { 35 tt[x][y]=0; 36 num++; 37 } 38 } 39 else if(ss[pos]=='U') x--; 40 else if(ss[pos]=='D') x++; 41 else if(ss[pos]=='L') y--; 42 else if(ss[pos]=='R') y++; 43 } 44 ans=num; 45 } 46 int main() 47 { 48 int T,cas=1; 49 scanf("%d",&T); 50 while(T--) 51 { 52 scanf("%d%d",&n,&m); 53 scanf("%d%d%lld",&A,&B,&K); 54 scanf("%s",ss); 55 rep(i,1,n) 56 scanf("%s",s[i]+1); 57 memset(tt,0,sizeof(tt)); 58 rep(i,1,n) 59 rep(j,1,m) 60 tt[i][j]=(s[i][j]&15); 61 K=min(K,((ll)n*(ll)n*(ll)m*(ll)m)); 62 bfs(); 63 printf("%d\n",ans); 64 //if(fg) puts("Yes"); else puts("No"); 65 } 66 return 0; 67 }
E
AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 int a[100010],b[100010]; 6 int main(){ 7 int n,k,T,i,x,cnta,cntb; 8 a[0]=0;b[0]=0; 9 cin>>T; 10 while(T--){ 11 cnta=1;cntb=1; 12 scanf("%d%d",&n,&k); 13 for(i=0;i<n;i++){ 14 scanf("%d",&x); 15 if(x>0){ 16 a[cnta]=x; 17 cnta++; 18 }//分别统计左边和右边各有多少个 19 else if(x<0){ 20 b[cntb]=-x; 21 cntb++; 22 } 23 } 24 25 cnta--; 26 cntb--; 27 28 sort(a+1,a+cnta+1); 29 sort(b+1,b+cntb+1); 30 31 //最靠近邮局的几个即使不满足K个也单独送,最近的几个虽然一次送不满,但是距离近,相比于远处的 32 //应该让远处的送满,近处的送不满。 33 34 long long suma=0; 35 for(i=cnta%k;i<cnta;i=i+k)//左边的每K个送一次 36 suma+=2*a[i]; 37 suma+=a[cnta];//最后一趟去了不回来 38 39 long long sumb=0; 40 for(i=cntb%k;i<cntb;i=i+k)//右边的每K个送一次 41 sumb+=2*b[i]; 42 sumb+=b[cntb];//最后一趟去了不回来 43 44 long long sum=0; 45 46 if(a[cnta]>b[cntb])//两边选不回来的那一趟最小的,让这一趟回来 47 sum=suma+sumb+b[cntb]; 48 else 49 sum=suma+sumb+a[cnta]; 50 51 printf("%lld\n",sum); 52 } 53 return 0; 54 55 }
H
AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<functional> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstdlib> 5 #include<cstring> 6 #include<climits> 7 #include<cstdio> 8 #include<vector> 9 #include<string> 10 #include<queue> 11 #include<cmath> 12 #include<list> 13 #include<set> 14 #include<map> 15 #define N 305005 16 using namespace std; 17 struct Edge{ 18 int u,v,next; 19 Edge(int a=0,int b=0,int c=0):u(a),v(b),next(c){} 20 }edg[N*2]; 21 22 int stamp=0,dfn[N],low[N],bccno[N],bcc_cnt=0; 23 bool isbridge[N]; 24 int head[N],sum_edge=0; 25 26 void addedge(int u,int v) 27 { 28 edg[sum_edge]=Edge(u,v,head[u]); 29 head[u]=sum_edge++; 30 edg[sum_edge]=Edge(v,u,head[v]); 31 head[v]=sum_edge++; 32 } 33 34 void tarjan(int index,int fa) 35 { 36 int tmp; 37 dfn[index]=low[index]=++stamp; 38 for(int i=head[index];i!=-1;i=edg[i].next) 39 { 40 tmp=edg[i].v; 41 if(!dfn[tmp]) 42 { 43 tarjan(tmp,index); 44 low[index]=min(low[index],low[tmp]); 45 if(low[tmp]>dfn[index]) 46 isbridge[i]=isbridge[i^1]=1; 47 } 48 else if(dfn[tmp]<dfn[index] && tmp!=fa) 49 { 50 low[index]=min(low[index], dfn[tmp]); 51 } 52 } 53 } 54 55 void dfs(int index) 56 { 57 dfn[index]=1; 58 bccno[index]=bcc_cnt; 59 for(int i=head[index];i!=-1;i=edg[i].next) 60 { 61 if(isbridge[i]) 62 continue; 63 if(!dfn[edg[i].v]) 64 { 65 dfs(edg[i].v); 66 } 67 } 68 } 69 70 void find_ebcc(int n){ 71 bcc_cnt=stamp=0; 72 memset(dfn,0,sizeof(dfn)); 73 memset(low,0,sizeof(low)); 74 memset(isbridge,0,sizeof(isbridge)); 75 memset(bccno,0,sizeof(bccno)); 76 for(int i=1;i<=n;i++) 77 if(!dfn[i]) 78 tarjan(i, -1); 79 memset(dfn,0,sizeof(dfn)); 80 for(int i=1;i<=n;i++) 81 { 82 if(!dfn[i]) 83 { 84 bcc_cnt++; 85 dfs(i); 86 } 87 } 88 } 89 90 void init2(int n) 91 { 92 for(int i=0;i<=n;i++)head[i]=-1; 93 // memset(head,-1,sizeof(head)); 94 sum_edge=0; 95 } 96 97 98 int grand[N][20]={0}; 99 int depth[N],DEPTH; 100 101 void dfs2(int x) 102 { 103 for(int i=1;i<=DEPTH;i++) 104 { 105 grand[x][i]=grand[grand[x][i-1]][i-1]; 106 } 107 108 for(int i=head[x];i!=-1;i=edg[i].next) 109 { 110 int to=edg[i].v; 111 if(grand[x][0]==to)continue; 112 113 depth[to]=depth[x]+1; 114 grand[to][0]=x; 115 dfs2(to); 116 } 117 } 118 119 void init(int n) 120 { 121 DEPTH=floor(log(n + 0.0) / log(2.0)); 122 for(int i=0;i<=n;i++)depth[i]=0; 123 124 //memset(grand,0,sizeof(grand)); 125 for(int i=0;i<=n;i++) 126 for(int j=0;j<20;j++)grand[i][j]=0; 127 128 for(int i=1;i<=n;i++) 129 if(!depth[i]) 130 { 131 depth[i]=1; 132 dfs2(i); 133 } 134 } 135 136 int lca(int a,int b) 137 { 138 if(depth[a]>depth[b])swap(a,b); 139 for(int i=DEPTH;i>=0;i--) 140 if(depth[a]<depth[b]&&depth[grand[b][i]]>=depth[a]) 141 b=grand[b][i]; 142 143 for(int i=DEPTH;i>=0;i--) 144 if(grand[a][i]!=grand[b][i]) 145 { 146 a=grand[a][i]; 147 b=grand[b][i]; 148 } 149 150 if(a!=b) 151 { 152 return grand[a][0]; 153 } 154 return a; 155 } 156 157 int u[N],v[N]; 158 159 int pre[N]; 160 161 void init3(int n) 162 { 163 for(int i=0;i<=n;i++)pre[i]=i; 164 } 165 166 int Find(int x) 167 { 168 if(x==pre[x])return x; 169 return pre[x]=Find(pre[x]); 170 } 171 172 int main() 173 { 174 int t; 175 scanf("%d",&t); 176 while(t--) 177 { 178 int n,m,q; 179 scanf("%d %d %d",&n,&m,&q); 180 init2(n); 181 for(int i=0;i<m;i++)scanf("%d %d",&u[i],&v[i]),addedge(u[i],v[i]); 182 183 find_ebcc(n); // 1....bcc_cnt 184 init2(n); 185 init3(n); 186 187 for(int i=0;i<m;i++) 188 if(bccno[u[i]]!=bccno[v[i]]) 189 { 190 addedge(bccno[u[i]],bccno[v[i]]); 191 pre[Find(bccno[u[i]])]=Find(bccno[v[i]]); 192 } 193 194 init(bcc_cnt+1); 195 196 while(q--) 197 { 198 int et,s1,s2; 199 scanf("%d %d %d",&et,&s1,&s2); 200 int a=bccno[s1],b=bccno[s2],c=bccno[et]; 201 202 if(Find(a)!=Find(c)||Find(b)!=Find(c)) 203 { 204 printf("No\n"); 205 continue; 206 } 207 208 if(lca(a,c)==c&&lca(a,b)==lca(c,b)||lca(b,c)==c&&lca(a,b)==lca(a,c))printf("Yes\n"); 209 else 210 { 211 printf("No\n"); 212 213 } 214 } 215 } 216 return 0; 217 }