A. Vladik and flights
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

The first line contains three integers na, 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.

Output

Print single integer — the minimum cost Vladik has to pay to get to the olympiad.

Examples
input
4 1 4
1010
output
1
input
5 5 2
10110
output
0
Note

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+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
char a[N];
int main()
{
int n,aa,bb;
scanf("%d%d%d",&n,&aa,&bb);
scanf("%s",a+);
if(a[aa]==a[bb])
{
return puts("");
}
printf("1\n");
return ;
}
B. Chloe and the sequence
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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!

Input

The only line contains two integers n and k (1 ≤ n ≤ 50, 1 ≤ k ≤ 2n - 1).

Output

Print single integer — the integer at the k-th position in the obtained sequence.

Examples
input
3 2
output
2
input
4 8
output
4
Note

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+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
map<ll,int>m;
vector<ll>v;
int dfs(ll x)
{
if(x==)
return ;
if(m[x])
return dfs(x/)+;
int pos=upper_bound(v.begin(),v.end(),x)-v.begin()-;
return dfs(x-v[pos]);
}
int main()
{
ll num=;
v.push_back(1LL);
for(int i=;i<;i++)
{
num*=;
m[num]=;
v.push_back(num);
}
ll n,k;
scanf("%lld%lld",&n,&k);
printf("%d\n",dfs(k));
return ;
}
C. Vladik and fractions
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 xy 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.

Input

The single line contains single integer n (1 ≤ n ≤ 104).

Output

If the answer exists, print 3 distinct numbers xy and z (1 ≤ x, y, z ≤ 109, x ≠ yx ≠ zy ≠ z). Otherwise print -1.

If there are multiple answers, print any of them.

Examples
input
3
output
2 7 42
input
7
output
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+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
ll n;
int main()
{
scanf("%lld",&n);
if(n==)
printf("-1");
else
printf("%lld %lld %lld\n",n,n+,n*(n+));
return ;
}
D. Chloe and pleasant prizes
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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 ≤ nui ≠ 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.

Output

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.

Examples
input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
output
25
input
4
1 -5 1 1
1 2
1 4
2 3
output
2
input
1
-1
output
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+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
struct is
{
int v;
int next;
}edge[N<<];
int head[N<<],edg;
ll sum[N],a[N],dp[N][];
void init()
{
memset(head,-,sizeof(head));
edg=;
}
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!=-;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!=-;i=edge[i].next)
{
int v=edge[i].v;
if(v==fa)continue;
dfs1(v,u);
ans2=max(ans2,dp[v][]);
ans1=max(ans1,dp[v][]);
q.push(dp[v][]);
while(q.size()>)
{
q.pop();
}
}
dp[u][]=max(dp[u][],ans1);
dp[u][]=max(dp[u][],ans2);
dp[u][]=max(dp[u][],sum[u]);
ll s1=-INF,s2=-INF,p=,s3;
while(!q.empty())
{
if(p==)
s1=q.top();
else
s2=q.top();
q.pop();
p++;
}
if(s2==-INF)
s3=-INF;
else
s3=s2+s1;
dp[u][]=max(dp[u][],s3);
}
int main()
{
init();
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
dp[i][]=dp[i][]=-INF;
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
for(int i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(,-);
dfs1(,-);
//for(int i=1;i<=n;i++)
//cout<<dp[i][1]<<" "<<dp[i][2]<<endl;
if(dp[][]>-INF)
printf("%lld\n",dp[][]);
else
puts("Impossible\n");
return ;
}

Codeforces Round #384 (Div. 2)A,B,C,D的更多相关文章

  1. Codeforces Round #384 (Div. 2) E. Vladik and cards 状压dp

    E. Vladik and cards 题目链接 http://codeforces.com/contest/743/problem/E 题面 Vladik was bored on his way ...

  2. Codeforces Round #384 (Div. 2)D - Chloe and pleasant prizes 树形dp

    D - Chloe and pleasant prizes 链接 http://codeforces.com/contest/743/problem/D 题面 Generous sponsors of ...

  3. Codeforces Round #384 (Div. 2) C. Vladik and fractions 构造题

    C. Vladik and fractions 题目链接 http://codeforces.com/contest/743/problem/C 题面 Vladik and Chloe decided ...

  4. Codeforces Round #384 (Div. 2)B. Chloe and the sequence 数学

    B. Chloe and the sequence 题目链接 http://codeforces.com/contest/743/problem/B 题面 Chloe, the same as Vla ...

  5. Codeforces Round #384 (Div. 2) A. Vladik and flights 水题

    A. Vladik and flights 题目链接 http://codeforces.com/contest/743/problem/A 题面 Vladik is a competitive pr ...

  6. Codeforces Round #384(div 2)

    A 题意:有n个机场处于一直线上,可两两到达,每个机场只可能属于两家公司中的一家(用0,1表示),现在要从a机场到b机场,可任意次转机.若机场i与机场j从属同一公司,则费用为0,否则费用为1.问最小费 ...

  7. Codeforces Round #384 (Div. 2) C. Vladik and fractions(构造题)

    传送门 Description Vladik and Chloe decided to determine who of them is better at math. Vladik claimed ...

  8. Codeforces Round #384 (Div. 2) B. Chloe and the sequence(规律题)

    传送门 Description Chloe, the same as Vladik, is a competitive programmer. She didn't have any problems ...

  9. Codeforces Round #384 (Div. 2) 734E Vladik and cards

    E. Vladik and cards time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  10. Codeforces Round #384 (Div. 2)D-Chloe and pleasant prizes

    D. Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. Java程序设计 实验三

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java程序设计   班级:1353       姓名:李海空  学号:20135329 成绩:             指 ...

  2. [转]MongoDB密码设置(基于windows)

    参考文档:http://www.cnblogs.com/zengen/archive/2011/04/23/2025722.html   MongoDB部署到Windows上后是默认是无权限限制的的. ...

  3. C# Lock 解读[转]

    一.Lock定义     lock 关键字可以用来确保代码块完成运行,而不会被其他线程中断.它可以把一段代码定义为互斥段(critical section),互斥段在一个时刻内只允许一个线程进入执行, ...

  4. 解决ssh登录后闲置时间过长而断开连接

     ++++++++++++++++++++++++++++ #!/usr/bin/env bash password_5="Uxxx7"host_5="129.x.x.1 ...

  5. JAVA NIO系列(四) 选择器

    前面介绍过Channel.Buffer,后面的文章主要讲解Selector的实践以及实现原理,选择器的概念比起通道.缓冲区要复杂一些,并且选择器是NIO中最重要的一部分内容. 为什么使用Selecto ...

  6. gradle基础的build文件模板_jetty

    group '组织名' version '版本号' /* 支持的插件 */ apply plugin: 'java' // 项目基础变成语言支持为java apply plugin: 'war' // ...

  7. 摸索js的3d全景

    先我在网上找到了一个例子,http://silali.vicp.net/three/emaple.html 完美实现3d全景,在详细查看这个例子后,发现他在手机上运行并不流畅,而且显不全并会卡顿. 我 ...

  8. Linux 多线程编程 实例 1

    子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码. #include <pthread.h> ...

  9. Linux之保留yum安装软件后的RPM包

    yum安装软件很方便,但是下载下来的rpm包在安装后默认会被删除掉: 如果希望保留yum安装的软件包该如何做呢? 设置方法: 将/etc/yum.conf里对应的keepcache参数改为1即可,然后 ...

  10. Effective_Python mapreduce

    完全吊炸天构造器的写法... import os import threading,time class GenericInputData(object): def read(self): raise ...