题目链接:点击打开链接
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.
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 xi, yi and vi ( - 100 ≤ xi, yi ≤ 100, 1 ≤ 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.
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 .
0 0 2 2 0 1 0 2 2
1.00000000000000000000
1 3 3 3 3 2 -2 3 6 -2 7 10
0.50000000000000000000
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;
}
题目链接:点击打开链接
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".
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.
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.
5 3 10 8 6 11 4 1 10 3 11
0 4 1 5
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 1, 2, 3 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;
}
题目链接:点击打开链接
题意:给出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;
}