Educational Codeforces Round 15

昨天做cf的时候自己跟自己抢了一晚上的网。。。。。大半的时间都花在验证网络账号。就因为手机和pc端不能同时连。。

好了,进入正题

A. Maximum Increase
time limit per test
1 second
memory limit per test
256 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<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
using namespace std;
const int N=110000;
int f[N];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	scanf("%d",&f[i]);
	int ans=1;
	int cnt=1;
	for(int i=1;i<n;i++)
	{
		if(f[i]>f[i-1]){
			cnt++;
			ans=max(ans,cnt);
		}			
		else
			cnt=1;
	}
	printf("%d\n",ans);
	return 0;
}

B. Powers of Two
time limit per test
3 seconds
memory limit per test
256 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 xexists 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.

我们先把2*10^9之内2的次方幂都处理出来。然后用map去统计序列中的数出现了几次。然后在暴力枚举一个2的次幂,然后减去a[i],得到cnt。然后ans+=mp[a[i]],最后要注意一下a[i]和tmp相同时时ans+=mp[a[i]]-1;

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<stdlib.h> 
using namespace std;
const int N=110000;
long long f[35];
map<long long,int>mp;
long long a[N];
int main()
{
	int n;
    int cnt=1;
    f[0]=1;
    for(int i=1;f[i]<=2*1e9+10;i++)
    {
    	f[i]=f[i-1]*2;
    	if(f[i]>2*1e9+10)
    	break;
    }
    
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
    	scanf("%I64d",&a[i]);
    	mp[a[i]]++;
    }
    long long ans=0;
    for(int i=0;i<=34;i++)
    {
    	for(int j=0;j<n;j++)
    	{
    		long long tmp=f[i]-a[j];
    		if(a[j]==tmp)
    		{
    		   ans+=mp[tmp]-1;
    		}
    	    else
    		ans+=mp[tmp];
    	}
    }
    printf("%I64d\n",ans/2);
    return 0;
}

C. Cellular Network
time limit per test
3 seconds
memory limit per test
256 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
对于每一个城市他要么就是选择最靠近他的左边的塔,要么就是右边的塔。所以我们先把表示塔的下标记录下来。然后0-n+m扫。遇到城市就比较是取左边还是右边的塔,更新最大值

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<stdlib.h> 
using namespace std;
const int N=110000;
long long f[35];
 map<int,int>mp;
int c[2*N];
int d[N];
int main()
{
	int n,m;
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++)
	scanf("%d",&c[i]);
	for(int i=n;i<n+m;i++)
	{
		scanf("%d",&c[i]);
		mp[c[i]]++;
	}
	sort(c,c+n+m); 
	int k=0;
	for(int i=0;i<n+m;i++)
	{
		if(mp[c[i]]) 
		d[k++]=i;
	}
	int ans=0;
	int t=0; 
	int cnt;
    for(int i=0;i<n+m;i++)
    {
    	if(mp[c[i]])
    	{
    		t++;
    	}
    	else
    	{
    		if(t==0)
    		{
    			cnt=c[d[t]]-c[i];
    		}
    		else if(t==k){
    			cnt=c[i]-c[d[t-1]];
    		}
    		else
    		{
    			cnt=min(c[i]-c[d[t-1]],c[d[t]]-c[i]);
    		}
            //cout<<c[i]<<" "<<c[d[t-1]]<<" "<<c[d[t]]<<endl; 
    		ans=max(ans,cnt);
    	}
    }
    printf("%d\n",ans);
return 0;
}


posted @ 2016-07-30 21:57  __NaCl  阅读(124)  评论(0编辑  收藏  举报