Codeforces 702 (Educational Codeforces Round 15) A~E

来源:互联网 发布:有谁双飞过 知乎 编辑:程序博客网 时间:2024/05/21 06:49

A. Maximum Increase
time limit per test1 second
memory limit per test256 megabytes
input standard input
output standard output

You are given array consisting of n integers. Your task is to find the maximum length of an increasing subarray of the given array.

A subarray is the sequence of consecutive elements of the array. Subarray is called increasing if each element of this subarray strictly greater than previous.

Input
The first line contains single positive integer n (1 ≤ n ≤ 105) — the number of integers.

The second line contains n positive integers a1, a2, …, an (1 ≤ ai ≤ 109).

Output
Print the maximum length of an increasing subarray of the given array.

Examples

input
5
1 7 2 11 15

output
3

input
6
100 100 100 100 100 100

output
1

input
3
1 2 3

output
3


边读边处理即可

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#define AUTO "%I64d"using namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)const int INF=0x3f3f3f3f;const int maxn = 100005;int n;int a[maxn];int main(){    scanf("%d",&n);    for(int i=1;i<=n;i++) scanf("%d",a+i);    int ans = 0 , last = -1 , tmp = 0;    a[n+1] = -1;    for(int i=1;i<=n+1;i++)        if(last<a[i]) tmp++,last=a[i];        else smax(ans,tmp),tmp=1,last=a[i];    printf("%d",ans);    return 0;}

B. Powers of Two
time limit per test3 seconds
memory limit per test256 megabytes
input standard input
output standard output

You are given n integers a1, a2, …, an. Find the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2 (i. e. some integer x exists so that ai + aj = 2x).

Input
The first line contains the single positive integer n (1 ≤ n ≤ 105) — the number of integers.

The second line contains n positive integers a1, a2, …, an (1 ≤ ai ≤ 109).

Output
Print the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2.

Examples

input
4
7 3 2 1

output
2

input
3
1 1 1

output
3

Note
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).

In the second example all pairs of indexes (i, j) (where i < j) include in answer.


用map存出现的次数,相乘的和就是答案,不过要注意剪枝!! k小的时候没有就要break掉

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#define AUTO "%I64d"using namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)const int INF=0x3f3f3f3f;const int maxn = 100005;typedef long long LL;map <int,int> g;int n,maxnum;LL ans = 0ll;int main(){    scanf("%d",&n);    for(int i=1;i<=n;i++)    {        int tmp;        scanf("%d",&tmp);        smax(maxnum,tmp);        if(!g.count(tmp)) g[tmp]=1;        else g[tmp]++;    }    for(map<int,int>::iterator it = g.begin();it!=g.end();it++)    {        int u = (*it).first , tot = (*it).second;        for(int k=1;(1<<k)<=(maxnum<<1);k++)        {            int v = (1<<k) - u;            if(v>u) break; // if continue , TLE !!            if(!g.count(v)) continue;            if(u^v) ans += (LL)tot*g[v];            else ans += (LL)tot*(tot-1)/2;        }    }    printf(AUTO,ans);    return 0;}

C. Cellular Network
time limit per test3 seconds
memory limit per test256 megabytes
input standard input
output standard output

You are given n points on the straight line — the positions (x-coordinates) of the cities and m points on the same line — the positions (x-coordinates) of the cellular towers. All towers work in the same way — they provide cellular network for all cities, which are located at the distance which is no more than r from this tower.

Your task is to find minimal r that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than r.

If r = 0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than r from this tower.

Input
The first line contains two positive integers n and m (1 ≤ n, m ≤ 105) — the number of cities and the number of cellular towers.

The second line contains a sequence of n integers a1, a2, …, an ( - 109 ≤ ai ≤ 109) — the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates ai are given in non-decreasing order.

The third line contains a sequence of m integers b1, b2, …, bm ( - 109 ≤ bj ≤ 109) — the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates bj are given in non-decreasing order.

Output
Print minimal r so that each city will be covered by cellular network.

Examples

input
3 2
-2 2 4
-3 0

output
4

input
5 3
1 5 10 14 17
4 11 15

output
3


首先想到二分答案,但是check时要用双指针法!!

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#define AUTO "%I64d"using namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)typedef long long LL;const LL INF=(1ll<<60);const int maxn = 100005;int ln,lm,n,m;LL a[maxn],b[maxn];inline bool f(LL r){    int i=1,j=1;    while(true)    {        if(abs(a[i]-b[j])<=r) i++;        else j++;        if(i>n) return true;        if(j>m) return false;    }}LL work(){    LL l=0ll,r=(1ll<<31);    while(l<r)    {        LL m = (l+r)>>1;        bool cur = f(m);        if(cur) r = m;        else l = m + 1;    }    return l;}int main(){    scanf("%d%d",&ln,&lm);    LL last = -INF;    for(int i=1;i<=ln;i++)    {        LL tmp;        scanf(AUTO,&tmp);        if(tmp^last) a[++n] = tmp , last = tmp;    }    last = -INF;    for(int i=1;i<=lm;i++)    {        LL tmp;        scanf(AUTO,&tmp);        if(tmp^last) b[++m] = tmp , last = tmp;    }    LL ans = work();    printf(AUTO,ans);    return 0;}

D. Road to Post Office
time limit per test1 second
memory limit per test256 megabytes
input standard input
output standard output

Vasiliy has a car and he wants to get from home to the post office. The distance which he needs to pass equals to d kilometers.

Vasiliy’s car is not new — it breaks after driven every k kilometers and Vasiliy needs t seconds to repair it. After repairing his car Vasiliy can drive again (but after k kilometers it will break again, and so on). In the beginning of the trip the car is just from repair station.

To drive one kilometer on car Vasiliy spends a seconds, to walk one kilometer on foot he needs b seconds (a < b).

Your task is to find minimal time after which Vasiliy will be able to reach the post office. Consider that in every moment of time Vasiliy can left his car and start to go on foot.

Input
The first line contains 5 positive integers d, k, a, b, t (1 ≤ d ≤ 1012; 1 ≤ k, a, b, t ≤ 106; a < b), where:

d — the distance from home to the post office;
k — the distance, which car is able to drive before breaking;
a — the time, which Vasiliy spends to drive 1 kilometer on his car;
b — the time, which Vasiliy spends to walk 1 kilometer on foot;
t — the time, which Vasiliy spends to repair his car.
Output
Print the minimal time after which Vasiliy will be able to reach the post office.

Examples

input
5 2 1 4 10

output
14

input
5 2 1 4 5

output
13

Note
In the first example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds) and then to walk on foot 3 kilometers (in 12 seconds). So the answer equals to 14 seconds.

In the second example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds), then repair his car (in 5 seconds) and drive 2 kilometers more on the car (in 2 seconds). After that he needs to walk on foot 1 kilometer (in 4 seconds). So the answer equals to 13 seconds.


这题一开始想到dp,但是d是10^12数量级,不可承受的
仔细想想发现这是有周期性的,那么算出走路和 开♂车 的平均速度,采用快的方式走到还剩 小于 k 距离的地方 (一开始默认开♀车,答案必定最优),最后的距离模拟比较大小即可

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#define AUTO "%I64d"using namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)typedef long long LL;LL d,k,a,b,t;inline void print(LL x){    printf(AUTO,x);    exit(0);}int main(){//  freopen("d.in","r",stdin);//  freopen("d.out","w",stdout);    scanf(AUTO AUTO AUTO AUTO AUTO ,&d,&k,&a,&b,&t);    LL ans = 0ll;    LL cur = d;    if(d<=k) print(d*a);    cur -= k; ans += a*k;    double aver_car = (double) k / (a*k+t);    double aver_walk = 1.0 / b;    if(aver_walk > aver_car || fabs(aver_walk-aver_car)<1e-10) print(ans + cur*b); // mininum to 1e-6 must be far more exact!!    ans += (cur - cur%k)/k * (a*k+t); // repeat round until the left is less than k kilometers    cur %= k;    if(a*cur + t > b*cur) ans += b * cur;    else ans += a*cur+t;    print(ans);    return 0;}

E. Analysis of Pathes in Functional Graph
time limit per test2 seconds
memory limit per test512 megabytes
input standard input
output standard output

You are given a functional graph. It is a directed graph, in which from each vertex goes exactly one arc. The vertices are numerated from 0 to n - 1.

Graph is given as the array f0, f1, …, fn - 1, where fi — the number of vertex to which goes the only arc from the vertex i. Besides you are given array with weights of the arcs w0, w1, …, wn - 1, where wi — the arc weight from i to fi.

The graph from the first sample test.
Also you are given the integer k (the length of the path) and you need to find for each vertex two numbers si and mi, where:

si — the sum of the weights of all arcs of the path with length equals to k which starts from the vertex i;
mi — the minimal weight from all arcs on the path with length k which starts from the vertex i.
The length of the path is the number of arcs on this path.

Input
The first line contains two integers n, k (1 ≤ n ≤ 105, 1 ≤ k ≤ 1010). The second line contains the sequence f0, f1, …, fn - 1 (0 ≤ fi < n) and the third — the sequence w0, w1, …, wn - 1 (0 ≤ wi ≤ 108).

Output
Print n lines, the pair of integers si, mi in each line.

Examples

input
7 3
1 2 3 4 3 2 6
6 3 1 4 2 2 3

output
10 1
8 1
7 1
10 2
8 2
7 1
9 3

input
4 4
0 1 2 3
0 1 2 3

output
0 0
4 1
8 2
12 3

input
5 3
1 2 3 4 0
4 1 2 14 3

output
7 1
17 1
19 2
21 3
8 1


倍增算法即可,由于始终没有终点,细节就可以不管了

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#define AUTO "%I64d"using namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)typedef long long LL;const LL INF = (1ll<<60);const int maxn = 100005;const int maxd = 45;const int D = 40; // k max ranged 1e10 , 20 is far not enough !!  coz circle exist!!LL fa[maxn][maxd],m[maxn][maxd],s[maxn][maxd];LL n,len;void ST(){    for(int k=1;k<=D;k++)        for(int i=1;i<=n;i++)        {            fa[i][k] = fa[fa[i][k-1]][k-1];            s[i][k] = s[i][k-1] + s[fa[i][k-1]][k-1];            m[i][k] = min(m[i][k-1] , m[fa[i][k-1]][k-1]);        }}pair <LL,LL> query(LL u,LL cur){    LL rets = 0ll , retm = INF;    for(int k=D;k>=0;k--)        if(cur>=(1ll<<k)) rets += s[u][k] , smin(retm,m[u][k]) , u = fa[u][k] , cur -= (1ll<<k); // care the exceeding of int!!!    return make_pair(rets,retm);}int main(){    //freopen("e.in","r",stdin);    //freopen("e.out","w",stdout);    scanf(AUTO AUTO,&n,&len);    for(int i=1;i<=n;i++) scanf(AUTO,&fa[i][0]),fa[i][0]++; // use 1 ~ n rather than 0 ~ n-1    for(int i=1;i<=n;i++) scanf(AUTO,&m[i][0]),s[i][0]=m[i][0];    ST();    for(int i=1;i<=n;i++)    {        pair <LL,LL> ans = query(i,len);        printf(AUTO" "AUTO"\n",ans.first,ans.second);    }    return 0;}
0 0
原创粉丝点击