DAY3(测验)(模拟&stl&二分)

A - Tricky Sum

 CodeForces - 598A 

http://codeforces.com/problemset/problem/598/A

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

In this problem you are to calculate the sum of all integers from 1 to n, but you should take all powers of two with minus in the sum.

For example, for n = 4 the sum is equal to  - 1 - 2 + 3 - 4 =  - 4, because 1, 2 and 4 are 20, 21 and 22 respectively.

Calculate the answer for t values of n.

Input

The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of values of n to be processed.

Each of next t lines contains a single integer n (1 ≤ n ≤ 109).

Output

Print the requested sum for each of t integers n given in the input.

Examples

input

Copy

2
4
1000000000

output

Copy

-4
499999998352516354

Note

The answer for the first sample is explained in the statement.

思路就是先从1到n求和再减去小于的2的幂的两倍

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
typedef long long LL;
using namespace std;
struct node{
	LL x;
	LL powx;
	friend bool operator<(node a,node b)
	{
		return a.powx>b.powx;
	}
	friend bool operator==(node a,node b)
	{
		return a.powx==b.powx;
	}
	friend bool operator>(node a,node b)
	{
		return a.powx<b.powx;
	}
};
set<node> s;
void pre_solve()
{
	node tmp;
	for(int i=0;i<32;i++)
	{
		tmp.x=i;
		tmp.powx=pow(2,i);
		s.insert(tmp);
	}
}

int main()
{
	pre_solve();
	int t;
	while(cin>>t)
	{
		while(t--)
		{
			LL n;
			cin>>n;
			LL sum;
			node q;q.powx=n;
			LL lim=s.lower_bound(q)->x;
			sum=(1+n)*n/2+2-pow(2,lim+2);
			cout<<sum<<endl;
		}
	}
	return 0;
}

我也不知道自己为什么要写成这个鬼样子。。。

B - Queries about less or equal elements

 CodeForces - 600B 

http://codeforces.com/problemset/problem/600/B

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two arrays of integers a and b. For each element of the second array bj you should find the number of elements in array athat are less than or equal to the value bj.

Input

The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the sizes of arrays a and b.

The second line contains n integers — the elements of array a ( - 109 ≤ ai ≤ 109).

The third line contains m integers — the elements of array b ( - 109 ≤ bj ≤ 109).

Output

Print m integers, separated by spaces: the j-th of which is equal to the number of such elements in array a that are less than or equal to the value bj.

Examples

input

Copy

5 4
1 3 5 7 9
6 4 2 8

output

Copy

3 2 1 4

input

Copy

5 5
1 2 1 2 5
3 1 4 1 5

output

Copy

4 2 4 2 5

sort之后直接upper_bound就好了,不解释

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>

using namespace std;
const int size=2e5+5;
int main()
{
	int a,b;
	int arra[size],arrb[size];
	while(cin>>a>>b)
	{
		int i;
		for(i=0;i<a;i++)
		{
			scanf("%d",&arra[i]);
		}
		sort(arra,arra+a);
		for(i=0;i<b;i++)
		{
			scanf("%d",&arrb[i]);
		}
		for(i=0;i<b;i++)
		{
			printf("%d",upper_bound(arra,arra+a,arrb[i])-arra);
			if(i!=b-1)
			{
				printf(" ");
			}
			else printf("\n");
		}
	}
	return 0;
}

C - 极角

 CodeForces - 598C 

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given the set of vectors on the plane, each of them starting at the origin. Your task is to find a pair of vectors with the minimal non-oriented angle between them.

Non-oriented angle is non-negative value, minimal between clockwise and counterclockwise direction angles. Non-oriented angle is always between 0 and π. For example, opposite directions vectors have angle equals to π.

Input

First line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of vectors.

The i-th of the following n lines contains two integers xi and yi (|x|, |y| ≤ 10 000, x2 + y2 > 0) — the coordinates of the i-th vector. Vectors are numbered from 1 to n in order of appearing in the input. It is guaranteed that no two vectors in the input share the same direction (but they still can have opposite directions).

Output

Print two integer numbers a and b (a ≠ b) — a pair of indices of vectors with the minimal non-oriented angle. You can print the numbers in any order. If there are many possible answers, print any.

Examples

input

Copy

4
-1 0
0 -1
1 0
1 1

output

Copy

3 4

input

Copy

6
-1 0
0 -1
1 0
1 1
-4 -5
-4 -6

output

Copy

6 5

吐槽一发这个英文表述。。我硬是看了半个小时才看懂。

题意:

给出一堆向量,求出拥有最小夹角的两个向量

思路:

对所有向量用其角度进行排序,相邻两个做差,再首尾做差并加2PI,其中的最小值便是

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>

using namespace std;
const int size=2e5+5;
int main()
{
	int a,b;
	int arra[size],arrb[size];
	while(cin>>a>>b)
	{
		int i;
		for(i=0;i<a;i++)
		{
			scanf("%d",&arra[i]);
		}
		sort(arra,arra+a);
		for(i=0;i<b;i++)
		{
			scanf("%d",&arrb[i]);
		}
		for(i=0;i<b;i++)
		{
			printf("%d",upper_bound(arra,arra+a,arrb[i])-arra);
			if(i!=b-1)
			{
				printf(" ");
			}
			else printf("\n");
		}
	}
	return 0;
}

D - Space Golf

 CSU - 2092 

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2092

Description

You surely have never heard of this new planet surface exploration scheme, as it is being carried out in a project with utmost secrecy. The scheme is expected to cut costs of conventional rover-type mobile explorers considerably, using projected-type equipment nicknamed "observation bullets".

Bullets do not have any active mobile abilities of their own, which is the main reason of their cost-efficiency. Each of the bullets, after being shot out on a launcher given its initial velocity, makes a parabolic trajectory until it touches down. It bounces on the surface and makes another parabolic trajectory. This will be repeated virtually infinitely.

We want each of the bullets to bounce precisely at the respective spot of interest on the planet surface, adjusting its initial velocity. A variety of sensors in the bullet can gather valuable data at this instant of bounce, and send them to the observation base. Although this may sound like a conventional target shooting practice, there are several issues that make the problem more difficult.

  • There may be some obstacles between the launcher and the target spot. The obstacles stand upright and are very thin that we can ignore their widths. Once the bullet touches any of the obstacles, we cannot be sure of its trajectory thereafter. So we have to plan launches to avoid these obstacles.
  • Launching the bullet almost vertically in a speed high enough, we can easily make it hit the target without touching any of the obstacles, but giving a high initial speed is energy-consuming. Energy is extremely precious in space exploration, and the initial speed of the bullet should be minimized. Making the bullet bounce a number of times may make the bullet reach the target with lower initial speed.
  • The bullet should bounce, however, no more than a given number of times. Although the body of the bullet is made strong enough, some of the sensors inside may not stand repetitive shocks. The allowed numbers of bounces vary on the type of the observation bullets.

You are summoned engineering assistance to this project to author a smart program that tells the minimum required initial speed of the bullet to accomplish the mission.

Figure D.1 gives a sketch of a situation, roughly corresponding to the situation of the Sample Input 4 given below.

Figure D.1. A sample situation

Figure D.1. A sample situation

You can assume the following.

  • The atmosphere of the planet is so thin that atmospheric resistance can be ignored.
  • The planet is large enough so that its surface can be approximated to be a completely flat plane.
  • The gravity acceleration can be approximated to be constant up to the highest points a bullet can reach.

These mean that the bullets fly along a perfect parabolic trajectory.

You can also assume the following.

  • The surface of the planet and the bullets are made so hard that bounces can be approximated as elastic collisions. In other words, loss of kinetic energy on bounces can be ignored. As we can also ignore the atmospheric resistance, the velocity of a bullet immediately after a bounce is equal to the velocity immediately after its launch.
  • The bullets are made compact enough to ignore their sizes.
  • The launcher is also built compact enough to ignore its height.

You, a programming genius, may not be an expert in physics. Let us review basics of rigid-body dynamics.

We will describe here the velocity of the bullet v with its horizontal and vertical components vx and vy (positive meaning upward). The initial velocity has the components vix and viy, that is, immediately after the launch of the bullet, vx = vix and vy = viy hold. We denote the horizontal distance of the bullet from the launcher as x and its altitude as y at time t.

  • The horizontal velocity component of the bullet is kept constant during its flight when atmospheric resistance is ignored. Thus the horizontal distance from the launcher is proportional to the time elapsed. 

     

    x=vixt(1)(1)x=vixt

  • The vertical velocity component vy is gradually decelerated by the gravity. With the gravity acceleration of g, the following differential equation holds during the flight. 

     

    dvydt=−g(2)(2)dvydt=−g


    Solving this with the initial conditions of vy = viy and y = 0 when t = 0, we obtain the following. 

    y==−12gt2+viyt−(12gt−viy)t(3)(4)(3)y=−12gt2+viyt(4)=−(12gt−viy)t


    The equation (4) tells that the bullet reaches the ground again when t = 2viy/g. Thus, the distance of the point of the bounce from the launcher is 2vixviy/g. In other words, to make the bullet fly the distance of l, the two components of the initial velocity should satisfy 2vixviy = lg.
  • Eliminating the parameter t from the simultaneous equations above, we obtain the following equation that escribes the parabolic trajectory of the bullet. 

     

    y=−(g2v2ix)x2+(viyvix)x(5)(5)y=−(g2vix2)x2+(viyvix)x

For ease of computation, a special unit system is used in this project, according to which the gravity acceleration g of the planet is exactly 1.0.

Input

The input consists of several tests case with the following format. 

 

d n bp1 h1p2 h2⋮pn hnd n bp1 h1p2 h2⋮pn hn

 

For each test, the first line contains three integers, dn, and b. Here, d is the distance from the launcher to the target spot (1 ≤ d ≤ 10000), n is the number of obstacles (1 ≤ n ≤ 10), and b is the maximum number of bounces allowed, not including the bounce at the target spot (0 ≤ b ≤ 15).

Each of the following n lines has two integers. In the k-th line, pk is the position of the k-th obstacle, its distance from the launcher, and hk is its height from the ground level. You can assume that 0 < p1, pk < pk + 1 for k = 1, …, n − 1, and pn < d. You can also assume that 1 ≤ hk ≤ 10000 for k = 1, …, n.

Output

Output the smallest possible initial speed vi that makes the bullet reach the target. The initial speed vi of the bullet is defined as follows. 

 

vi=v2ix+v2iy−−−−−−−√vi=vix2+viy2


The output should not contain an error greater than 0.0001.

Sample Input

100 1 0
50 100

10 1 0
4 2

100 4 3
20 10
30 10
40 10
50 10

343 3 2
56 42
190 27
286 34

Sample Output

14.57738
3.16228
7.78175
11.08710

Hint

Source

Asia Regional Contest, Tokyo, 2014

题意:

在一有障碍物的水平地面上有一球做斜抛运动,其每次与地面的碰撞都可视为完全弹性碰撞,求最终恰好落到终点的最小速度。

思路:

枚举碰撞次数,将整个平面上的障碍物等效到发生第一次碰撞与发射点之间的平面上。然后判断速度与平面夹角为45°时能否到达,如能到达,这就是这个碰撞次数下的最小速度,否则当其恰好不会碰到所有障碍物时的速度时最小速度

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>

#define sq(x) x*x 
#define f(x,y,z) y/(x*(x-z))
#define F(d,b) sqrt((sq(b)*sq(d)+1)/(2*b))
#define m(a,b) a<b?a:b
#define eps 1e-7
typedef long double ld;
using namespace std;
ld len;
const int INF=0x3f3f3f3f;
const ld g=1;
int num_obs,num_boun;
struct node{
	ld x,y;
};
node obs[20];
node ori[20];
ld ans=INF;
bool judge(double vx,double tot){
    for(int i=0;i<num_obs;i++){
        double h1 = obs[i].x*tot/(2*vx*vx) - obs[i].x*obs[i].x/(2*vx*vx);
        if(obs[i].y - h1 >= eps) return false;
    }
    return true;
}
inline ld help_get(ld x,ld y)
{
	int use=x/y;
	return x-use*y;
}
void v_get(int cnt)
{
	int i;
	//memset(obs,0,sizeof(obs));
	ld tot=len/cnt;
	ld minn=0;
	for(i=0;i<num_obs;i++)
	{
		obs[i].x=help_get(ori[i].x,tot);
		if(!obs[i].x) return;
	}
	double vx = sqrt(tot/2.0);
    if(judge(vx,tot)){
        ans = vx*sqrt(2.0);
        return;
    }
    for(i=0;i<num_obs;i++)
	{
		minn=m(minn,f(obs[i].x,obs[i].y,tot));
		//cout<<f(obs[i].x,obs[i].y,tot)<<endl;
	}
	minn=-minn;
	ans=min(ans,F(tot,minn));
	//cout<<ans<<endl;
}
int main()
{
	while(cin>>len>>num_obs>>num_boun)
	{
		ans=INF;
		int i;
		for(i=0;i<num_obs;i++)cin>>ori[i].x>>ori[i].y,obs[i].y=ori[i].y;
		for(i=0;i<=num_boun;i++)
		{
			v_get(i+1);
		}
		printf("%.5Lf\n",ans);
	}
	return 0;
}

E - Mafia

 CodeForces - 348A

http://codeforces.com/problemset/problem/348/A

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?

Input

The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.

Output

In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.

Examples

input

Copy

3
3 2 2

output

Copy

4

input

Copy

4
2 2 2 2

output

Copy

3

题意:

每个人最少玩ai把游戏,每把游戏最多有n-1个人玩,求最少要玩多少把。

思路:

对把数二分。每轮游戏至多n-1人。当令一个s不断叠加ai直至ai大于把数则证明每一轮游戏的第一席位已经被占满,再去占第二个席位以此类推。(说起来这样可以直接推出k(n-1)>∑ai时成立然后直接推出答案了呢。。。)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>

#define md(x,y) (x+y)/2
using namespace std;
typedef long long LL;
const int size=1e5+5;
LL cnt;
LL arr[size];
int check(LL x)
{
	LL i;
	LL s=0;
	LL ans=cnt-1;
	for(i=0;i<cnt;i++)
	{
		s+=arr[i];
		while(s>=x)
		{
			ans--;
			s-=x;
			if(ans<0)
			{
				return 0;
			}
		} 
		
	}
	if(ans==0) return s<=0;
	return 1;
}
int main()
{
	
	while(cin>>cnt)
	{
		LL i;
		LL l=0,r=0;
		for(i=0;i<cnt;i++)
		{
			cin>>arr[i];
			l=max(l,arr[i]);
			r=r+arr[i]; 
		}
		//cout<<l<<endl;
		LL ans;
		while(l<=r)
		{
			LL mid=md(l,r);
			if(check(mid))
			{
				ans=mid;
				r=mid-1;
			}
			else{
				l=mid+1;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

 

posted @ 2018-07-25 23:15  Fly_White  阅读(207)  评论(0编辑  收藏  举报