• Codeforces Round #367 (Div. 2) A B C 暴力 二分 dp(字符串的反转)


    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 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.

    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.

    题意:起始点为(a,b)  给n辆出租车的位置以及速度 问你最短的时间出租车到达(a,b)

    题解:暴力 注意精度

     1 /******************************
     2 code by drizzle
     3 blog: www.cnblogs.com/hsd-/
     4 ^ ^    ^ ^
     5  O      O
     6 ******************************/
     7 #include<bits/stdc++.h>
     8 #include<iostream>
     9 #include<cstring>
    10 #include<cstdio>
    11 #include<map>
    12 #include<algorithm>
    13 #include<queue>
    14 #define ll __int64
    15 using namespace std;
    16 double a,b;
    17 int n;
    18 struct node
    19 {
    20     double x,y,v;
    21 }N[1005];
    22 double ans=0;
    23 int main()
    24 {
    25     ans=1000005;
    26     scanf("%lf %lf",&a,&b);
    27     scanf("%d",&n);
    28     for(int i=1;i<=n;i++)
    29         scanf("%lf %lf %lf",&N[i].x,&N[i].y,&N[i].v);
    30     for(int i=1;i<=n;i++)
    31     {
    32         double exm=0.0;
    33         exm=sqrt((a-N[i].x)*(a-N[i].x)+(b-N[i].y)*(b-N[i].y))/N[i].v;
    34         if((ans-exm)>0.000001)
    35             ans=exm;
    36     }
    37     printf("%.15f
    ",ans);
    38     return 0;
    39 }

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

     
    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 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.

    题意:给你n个值   q组查询m 输出这n个值中小于等于m的数量

    题解:排序之后 二分

     1 /******************************
     2 code by drizzle
     3 blog: www.cnblogs.com/hsd-/
     4 ^ ^    ^ ^
     5  O      O
     6 ******************************/
     7 #include<bits/stdc++.h>
     8 #include<iostream>
     9 #include<cstring>
    10 #include<cstdio>
    11 #include<map>
    12 #include<algorithm>
    13 #include<queue>
    14 #define ll __int64
    15 using namespace std;
    16 int n;
    17 int a[100005];
    18 int q,m;
    19 int main()
    20 {
    21     scanf("%d",&n);
    22     for(int i=0;i<n;i++)
    23         scanf("%d",&a[i]);
    24     sort(a,a+n);
    25     scanf("%d",&q);
    26     for(int i=1;i<=q;i++)
    27     {
    28      scanf("%d",&m);
    29      int pos=0;
    30      pos=upper_bound(a,a+n,m)-a;
    31      printf("%d
    ",pos);
    32     }
    33     return 0;
    34 }
    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 exceed 100 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个字符串 对于每个字符串进行反转操作会有一个权值  为了使得这n个字符串的字典升序

    输出的最小的权值和 ,如果不存在则输出-1

    题解:dp[i][1]表示第i个字符串反转的最优解

             dp[i][0]表示第i个字符串不反转的最优解

     1 /******************************
     2 code by drizzle
     3 blog: www.cnblogs.com/hsd-/
     4 ^ ^    ^ ^
     5  O      O
     6 ******************************/
     7 #include<bits/stdc++.h>
     8 #include<iostream>
     9 #include<cstring>
    10 #include<cstdio>
    11 #include<map>
    12 #include<algorithm>
    13 #include<queue>
    14 #define ll __int64
    15 using namespace std;
    16 int n;
    17 ll w[100005];
    18 string c[100005],s[100005];
    19 ll dp[100005][3];
    20 ll ans;
    21 int main()
    22 {
    23     scanf("%d",&n);
    24     for(int i=1;i<=n;i++)
    25         scanf("%I64d",&w[i]);
    26     for(int i=1;i<=n;i++){
    27        cin>>c[i];
    28        s[i]=c[i];
    29        reverse(s[i].begin(),s[i].end());
    30        }
    31     memset(dp,0,sizeof(dp));
    32     dp[1][0]=0;
    33     dp[1][1]=w[1];
    34     for(int i=2;i<=n;i++)
    35     {
    36         dp[i][0]=1e18;
    37         dp[i][1]=1e18;
    38         if(c[i]>=c[i-1]) {
    39             dp[i][0]=dp[i-1][0];
    40         }
    41         if(c[i]>=s[i-1]){
    42             dp[i][0]=min(dp[i][0],dp[i-1][1]);
    43         }
    44         if(s[i]>=c[i-1]){
    45             dp[i][1]=dp[i-1][0]+w[i];
    46         }
    47         if(s[i]>=s[i-1]){
    48             dp[i][1]=min(dp[i][1],dp[i-1][1]+w[i]);
    49         }
    50     }
    51     ans=min(dp[n][1],dp[n][0]);
    52     if(ans==1e18)
    53         cout<<"-1"<<endl;
    54     else
    55         cout<<ans<<endl;
    56     return 0;
    57 }
  • 相关阅读:
    pku3225 区间
    pku2136 Vertical Histogram
    NOI2006 最大获利
    APIO2010 特别行动队
    停电两夜
    偷偷乐一把
    那些花儿...
    一件一块钱的小事
    web service 的Section=ResponseStatusLine 错误和skype
    杂谈
  • 原文地址:https://www.cnblogs.com/hsd-/p/5840192.html
Copyright © 2020-2023  润新知