Codeforces Round #403 Div. 2 A B 二分

来源:互联网 发布:阿里云主机 smtp 编辑:程序博客网 时间:2024/05/21 05:06

A. Andryusha and Socks
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Andryusha is an orderly boy and likes to keep things in their place.

Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.

Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time?

Input

The first line contains the single integer n (1 ≤ n ≤ 105) — the number of sock pairs.

The second line contains 2n integers x1, x2, ..., x2n (1 ≤ xi ≤ n), which describe the order in which Andryusha took the socks from the bag. More precisely, xi means that the i-th sock Andryusha took out was from pair xi.

It is guaranteed that Andryusha took exactly two socks of each pair.

Output

Print single integer — the maximum number of socks that were on the table at the same time.

Examples
input
11 1
output
1
input
32 1 1 3 2 3
output
2
Note

In the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time.

In the second example Andryusha behaved as follows:

  • Initially the table was empty, he took out a sock from pair 2 and put it on the table.
  • Sock (2) was on the table. Andryusha took out a sock from pair 1 and put it on the table.
  • Socks (1, 2) were on the table. Andryusha took out a sock from pair 1, and put this pair into the wardrobe.
  • Sock (2) was on the table. Andryusha took out a sock from pair 3 and put it on the table.
  • Socks (2, 3) were on the table. Andryusha took out a sock from pair 2, and put this pair into the wardrobe.
  • Sock (3) was on the table. Andryusha took out a sock from pair 3 and put this pair into the wardrobe.
Thus, at most two socks were on the table at the same time.

水题,不多说

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <map>using namespace std;map<int,int> judge;int main(){    int n,i,j,a;    judge.clear();    scanf("%d",&n);    int sum = 0,res = 0;    for(i=1;i<=2*n;i++){        scanf("%d",&a);        if(judge[a]){            judge[a] = 0;            sum--;        }else{            judge[a]++;            sum++;        }        res = max(res,sum);    }    printf("%d\n",res);        return 0;}

B. The Meeting Place Cannot Be Changed
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction.

At some points on the road there are n friends, and i-th of them is standing at the point xi meters and can move with any speed no greater than vi meters per second in any of the two directions along the road: south or north.

You are to compute the minimum time needed to gather all the n friends at some point on the road. Note that the point they meet at doesn't need to have integer coordinate.

Input

The first line contains single integer n (2 ≤ n ≤ 60 000) — the number of friends.

The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) — the current coordinates of the friends, in meters.

The third line contains n integers v1, v2, ..., vn (1 ≤ vi ≤ 109) — the maximum speeds of the friends, in meters per second.

Output

Print the minimum time (in seconds) needed for all the n friends to meet at some point on the road.

Your answer will be considered correct, if its absolute or relative error isn't greater than 10 - 6. Formally, let your answer be a, while jury's answer be b. Your answer will be considered correct if  holds.

Examples
input
37 1 31 2 1
output
2.000000000000
input
45 10 3 22 3 2 4
output
1.400000000000
Note

In the first sample, all friends can gather at the point 5 within 2 seconds. In order to achieve this, the first friend should go south all the time at his maximum speed, while the second and the third friends should go north at their maximum speeds.

这题一开始还在想用二分,对于在直线上的点进行二分,但是时间的增大或减小似乎并没有什么关系。于是放弃,去查了题解发现真的是二分,比赛时还有近两千人做出来。顿时觉得自己很辣鸡。。

依旧是对这条直线上的点进行二分的,然后每次比较目标点左边的所有点到目标点的最长时间,和这个点右边的所有点到目标点的最长时间。左边所费最长时间和右边所费最长时间比较,肯定目标点是要朝向费时长的那边移动,直到最终花费时间不再减少为止。

二分二分,哎。。。自己脑子笨啊!

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <map>using namespace std;const int maxn = 60010;struct unit{    double pos;    double v;}save[maxn];int n;double lsum,rsum;double eps = 1e-7;double getsum(double point){    int i;    double sum = 0.0;    i = 1;    while(save[i].pos<point||abs(save[i].pos-point)<eps){        sum = max(sum,abs(save[i].pos-point)/save[i].v);        i++;    }    lsum = sum;    sum = 0.0;    while (i<=n) {        sum = max(sum,abs(save[i].pos-point)/save[i].v);        i++;    }    rsum = sum;    return max(lsum,rsum);}bool compare(unit a,unit b){    return a.pos<b.pos;}int main(){    int i,j;    double maxx = -1;    double res = 999999999.0f;    double a;    scanf("%d",&n);    for(i=1;i<=n;i++){        scanf("%lf",&a);        maxx = max(maxx,a);        save[i].pos = a;    }    for(i=1;i<=n;i++){        scanf("%lf",&a);        save[i].v = a;    }    sort(save+1, save+1+n, compare);    double left = 0, right = maxx;    while(abs(left-right)>eps){        double middle = (left+right)/2;        double now = getsum(middle);        if(lsum>rsum){            right = middle - eps;        }else{            left = middle + eps;        }        res = min(res,now);    }    printf("%.12lf\n",res);    return 0;}









0 0
原创粉丝点击