codeforces-706【二分】【string类】

题目链接:点击打开链接

A. Beru-taxi
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasiliy lives at point (a, b) of the coordinate plane. He is hurrying up to work so he wants to get out of his house as soon as possible. New app suggested n available Beru-taxi nearby. The i-th taxi is located at point (xi, yi) and moves with a speed vi.

Consider that each of n drivers will move directly to Vasiliy and with a maximum possible speed. Compute the minimum time when Vasiliy will get in any of Beru-taxi cars.

Input

The first line of the input contains two integers a and b ( - 100 ≤ a, b ≤ 100) — coordinates of Vasiliy's home.

The second line contains a single integer n (1 ≤ n ≤ 1000) — the number of available Beru-taxi cars nearby.

The i-th of the following n lines contains three integers xiyi and vi ( - 100 ≤ xi, yi ≤ 1001 ≤ vi ≤ 100) — the coordinates of the i-th car and its speed.

It's allowed that several cars are located at the same point. Also, cars may be located at exactly the same point where Vasiliy lives.

Output

Print a single real value — the minimum time Vasiliy needs to get in any of the Beru-taxi cars. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

Examples
input
0 0
2
2 0 1
0 2 2
output
1.00000000000000000000
input
1 3
3
3 3 2
-2 3 6
-2 7 10
output
0.50000000000000000000
Note

In the first sample, first taxi will get to Vasiliy in time 2, and second will do this in time 1, therefore 1 is the answer.

In the second sample, cars 2 and 3 will arrive simultaneously.

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
const int INF=0x3f3f3f3f;
int a,b,n;
double s[1010];
int main()
{
	while(~scanf("%d %d",&a,&b))
	{
		scanf("%d",&n);
		double x,y,val,time,ans=INF+0.0;
		for(int i=1;i<=n;i++)
		{
			scanf("%lf %lf %lf",&x,&y,&val);
			time=sqrt( (x-a)*(x-a) + (y-b)*(y-b) )/val;
			ans=min(ans,time);
		}
		printf("%.8lf\n",ans);
	}
	return 0;
}

题目链接:点击打开链接

B. Interesting drink
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink "Beecola", which can be bought in n different shops in the city. It's known that the price of one bottle in the shop i is equal to xi coins.

Vasiliy plans to buy his favorite drink for q consecutive days. He knows, that on the i-th day he will be able to spent mi coins. Now, for each of the days he want to know in how many different shops he can buy a bottle of "Beecola".

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of shops in the city that sell Vasiliy's favourite drink.

The second line contains n integers xi (1 ≤ xi ≤ 100 000) — prices of the bottles of the drink in the i-th shop.

The third line contains a single integer q (1 ≤ q ≤ 100 000) — the number of days Vasiliy plans to buy the drink.

Then follow q lines each containing one integer mi (1 ≤ mi ≤ 109) — the number of coins Vasiliy can spent on the i-th day.

Output

Print q integers. The i-th of them should be equal to the number of shops where Vasiliy will be able to buy a bottle of the drink on the i-th day.

Example
input
5
3 10 8 6 11
4
1
10
3
11
output
0
4
1
5
Note

On the first day, Vasiliy won't be able to buy a drink in any of the shops.

On the second day, Vasiliy can buy a drink in the shops 123 and 4.

On the third day, Vasiliy can buy a drink only in the shop number 1.

Finally, on the last day Vasiliy can buy a drink in any shop.


普通二分查找:

#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,q;
int a[100010];
int solve(int x)
{
	int mid,left=1,right=n,ans=0;
	while(left<=right)
	{
		mid=(left+right)/2;
		if(x>=a[mid])  // 刚开始一直错,加个 = 就行了 
		{
			ans=mid;
			left=mid+1;
		}
		else
			right=mid-1;
	}
	return ans;
}
int main()
{
	while(~scanf("%d",&n))
	{
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		sort(a+1,a+n+1);
		scanf("%d",&q);
		int money;
		while(q--)
		{
			scanf("%d",&money);
			printf("%d\n",solve(money));
		}
	}
	return 0;
}

贴个STL:两个函数在  < algorithm > 里面

函数 lower_bound( ) 在 first 和 last 中的前闭后开区间进行二分查找,返回大于或等于 val 的第一个元素位置。如果所有元素都小于 val,则返回 last 的位置,且 last 的位置是越界的!!
函数 upper_bound ( )返回的在前闭后开区间查找的关键字的上界,如果插入元素大于数组中全部元素,返回的是last。(注意:此时数组下标越界!!)

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,q;
int a[100010];
int main()
{
	while(~scanf("%d",&n))
	{
		for(int i=0;i<n;i++)
			scanf("%d",&a[i]);
		sort(a,a+n);
		scanf("%d",&q);
		while(q--)
		{
			int money;
			scanf("%d",&money);
			int pos=upper_bound(a,a+n,money)-a;
			printf("%d\n",pos);
		}
	}
	return 0;
}


题目链接:点击打开链接

C. Hard problem
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.

Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on).

To reverse the i-th string Vasiliy has to spent ci units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order.

String A is lexicographically smaller than string B if it is shorter than B (|A| < |B|) and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character in A is smaller than the character in B.

For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of strings.

The second line contains n integers ci (0 ≤ ci ≤ 109), the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the i-th string.

Then follow n lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed100 000.

Output

If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print  - 1. Otherwise, print the minimum total amount of energy Vasiliy has to spent.

Examples
input
2
1 2
ba
ac
output
1
input
3
1 3 1
aa
ba
ac
output
1
input
2
5 5
bbb
aaa
output
-1
input
2
3 3
aaa
aa
output
-1
Note

In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller.

In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is  - 1.

In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string "aa" should go before string "aaa", thus the answer is  - 1.


题意:给出n个字符串,可以颠倒任意一个字符串,颠倒每个字符串都有其对应的花费ci。经过操作后是否能满足字符串 str [ i ] >= str [ i -1] ,能输出最小花费,不能输出 -1。

题解:这题刚开始GG,看了别人的思路才知道用 string类,额好吧....没用过...然后 dp [ i ] [ 0 / 1 ]表示第 i 个字符串是否颠倒( 0 表示不颠倒,1 表示颠倒)的状态下满足字符串str [ i ] >= str [ i - 1 ]的最小花费。转态转移中判断各种使得str [ i ] >= str [ i - 1 ] 的各种情况就可以了。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#define LL long long
using namespace std;
const int MAX=100010;
const LL INF=1e15; 
int n;
string str[MAX][2];
int c[MAX];
LL dp[MAX][2];
string reverse(string str)
{
	string s=str;
	int len=s.length();
	for(int i=0;i<len/2;i++)
		swap(s[i],s[len-i-1]);
	return s;
}
int main()
{
	while(~scanf("%d",&n))
	{
		for(int i=0;i<n;i++)
			scanf("%d",&c[i]);
		for(int i=0;i<n;i++)
		{
			cin>>str[i][0]; //  string类要用 cin输入 
			str[i][1]=reverse(str[i][0]);
		}
		for(int i=0;i<n;i++)
			dp[i][0]=dp[i][1]=INF;
		dp[0][0]=0;	dp[0][1]=c[0];
		bool flag=0;
		for(int i=1;i<n;i++)
		{
			if(str[i][0]>=str[i-1][0])
			{
				dp[i][0]=dp[i-1][0];
			}
			if(str[i][0]>=str[i-1][1])
			{
				dp[i][0]=min(dp[i][0],dp[i-1][1]);
			}
			if(str[i][1]>=str[i-1][0])
			{
				dp[i][1]=dp[i-1][0]+c[i];
			}
			if(str[i][1]>=str[i-1][1])
			{
				dp[i][1]=min(dp[i][1],dp[i-1][1]+c[i]);
			}
			if(dp[i][1]==INF&&dp[i][0]==INF)
			{
				flag=1;
				break;
			}
		}
		if(flag)	puts("-1");
		else	printf("%I64d\n",min(dp[n-1][0],dp[n-1][1])); // 这里 long long 可以用 %I64d 输入输出,涨姿势了 
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值