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

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.

题意:n个机场 起点a 终点b  给你长度魏n的01串  符号相同表示机场属于同一家公司  相同公司通航免费 不同公司通航费用为abs(i-j) 输出最小的花费

题解:细细分析一下  相同公司的花费为零 不同公司任意点的花费为1   水

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
const int N=;
using namespace std;
int n,a,b;
char c[];
int main()
{
scanf("%d %d %d",&n,&a,&b);
getchar();
for(int i=;i<=n;i++)
scanf("%c",&c[i]);
if(c[a]==c[b])
{
printf("0\n");
return ;
}
else
{
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.

题意:按照题目要求生成序列 问第k个数是什么

题解:将k转换为二进制 可以发现结果为末尾零的个数加一

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
const int N=;
using namespace std;
ll n,k;
int main()
{
scanf("%I64d %I64d",&k,&n);
ll ans=;
while(((n>>ans)&)==) ans++;
printf("%d\n",ans+);
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 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.

Input

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

Output

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.

Examples
Input
3
Output
2 7 42
Input
7
Output
7 8 56
题意:公式题目
题解:我是莫名其妙暴力出来的
看了别人的题解 是直接分解式子的 2/n=1/n+1/(n+1)+1/n*(n+1)
 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
const int N=;
using namespace std;
int n;
ll x1,x2,x3;
int main()
{
scanf("%d",&n);
int i=n;
while(i)
{
if(*i>n)
{
x1=i;
break;
}
i++;
}
ll zi1,mu1;
zi1=*x1-n;
mu1=n*x1;
i=x1+;
while(i)
{
if(zi1*i>mu1)
{
x2=i;
break;
}
i++;
}
ll zi2,mu2;
zi2=zi1*x2-mu1;
mu2=mu1*x2;
if(mu2%zi2==)
{
x3=mu2/zi2;
if(x1!=x3&&x2!=x3)
{
cout<<x1<<" "<<x2<<" "<<x3<<endl;
return ;
}
}
cout<<"-1"<<endl;
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 integer ai — 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 ≤ 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.

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
题意:给你一颗树 树有点权 现在找到两个不关联的子树 输出最大的子树点权和
题解:dfs序将子树转化为区间 在n个区间中选取两个不相交的区间 输出最大的权值和。(具体看代码)
 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace std;
int n;
ll a[];
int u,v;
int pre[];
ll d[];
int in[];
int out[];
ll dp[];
int nedge=;
struct node
{
int from;
int to;
}N[];
struct xx
{
int l;
int r;
ll w;
}M[];
void add(int from ,int to)
{
nedge++;
N[nedge].to=to;
N[nedge].from=pre[from];
pre[from]=nedge;
}
int dfn=;
ll sum=;
map<int,int> mp;
map<int,int>mpp;
void getdfs(int root)
{
sum+=a[root];
in[root]=++dfn;
d[dfn]=sum;
mp[dfn]=root;
mpp[root]=;
for(int i=pre[root];i;i=N[i].from)
{
if(mpp[N[i].to])
continue;
getdfs(N[i].to);
}
out[root]=dfn;
}
bool cmp(struct xx aa,struct xx bb)
{
return aa.r<bb.r;
}
int main()
{
memset(pre,,sizeof(pre));
mp.clear();
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%I64d",&a[i]);
for(int i=;i<=n-;i++)
{
scanf("%d %d",&u,&v);
add(u,v);
add(v,u);
}
getdfs();
for(int i=;i<=n;i++)
{
M[i].l=in[i];
M[i].r=out[i];
M[i].w=d[out[i]]-d[in[i]]+a[mp[in[i]]];
}
sort(M+,M++n,cmp);
ll ans=-1e18-;
ll maxn=-1e18-;
int flag=;
for(int i=;i<=n;i++)
dp[i]=-1e18-;
for(int i=;i<=n;i++)
{
maxn=max(M[i].w,maxn);
dp[M[i].r]=maxn;
}
for(int i=;i<=n;i++)
{
if(dp[i]<=dp[i-])
dp[i]=dp[i-];
}
for(int i=;i<=n;i++){
ans=max(ans,M[i].w+dp[M[i].l-]);
if(dp[M[i].l-]!=(-1e18-))
flag=;
}
if(flag==)
printf("Impossible\n");
else
printf("%I64d\n",ans);
return ;
}
/*
10
-1 2 -2 -3 -1 -1 0 -4 -5 -4
4 6
6 9
1 2
6 2
7 8
7 9
5 10
6 3
10 1
*/

Codeforces Round #384 (Div. 2) A B C D dfs序+求两个不相交区间 最大权值和的更多相关文章

  1. Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)

    思路: dfs序其实是很水的东西.  和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...

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

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

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

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

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

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

  7. Codeforces Round #384(div 2)

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

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

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

随机推荐

  1. CsvHelper文档-6类型转换

    CsvHelper文档-6类型转换 CsvHelper使用类型转换器来转换string到对象,或者对象到string: ITypeConverter 类型转换器的结构,必须实现: public int ...

  2. [C++基础] 成员变量的初始化顺序

    转载链接:https://blog.csdn.net/qq_37059483/article/details/78608375 1.成员变量在使用初始化列表初始化时,只与定义成员变量的顺序有关,与构造 ...

  3. shell命令之at 执行一次性定时任务的用法

    大家都知道crontab是执行定时任务的命令,那么at又是什么呢? 其实at也是定时任务命令,不同的是crontab是执行循环任务,at执行一次性任务 首先说下时间例子 Minute    at no ...

  4. python正则表达式中含有变量的写法

    使用格式化字符串的方式实现举例: re.findall("(this,%s,'(.*?)'"%str(i),"abcd(this,1,'123123)')这里i为变量  

  5. $_SERVER的详细参数整理下

    PHP编程中经常需要用到一些服务器的一些资料,特把$_SERVER的详细参数整理下,方便以后使用. $_SERVER['PHP_SELF'] #当前正在执行 脚本的文件名,与 document roo ...

  6. Java 学习笔记 ------第一章 Java平台概论

    本章学习目标: Java版本迁移简介 认识Java SE.Java EE.Java ME 认识JDK规范与操作 了解JVM.JRE与JDK 下载与安装JDK 一.Java版本迁移简介 书上已经表达得非 ...

  7. 软工实践-Alpha 冲刺 (8/10)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 已经解决登录注册等基本功能的界面. 完成非功能的主界面制作 ...

  8. HDU 5228 ZCC loves straight flush 暴力

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5228 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  9. <<世界是数字的>>读书笔记

    <世界是数字的>这本书是大学职业规划老师介绍个我读的,从着本中我学到了很多. 第一章,计算机里有什么.这个问题可以从两方面来看:逻辑上或者说功能上的组成,即每一部分是什么.做什么.怎样做. ...

  10. Ubuntu命令行安装显卡驱动

    1. sudo apt-et purge nvidia* 卸载原有驱动 2. sudo add-apt-repository ppa:graphics-drivers sudo apt-get upd ...