2019 Jiangsu Province match

A.Cotreey

Transmission: http://acm.hdu.edu.cn/showproblem.php?pid=6567

Meaning of the questions: There is a tree $ n $ points, $ n-2 $ edges, to add an edge between any two points, the distance between the point and asked twenty-two and how much minimum.

Data range: $ 2 <= n <= 10 ^ 5 $.

Analysis: After reading problems, school girl: This is not a tree root dp + change thing? I: meow meow meow? ? ? (I will not ah ..

Then read for a while ,,, emmmm center of gravity is not the sum of two sub-tree like thing.

The nature of the tree are:

  1. Tree in the distance and all points of a point, and the distance to the center of gravity is minimal, if there are two distances and their distance and the same.
  2. The two trees are connected by an edge, the new center of gravity of the tree the two trees in the center of gravity of the original connection.
  3. A tree add or delete a node, moving the center of gravity of the tree at most one edge position.
  4. The center of gravity of a tree has at most two, and adjacent.

Therefore, the center of gravity of the two trees are connected, and the minimum distance obtained.

Solving distance and time, and to consider the contribution of each side.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=1e5+10;
 5 vector<int> mp[maxn];
 6 int mi1,mi2,id1,id2,num1,num2;
 7 int dp[maxn],vis[maxn],n;
 8 ll num[maxn];
 9 ll ans;
10 void dfs(int x,int fa){
11     vis[x]=1;
12     for (int i=0;i<mp[x].size();i++){
13         if (mp[x][i]==fa) continue;
14         dfs(mp[x][i],x);
15     }
16 }
17 void dfs1(int x,int fa){
18     dp[x]=1;
19     int mx=0;
20     for (int i=0;i<mp[x].size();i++){
21         if (mp[x][i]==fa) continue;
22         dfs1(mp[x][i],x);
23         dp[x]+=dp[mp[x][i]];
24         mx=max(mx,dp[mp[x][i]]); 
25     }
26     mx=max(mx,num1-dp[x]);
27     if (mx<mi1){
28         mi1=mx;
29         id1=x;
30     }
31 }
32 void dfs2(int x,int fa){
33     dp[x]=1; vis[x]=1;
34     int mx=0;
35     for (int i=0;i<mp[x].size();i++){
36         if (mp[x][i]==fa) continue;
37         dfs2(mp[x][i],x);
38         dp[x]+=dp[mp[x][i]];
39         mx=max(mx,dp[mp[x][i]]); 
40     }
41     mx=max(mx,num2-dp[x]);
42     if (mx<mi2){
43         mi2=mx;
44         id2=x;
45     }
46 }
47 void dfs3(int x,int fa){
48     num[x]=1;
49     for (int i=0;i<mp[x].size();i++){
50         if (mp[x][i]==fa) continue;
51         dfs3(mp[x][i],x);
52         num[x]+=num[mp[x][i]];
53     }
54 }
55 void dfsans(int x,int fa){
56     for (int i=0;i<mp[x].size();i++){
57         if (mp[x][i]==fa) continue;
58         dfsans(mp[x][i],x);
59         ans+=1ll*(num[mp[x][i]]*(n-num[mp[x][i]]));
60     }
61 }
62 int main(){
63     int x,y,rt;  scanf("%d",&n);
64     for (int i=0;i<n-2;i++){
65         scanf("%d%d",&x,&y);
66         mp[x].push_back(y);
67         mp[y].push_back(x);
68         vis[i]=0; num[i]=0;
69     }
70     dfs(1,0);
71     num1=0; for (int i=1;i<=n;i++) if (vis[i]) num1++;
72     num2=n-num1;
73     mi1=2*n,mi2=2*n;
74     dfs1(1,0);
75     for (int i=1;i<=n;i++) if (!vis[i]){rt=i; break;}
76     dfs2(rt,0);
77     //cout << id1 << "   " << id2<< endl;
78     dfs3(id1,0);
79     dfs3(id2,0);
80     //cout << num[id1] << "   " << num[id2] << endl;
81     ans=0;
82     dfsans(id1,0); dfsans(id2,0);
83     ans+=1ll*(num[id1]*num[id2]);
84     printf("%lld\n",ans);
85     return 0;
86 }
A

B.Math

Transmission: http://acm.hdu.edu.cn/showproblem.php?pid=6568

 

C.Trap

Transmission: http://acm.hdu.edu.cn/showproblem.php?pid=6569

 

D.Wave

Transmission: http://acm.hdu.edu.cn/showproblem.php?pid=6570

The meaning of problems: a sequence number of a $ $ n, comprising a number in the range of $ a $ 1-c, requires solving a sequence of the following requirements:

The same odd digits; same even bit numbers; different parity bits. Ask, such as how long the longest sequence?

Data range: $ 1 <= n <= 10 ^ 5,1 <= c <= 100 $.

analysis:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e5+7;
 4 struct node{int num,cnt;} dp[110][110];
 5 int a[maxn];
 6 int main()
 7 {
 8     int n,c;
 9     while (~scanf("%d%d",&n,&c))
10     {
11         for (int i=1;i<=n;i++) scanf("%d",&a[i]);
12         for (int i=1;i<=c;i++)
13             for (int j=1;j<=c;j++) dp[i][j].cnt=0,dp[i][j].num=i;
14         for (int i=1;i<=n;i++)
15         {
16             int x=a[i];
17             for (int i=1;i<=c;i++)
18             {
19                 if(x==i) continue;
20                 if(dp[i][x].num==x) {dp[i][x].cnt++;dp[i][x].num=i;}
21                 if(dp[x][i].num==x) {dp[x][i].cnt++;dp[x][i].num=i;}
22             }
23         }
24         int ans=0;
25         for (int i=1;i<=c;i++)
26             for (int j=1;j<=c;j++) ans=max(ans,dp[i][j].cnt);
27         printf("%d\n",ans);
28     }
29      return  0 ;
30 }
D

E.Packing

Transmission: http://acm.hdu.edu.cn/showproblem.php?pid=6571

 

F.String

Transmission: http://acm.hdu.edu.cn/showproblem.php?pid=6572

Meaning of the questions: contains only give you a $ a, v, i, n $ $ n $ is the length of the string, and so asked to select $ a, v, i, n $ probability is.

Data range: $ 1 <= n <= 100 $.

Analysis: multiplication principle.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 map<int,int> mp;
 4 int num[5];
 5 int gcd_(int xx,int yy){
 6     if (yy==0) return xx;
 7     else return gcd_(yy,xx%yy);
 8 }
 9 int main(){
10     int n;char ch; mp['a']=1; mp['v']=2; mp['i']=3; mp['n']=4;
11     while (~scanf("%d",&n)){
12         getchar();
13         memset(num,0,sizeof(num));
14         for (int i=1;i<=n;i++){
15             scanf("%c",&ch);
16             num[mp[ch]]++;
17         }
18         int xx=1,yy=n*n*n*n;
19         for (int i=1;i<=4;i++){
20             xx*=num[i];
21         }
22         if (xx==0){
23             printf("0/1\n");
24             continue;
25         }
26         int tmp=gcd_(xx,yy);
27         xx/=tmp; yy/=tmp;
28         printf("%d/%d\n",xx,yy);
29     }
30     return 0;
31 }
F

G.Traffic

Transmission: http://acm.hdu.edu.cn/showproblem.php?pid=6573

Meaning of the questions: what cars to have $ n $, $ m $ has a north-south car, they passed the intersection when the time was $ a_i, b_i $. North and south to ask how long the car can wait at least ensure that no cars collided.

Data range: $ 1 <= n, m <= 1000,1 <= a_i, b_i <= 1000 $.

Analysis: enumeration waiting time to meet the conditions namely break.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1010;
 4 int a[maxn],b[maxn],c[maxn*2];
 5 int n,m;
 6 int main() {
 7     while (~scanf("%d%d",&n,&m)){
 8         for (int i=1;i<=n;i++) scanf("%d",&a[i]);
 9         for (int i=1;i<=m;i++) scanf("%d",&b[i]);
10         sort(a+1,a+1+n);
11         sort(b+1,b+1+m);
12         int kk=0;
13         while (1){
14             int num=0;
15             for (int i=1;i<=n;i++) c[++num]=a[i];
16             for (int i=1;i<=m;i++) c[++num]=b[i]+kk;
17             sort(c+1,c+1+num);
18             int m=unique(c+1,c+1+num)-(c+1);
19             if (m==num) break;
20             else kk++;
21         }
22         printf("%d\n",kk);
23     }
24     return 0;
25 }
G

H.Rng

Transmission: http://acm.hdu.edu.cn/showproblem.php?pid=6574

The meaning of problems: in the range of $ 1-n $ to select the right boundary interval $ r $, $ L then select the left boundary in the $ $ 1-r $. Take any two probability asked how much intersects interval.

Data range: $ 1 <= n <= 10 ^ 6 $.

Analysis: The inclusion-exclusion principle. Disjoint probability analysis of how much.

And a school girl who push QAQ.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int mod=1e9+7;
 5 ll pow_(ll x,int y){
 6     ll res=1,base=1ll*x;
 7     while (y){
 8         if (y&1) (res*=base)%=mod;
 9         (base*=base)%=mod;
10         y>>=1;
11     }
12     return res;
13 }
14 int main(){
15     int n;
16     while (~scanf("%d",&n)){
17         ll ans=(1ll*(n+1)*pow_(2ll*n,mod-2))%mod;
18         printf("%lld\n",ans);
19     }
20     return 0;
21 } 
H

I.Budget

Transmission: http://acm.hdu.edu.cn/showproblem.php?pid=6575

Meaning of the questions: There are $ n $ th three decimal places, rounded to two decimal places requirements, each number after rounding taking the difference of the original number, ask all the difference and how much.

Data range: $ 1 <= n <= 1000 $.

Analysis: sign.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e5+7;
 4 int main()
 5 {
 6     int n;
 7     while (~scanf("%d",&n))
 8     {
 9         string s;
10         double ans=0;
11         while (n--)
12         {
13             cin>>s;
14             int st=s.length(),f;
15             for (int i=0;i<st;i++) 
16                 if(s[i]=='.'){f=i;break;}
17             int x=s[f+3]-'0';
18             if(x>=5) ans+=10-x;
19             else ans-=x;
20         }
21         ans/=1000.0;
22         printf("%.3f\n",years);
23      }
 24      return  0 ;
25 }
I

J.Worker

Transmission: http://acm.hdu.edu.cn/showproblem.php?pid=6576

Meaning of the questions: There are two workshops $ n $, $ m $ workers, each workplace to complete the workload per person per day is $ a_i $, $ m $ and asked whether these individuals assigned to this $ n $ th workplace, the same requirements for each workshop to complete the daily workload. Each output can be assigned the number of workplace allocation.

Data range: $ 1 <= n <= 1000,1 <= m <= 10 ^ {18} $.

Analysis: Consider all $ a_i $ is the least common multiple, then the ratio of the number of people in each workplace, and then determine whether or not separable. Sign ++.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=1010;
 5 ll a[maxn],b[maxn];
 6 ll gcd_(ll xx,ll yy){
 7     if (yy==0) return xx;
 8     else return gcd_(yy,xx%yy);
 9 }
10 int main(){
11     ll n,m;
12     while (~scanf("%lld%lld",&n,&m)){
13         for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
14         ll tmp=a[1];
15         for (int i=2;i<=n;i++){
16             ll kk=gcd_(a[i],tmp);
17             tmp=tmp/kk*a[i];
18         }
19         ll sum=0;
20         for (int i=1;i<=n;i++){
21             b[i]=tmp/a[i];
22             sum+=b[i];
23         }
24         if (m%sum!=0){
25             printf("No\n");
26             continue;
27         }
28         printf("Yes\n"); 
29         ll t=m/sum;
30         printf("%lld",t*b[1]);
31         for (int i=2;i<=n;i++){
32             printf(" %lld",t*b[i]);
33         }
34         printf("\n");
35     }
36     return 0;
37 }
J

K.Class

Transmission: http://acm.hdu.edu.cn/showproblem.php?pid=6577

The meaning of problems: Known $ x = a + b, y = ab $. Solving $ a * b $.

Analysis: $ a = \ frac {x + y} {2}, b = \ frac {xy} {2} $. Sign ++.

1 #include<bits/stdc++.h>
2 using namespace std;
3 int main(){
4     int x,y;scanf("%d%d",&x,&y);
5     int tmp=(x*x-y*y)/4;
6     printf("%d\n",tmp);
7     return 0;
8 } 
K

 

 

Guess you like

Origin www.cnblogs.com/changer-qyz/p/11223404.html