题目链接:点击打开链接
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.题目大意:给你自己的位置,然后有另外的m个位置,后面跟着一个速度,问你到哪个点花费的最少时间是多少,求的是最少时间。直接暴力
#include <bits/stdc++.h>
using namespace std;
int main()
{
double n,m;
while (~scanf("%lf%lf",&n,&m))
{
int z;
scanf("%d",&z);
double res = -1,x,y,v;
for (int i = 0 ; i < z ; i++ )
{
scanf("%lf%lf%lf",&x,&y,&v);
if (res == -1)
{
res = sqrt((x - n ) * (x - n) + (y - m) * (y - m)) / v;
}
else if(res > sqrt((x - n ) * (x - n) + (y - m) * (y - m)) / v)
{
res = sqrt((x - n ) * (x - n) + (y - m) * (y - m)) / v;
}
}
printf("%f\n",res);
}
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 <bits/stdc++.h>
using namespace std;
int ar[111111];
int main()
{
int n;
while (~scanf("%d",&n))
{
for (int i = 1 ; i <= n ; i++)
{
scanf("%d",&ar[i]);
}
sort(ar + 1 , ar + n + 1);
int m;
scanf("%d",&m);
while (m--)
{
int l = 1 , r = n;
int x;
scanf("%d",&x);
while (l != r)
{
int mid = (l + r) / 2;
if (x >= ar[mid])
{
l = mid + 1;
}
else
{
r = mid;
}
}
if(ar[l] > x)
{
l--;
}
printf("%d\n",l);
}
}
}
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.
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 exceed 100 000.
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.
2 1 2 ba ac
1
3 1 3 1 aa ba ac
1
2 5 5 bbb aaa
-1
2 3 3 aaa aa
-1
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.
思路:dp思想,因为每次只有翻与不翻,所以定义dp[n][2],然后每次判断字典序,这样就可以了。可以预处理出来翻过去的字符串
#include <bits/stdc++.h>
using namespace std;
typedef __int64 LL;
LL c[111111];
string st[111111][2];
LL dp[111111][2];
string f(string s)
{
string res;
int len = s.size();
res = s;
for (int i = 0 ; i < len / 2 ; i ++ )
{
swap(res[i],res[len - i - 1]);
}
return res;
}
int main()
{
int n;
while (~scanf("%d",&n))
{
for (int i = 0 ; i < n ; i++ )
{
scanf("%I64d",&c[i]);
}
for (int i = 0 ; i < n ; i++ )
{
cin >> st[i][0];
st[i][1] = f(st[i][0]);
}
for (int i = 0 ; i < n ; i++ )
{
dp[i][0] = 1e18;
dp[i][1] = 1e18;
}
dp[0][0] = 0;
dp[0][1] = c[0];
int i;
for(i = 1 ; i < n ; i++)
{
if(st[i][0] >= st[i-1][0])
{
dp[i][0] = dp[i-1][0];
}
if(st[i][1] >= st[i-1][0])
{
dp[i][1] = dp[i - 1][0] + c[i];
}
if(st[i][0] >= st[i-1][1])
{
dp[i][0] = min(dp[i][0] , dp[i-1][1]);
}
if(st[i][1] >= st[i-1][1])
{
dp[i][1] = min(dp[i][1] , dp[i-1][1] + c[i]);
}
if(dp[i][1] == 1e18 && dp[i][0] == 1e18)
{
break;
}
}
if(i == n)
printf("%I64d\n",min(dp[n-1][0],dp[n-1][1]));
else
printf("-1\n");
}
}
Author has gone out of the stories about Vasiliy, so here is just a formal task description.
You are given q queries and a multiset A, initially containing only integer 0. There are three types of queries:
- "+ x" — add integer x to multiset A.
- "- x" — erase one occurrence of integer x from multiset A. It's guaranteed that at least one x is present in the multiset A before this query.
- "? x" — you are given integer x and need to compute the value , i.e. the maximum value of bitwise exclusive OR (also know as XOR) of integer x and some integer y from the multiset A.
Multiset is a set, where equal elements are allowed.
The first line of the input contains a single integer q (1 ≤ q ≤ 200 000) — the number of queries Vasiliy has to perform.
Each of the following q lines of the input contains one of three characters '+', '-' or '?' and an integer xi (1 ≤ xi ≤ 109). It's guaranteed that there is at least one query of the third type.
Note, that the integer 0 will always be present in the set A.
For each query of the type '?' print one integer — the maximum value of bitwise exclusive OR (XOR) of integer xi and some integer from the multiset A.
10 + 8 + 9 + 11 + 6 + 1 ? 3 - 8 ? 3 ? 8 ? 11
11 10 14 13
After first five operations multiset A contains integers 0, 8, 9, 11, 6 and 1.
The answer for the sixth query is integer — maximum among integers , , , and .题目大意:有n次操作,操作有三个,+ - ? 添加元素,删除元素,询问元素,询问该元素与集合中的元素异或的最大值是多少
思路:字典树,因为只有10^9次化为二进制最多是30位,所以就固定每个数为30位,不够大前面用0,因为这样不影响结果。不过要注意,一开始集合里面有0.每次添加,记录1和0的个数就好
#include <bits/stdc++.h>
using namespace std;
struct N
{
N *Next[2];
int cou;
};
int two[31];
N *tree;
void Add(int a)
{
int num[31];
memset(num,0,sizeof(num));
int cou = 30;
while (a)
{
num[cou--] = a % 2;
a /= 2;
}
N *p = tree,*q;
for (int i = 0 ; i < 31 ; i++)
{
int id = num[i];
if ( p -> Next[id] == NULL)
{
q = (N *)malloc(sizeof(N));
q -> cou = 1;
for (int j = 0 ; j < 2 ; j++ )
{
q -> Next[j] = NULL;
}
p -> Next[id] = q;
p = p -> Next[id];
}
else
{
p -> Next[id] -> cou++;
p = p -> Next[id];
}
//printf("%d\n",i);
}
}
void Del(int a)
{
int num[31];
memset(num,0,sizeof(num));
int cou = 30;
while (a)
{
num[cou--] = a % 2;
a /= 2;
}
N *p = tree;
for (int i = 0 ; i < 31 ; i++ )
{
int id = num[i];
p -> Next[id] -> cou--;
p = p -> Next[id];
//printf("%d\n",p -> cou);
}
return ;
}
int que(int a)
{
int num[31];
memset(num,0,sizeof(num));
int cou = 30;
while (a)
{
num[cou--] = a % 2;
a /= 2;
}
N *p = tree;
int sum = 0;
for (int i = 0 ; i < 31 ; i++ )
{
int id = (num[i] + 1) % 2;
if ( p -> Next[id] == NULL && p -> Next[num[i]] != NULL)
{
if ( p -> Next[num[i]] == 0)
{
sum += two[i] * num[i];
}
p = p -> Next[num[i]];
}
else if(p -> Next[id] != NULL && p -> Next[num[i]] != NULL)
{
if ( p -> Next[id] -> cou > 0)
{
sum += two[i];
p = p -> Next[id];
}
else
{
if (p -> Next[num[i]] -> cou == 0)
sum += two[i] * num[i];
else
{
p = p -> Next[num[i]];
}
}
}
else if( p -> Next[id] != NULL && p -> Next[num[i]] == NULL)
{
if (p -> Next[id] -> cou == 0)
{
sum += two[i] * num[i];
}
else
{
sum += two[i];
p = p -> Next[id];
}
}
else
{
sum += two[i] * num[i];
}
}
return sum;
}
int main()
{
tree = (N *)malloc(sizeof(N));
for (int i = 0 ; i < 2 ; i++)
{
tree -> Next[i] = NULL;
}
int n ;
two[30] = 1;
for (int i = 29 ; i >= 0 ; i -- )
{
two[i] = two[i+1] * 2;
}
Add(0);
//printf("kljkl");
while (~scanf("%d",&n))
{
char c;
int a;
for (int i = 0 ; i < n ; i++ )
{
getchar();
scanf("%c",&c);
getchar();
scanf("%d",&a);
if ( c == '+')
{
Add(a);
}
if ( c == '-')
{
Del(a);
}
if ( c == '?')
{
printf("%d\n",que(a));
}
}
}
}