Codeforces Round #384 (Div. 2)A,B,C,D
Vladik is a competitive programmer. This year he is going to win the International Olympiad in Informatics. But it is not as easy as it sounds: the question Vladik face now is to find the cheapest way to get to the olympiad.
Vladik knows n airports. All the airports are located on a straight line. Each airport has unique id from 1 to n, Vladik's house is situated next to the airport with id a, and the place of the olympiad is situated next to the airport with id b. It is possible that Vladik's house and the place of the olympiad are located near the same airport.
To get to the olympiad, Vladik can fly between any pair of airports any number of times, but he has to start his route at the airport a and finish it at the airport b.
Each airport belongs to one of two companies. The cost of flight from the airport i to the airport j is zero if both airports belong to the same company, and |i - j| if they belong to different companies.
Print the minimum cost Vladik has to pay to get to the olympiad.
The first line contains three integers n, a, and b (1 ≤ n ≤ 105, 1 ≤ a, b ≤ n) — the number of airports, the id of the airport from which Vladik starts his route and the id of the airport which he has to reach.
The second line contains a string with length n, which consists only of characters 0 and 1. If the i-th character in this string is 0, then i-th airport belongs to first company, otherwise it belongs to the second.
Print single integer — the minimum cost Vladik has to pay to get to the olympiad.
4 1 4
1010
1
5 5 2
10110
0
In the first example Vladik can fly to the airport 2 at first and pay |1 - 2| = 1 (because the airports belong to different companies), and then fly from the airport 2 to the airport 4 for free (because the airports belong to the same company). So the cost of the whole flight is equal to 1. It's impossible to get to the olympiad for free, so the answer is equal to 1.
In the second example Vladik can fly directly from the airport 5 to the airport 2, because they belong to the same company.
题意:给你一个01串,要你从a走到b,相同字符走不用花费,否则花费|i-j|;
思路:如果字符串全相等,输出0,否则
如果a,b位置两字符相等输出0,其他肯定有01想接的位置;
就是a,b相等输出0,否则输出1;
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 const int N=1e5+10,M=1e6+10,inf=1e9+10; const ll INF=1e18+10,mod=2147493647; char a[N]; int main() { int n,aa,bb; scanf("%d%d%d",&n,&aa,&bb); scanf("%s",a+1); if(a[aa]==a[bb]) { return puts("0"); } printf("1\n"); return 0; }
Chloe, the same as Vladik, is a competitive programmer. She didn't have any problems to get to the olympiad like Vladik, but she was confused by the task proposed on the olympiad.
Let's consider the following algorithm of generating a sequence of integers. Initially we have a sequence consisting of a single element equal to 1. Then we perform (n - 1) steps. On each step we take the sequence we've got on the previous step, append it to the end of itself and insert in the middle the minimum positive integer we haven't used before. For example, we get the sequence [1, 2, 1] after the first step, the sequence [1, 2, 1, 3, 1, 2, 1] after the second step.
The task is to find the value of the element with index k (the elements are numbered from 1) in the obtained sequence, i. e. after (n - 1)steps.
Please help Chloe to solve the problem!
The only line contains two integers n and k (1 ≤ n ≤ 50, 1 ≤ k ≤ 2n - 1).
Print single integer — the integer at the k-th position in the obtained sequence.
3 2
2
4 8
4
In the first sample the obtained sequence is [1, 2, 1, 3, 1, 2, 1]. The number on the second position is 2.
In the second sample the obtained sequence is [1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1]. The number on the eighth position is 4.
题意:给你些数,下一次变换,需要加中间的数+1,复制一遍;
思路:按题意逆推回去就可以
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 const int N=1e5+10,M=1e6+10,inf=1e9+10; const ll INF=1e18+10,mod=2147493647; map<ll,int>m; vector<ll>v; int dfs(ll x) { if(x==1) return 1; if(m[x]) return dfs(x/2)+1; int pos=upper_bound(v.begin(),v.end(),x)-v.begin()-1; return dfs(x-v[pos]); } int main() { ll num=1; v.push_back(1LL); for(int i=1;i<52;i++) { num*=2; m[num]=1; v.push_back(num); } ll n,k; scanf("%lld%lld",&n,&k); printf("%d\n",dfs(k)); return 0; }
Vladik and Chloe decided to determine who of them is better at math. Vladik claimed that for any positive integer n he can represent fraction as a sum of three distinct positive fractions in form
.
Help Vladik with that, i.e for a given n find three distinct positive integers x, y and z such that . Because Chloe can't check Vladik's answer if the numbers are large, he asks you to print numbers not exceeding 109.
If there is no such answer, print -1.
The single line contains single integer n (1 ≤ n ≤ 104).
If the answer exists, print 3 distinct numbers x, y and z (1 ≤ x, y, z ≤ 109, x ≠ y, x ≠ z, y ≠ z). Otherwise print -1.
If there are multiple answers, print any of them.
3
2 7 42
7
7 8 56
题意:2/n=1/x+1/y+1/z,给你n找x,y,z;
思路:想到就是想到了,这种题;
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 const int N=1e5+10,M=1e6+10,inf=1e9+10; const ll INF=1e18+10,mod=2147493647; ll n; int main() { scanf("%lld",&n); if(n==1) printf("-1"); else printf("%lld %lld %lld\n",n,n+1,n*(n+1)); return 0; }
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integerai — pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer — the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
25
4
1 -5 1 1
1 2
1 4
2 3
2
1
-1
Impossible
题意:给你一颗树(根为1),每个节点上面有个权值,可以割两条边,求能得到的权值最大和;
思路:树形dp水题,dp[i][1]表示以i为根的割一次的最大值;
dp[i][2]表示 二 ;
搓代码;
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 const int N=2e5+10,M=1e6+10,inf=1e9+10; const ll INF=1e18+10,mod=2147493647; struct is { int v; int next; }edge[N<<1]; int head[N<<1],edg; ll sum[N],a[N],dp[N][5]; void init() { memset(head,-1,sizeof(head)); edg=0; } void add(int u,int v) { edg++; edge[edg].v=v; edge[edg].next=head[u]; head[u]=edg; } void dfs(int u,int fa) { sum[u]=a[u]; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==fa)continue; dfs(v,u); sum[u]+=sum[v]; } } void dfs1(int u,int fa) { priority_queue<ll,vector<ll>,greater<ll> >q; ll ans2=-INF,ans1=-INF; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==fa)continue; dfs1(v,u); ans2=max(ans2,dp[v][2]); ans1=max(ans1,dp[v][1]); q.push(dp[v][1]); while(q.size()>2) { q.pop(); } } dp[u][1]=max(dp[u][1],ans1); dp[u][2]=max(dp[u][2],ans2); dp[u][1]=max(dp[u][1],sum[u]); ll s1=-INF,s2=-INF,p=1,s3; while(!q.empty()) { if(p==1) s1=q.top(); else s2=q.top(); q.pop(); p++; } if(s2==-INF) s3=-INF; else s3=s2+s1; dp[u][2]=max(dp[u][2],s3); } int main() { init(); int n; scanf("%d",&n); for(int i=0;i<=n;i++) dp[i][1]=dp[i][2]=-INF; for(int i=1;i<=n;i++) scanf("%lld",&a[i]); for(int i=1;i<n;i++) { int u,v; scanf("%d%d",&u,&v); add(u,v); add(v,u); } dfs(1,-1); dfs1(1,-1); //for(int i=1;i<=n;i++) //cout<<dp[i][1]<<" "<<dp[i][2]<<endl; if(dp[1][2]>-INF) printf("%lld\n",dp[1][2]); else puts("Impossible\n"); return 0; }