Codeforces Round #105 (Div. 2) (ABCDE題解)

_TCgogogo_發表於2015-08-09

比賽連結:http://codeforces.com/contest/148

比較簡單的一場,最長的一題也才寫了30行多一點



A. Insomnia cure
time limit per test:2 seconds
memory limit per test:256 megabytes

«One dragon. Two dragon. Three dragon», — the princess was counting. She had trouble falling asleep, and she got bored of counting lambs when she was nine.

However, just counting dragons was boring as well, so she entertained herself at best she could. Tonight she imagined that all dragons were here to steal her, and she was fighting them off. Everyk-th dragon got punched in the face with a frying pan. Everyl-th dragon got his tail shut into the balcony door. Everym-th dragon got his paws trampled with sharp heels. Finally, she threatened everyn-th dragon to call her mom, and he withdrew in panic.

How many imaginary dragons suffered moral or physical damage tonight, if the princess counted a total ofd dragons?

Input

Input data contains integer numbers k, l, m, n and d, each number in a separate line (1 ≤ k, l, m, n ≤ 10,1 ≤ d ≤ 105).

Output

Output the number of damaged dragons.

Sample test(s)
Input
1
2
3
4
12
Output
12
Input
2
3
4
5
24
Output
17
Note

In the first case every first dragon got punched with a frying pan. Some of the dragons suffered from other reasons as well, but the pan alone would be enough.

In the second case dragons 1, 7, 11, 13, 17, 19 and 23 escaped unharmed.


題目大意:就是給四個數,和一個d,問1-d中有多少個數字不是那四個數的倍數


題目分析:爆搞


#include <cstdio>
#include <cstring>
int const MAX = 1e5;
bool has[MAX];

int main()
{
    int a[4], d, cnt = 0;
    scanf("%d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &d);
    memset(has, false, sizeof(has));
    for(int i = 1; i <= d; i ++)
    {
        for(int j = 0; j < 4; j++)
        {
            if((i % a[j] == 0) && !has[i])
            {
                has[i] = true;
                cnt ++;
            }
        }
    }
    printf("%d\n", cnt);
}


B. Escape
time limit per test:2 seconds
memory limit per test:256 megabytes

The princess is going to escape the dragon's cave, and she needs to plan it carefully.

The princess runs at vp miles per hour, and the dragon flies atvd miles per hour. The dragon will discover the escape aftert hours and will chase the princess immediately. Looks like there's no chance to success, but the princess noticed that the dragon is very greedy and not too smart. To delay him, the princess decides to borrow a couple of bijous from his treasury. Once the dragon overtakes the princess, she will drop one bijou to distract him. In this case he will stop, pick up the item, return to the cave and spendf hours to straighten the things out in the treasury. Only after this will he resume the chase again from the very beginning.

The princess is going to run on the straight. The distance between the cave and the king's castle she's aiming for isc miles. How many bijous will she need to take from the treasury to be able to reach the castle? If the dragon overtakes the princess at exactly the same moment she has reached the castle, we assume that she reached the castle before the dragon reached her, and doesn't need an extra bijou to hold him off.

Input

The input data contains integers vp, vd, t, f andc, one per line (1 ≤ vp, vd ≤ 100,1 ≤ t, f ≤ 10, 1 ≤ c ≤ 1000).

Output

Output the minimal number of bijous required for the escape to succeed.

Sample test(s)
Input
1
2
1
1
10
Output
2
Input
1
2
1
1
8
Output
1
Note

In the first case one hour after the escape the dragon will discover it, and the princess will be 1 mile away from the cave. In two hours the dragon will overtake the princess 2 miles away from the cave, and she will need to drop the first bijou. Return to the cave and fixing the treasury will take the dragon two more hours; meanwhile the princess will be 4 miles away from the cave. Next time the dragon will overtake the princess 8 miles away from the cave, and she will need the second bijou, but after this she will reach the castle without any further trouble.

The second case is similar to the first one, but the second time the dragon overtakes the princess when she has reached the castle, and she won't need the second bijou.


題目大意:龍追公主,龍的速度為vd mile/h,公主速度為vp mile/h,龍在t小時後發現公主從老巢逃跑並開始追,一旦龍追上公主,公主可以丟個東西,這時候龍要拿著這個東西回老巢再用f小時研究它,然後重新出發開始追公主,龍的老巢和王宮相距c mile,問公主最少要準備多少個這樣的東西才能安全回王宮,如果龍和公主同時到王宮,算公主已經安全到達


題目分析:完完全全的一個小模擬,根據題意模擬即可


#include <cstdio>

int main()
{
    double vp, vd, t, f, c;
    int ans = 0;
    scanf("%lf %lf %lf %lf %lf", &vp, &vd, &t, &f, &c);
    if(vp >= vd) //公主比龍快,顯然不需要多餘的物品
    {
        printf("0\n");
        return 0;
    }
    double cur = 0, t1, t2; //cur表示當前公主跑了多遠
    cur = t * vp;   //龍發現公主逃跑時,公主的位置
    t2 = cur / (vd - vp); //龍第一次追到公主花的時間
    cur += t2 * vp; //龍第一次追到公主的位置
    while(cur < c)  //一個迴圈表示龍追到公主一次
    {
        ans ++;     
        t1 = cur / vd + f;  //龍回去並研究完物品花的時間t1
        cur += t1 * vp;     //t1時間公主又跑了t1*vp mile
        t2 = cur / (vd - vp);   //龍第i次追到公主的時間
        cur += t2 * vp;     //龍第i次追到公主時的位置
    }
    printf("%d\n",ans);
}



C. Terse princess
time limit per test:2 seconds
memory limit per test:256 megabytes

«Next please», — the princess called and cast an estimating glance at the next groom.

The princess intends to choose the most worthy groom, this is, the richest one. Whenever she sees a groom who is more rich than each of the previous ones, she says a measured «Oh...». Whenever the groom is richer than all previous ones added together, she exclaims «Wow!» (no «Oh...» in this case). At the sight of the first groom the princess stays calm and says nothing.

The fortune of each groom is described with an integer between 1 and 50000. You know that during the day the princess sawn grooms, said «Oh...» exactly a times and exclaimed «Wow!» exactly b times. Your task is to output a sequence ofn integers t1, t2, ..., tn, whereti describes the fortune ofi-th groom. If several sequences are possible, output any of them. If no sequence exists that would satisfy all the requirements, output a single number-1.

Input

The only line of input data contains three integer numbersn, a and b (1 ≤ n ≤ 100, 0 ≤ a, b ≤ 15, n > a + b), separated with single spaces.

Output

Output any sequence of integers t1, t2, ..., tn, whereti (1 ≤ ti ≤ 50000) is the fortune ofi-th groom, that satisfies the given constraints. If no sequence exists that would satisfy all the requirements, output a single number-1.

Sample test(s)
Input
10 2 3
Output
5 1 3 6 16 35 46 4 200 99
Input
5 0 0
Output
10 10 6 6 5
Note

Let's have a closer look at the answer for the first sample test.

  • The princess said «Oh...» (highlighted in bold): 5 1 36 16 35 46 4 200 99.
  • The princess exclaimed «Wow!» (highlighted in bold): 5 1 3 616 35 46 4 200 99.

題目大意:構造一個n個數的序列,滿足公主叫了a次Oh和b次Wow,公主遇到一個數字,這個數字比前面的所有數字都大時她會叫Oh,公主遇到一個數字,這個數字比前面所有數字的和都大時,公主會叫Wow,叫Wow的話就不叫Oh了,構造的數字不能大於5萬,如果不存在則輸出-1


題目分析:首先明確一點,前兩個數字,如果第二個比第一個大,這種情況公主會叫Wow,這題構造方法不唯一,最簡單的應該是二進位制法,舉個例子比如b等於3這時候我們就可以構造0001,0010,0100,1000,0001+0010 = 0011正好比0100小1,同理0001+0010+0100 = 0111,正好比1000小1,又b最大為15,所以構造b的值最大到2^15=32768,然後構造a就方便了,直接往後依次加1即可,構造完a,b,剩下來的數全都等於構造a時的最後一個數即可


#include <cstdio>
int ans[105];

int main()
{
    int a, b, n, cur = 1;
    scanf("%d %d %d", &n, &a, &b);
    ans[1] = 1;
    for(int i = 2; i <= n; i++)
    {
        if(b)
        {
            ans[i] = 1 << (i - 1);
            b --;
        }
        else if(a && i > 2)
        {
            ans[i] = ans[i - 1] + 1;
            a --;
        }
        else
            ans[i] = ans[i - 1];   
    }
    if(a || b)
        printf("-1\n");
    else 
    {
        for(int i = 1; i < n; i++)
            printf("%d ",ans[i]);
        printf("%d\n", ans[n]);
    }
}



D. Bag of mice
time limit per test:2 seconds
memory limit per test:256 megabytes

The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance.

They take turns drawing a mouse from a bag which initially containsw white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice).Princess draws first. What is the probability of the princess winning?

If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one.

Input

The only line of input data contains two integersw and b (0 ≤ w, b ≤ 1000).

Output

Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed10 - 9.

Sample test(s)
Input
1 3
Output
0.500000000
Input
5 5
Output
0.658730159
Note

Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.



題目大意:公主和龍抓老鼠,老鼠有w只白的,b只黑的,先抓到白老鼠的贏,龍每次抓完一隻老鼠後會多跑出去一隻老鼠,一旦沒有老鼠或者沒人抓到白老鼠,則算公主輸,公主和龍抓的老鼠是黑或白是等可能的,龍抓完跑掉的老鼠是黑或白也是等可能的,現在公主先抓,要求公主贏的概率


題目分析:比較好想的一道概率dp,dp[i][j]表示還剩i只白老鼠和j只黑老鼠時公主贏的概率,這裡只需要推公主贏的情況即可,顯然有三種情況公主會贏或者還有贏的可能

注意每一輪是按順序執行的,即公主先抓,龍再抓,老鼠再跑,注意每次還剩多少

第一:公主抓到白老鼠  dp[i][j] += i / (i + j)

第二:公主和龍都抓到黑老鼠,跑出去的是黑老鼠  dp[i][j] = ( j / (i + j) ) * ( (j - 1) / (i + j - 1) * (j - 2) / (i + j - 2) ) * dp[i][j - 3] (減3是因為這種情況下需要至少三隻黑老鼠)

第三:公主和龍都抓到黑老鼠,跑出去的是白老鼠  dp[i][j] = ( j / (i + j) ) * ( (j - 1) / (i + j - 1) * i / (i + j - 2) ) * dp[i - 1][j - 2]

注意初始化,沒有白老鼠,dp[0][i] = 0,沒有黑老鼠dp[i][0] = 1,一隻老鼠都沒有dp[0][0] = 0

最後答案是dp[w][b]


#include <cstdio>
#include <cstring>
int const MAX = 1005;
double dp[MAX][MAX];

int main()
{
    int w, b;
    memset(dp, 0, sizeof(dp));
    scanf("%d %d", &w, &b);
    for(int i = 0; i <= b; i ++)
        dp[0][i] = 0;
    for(int i = 1; i <= w; i++)
        dp[i][0] = 1.0;
    for(int i = 1; i <= w; i++)
    {
        for(int j = 1; j <= b; j++)
        {
            dp[i][j] += (double) i / (i + j);
            if(j >= 2)
                dp[i][j] += (double) j / (i + j) * (double) (j - 1) / (i + j - 1) * (double) i / (i + j - 2) * dp[i - 1][j - 2];
            if(j >= 3)
                dp[i][j] += (double) j / (i + j) * (double) (j - 1) / (i + j - 1) * (double) (j - 2) / (i + j - 2) * dp[i][j - 3];
        }
    }
    printf("%.9f\n", dp[w][b]);
}



E. Porcelain
time limit per test:3 seconds
memory limit per test:256 megabytes

During her tantrums the princess usually smashes some collectable porcelain. Every furious shriek is accompanied with one item smashed.

The collection of porcelain is arranged neatly onn shelves. Within each shelf the items are placed in one row, so that one can access only the outermost items — the leftmost or the rightmost item, not the ones in the middle of the shelf. Once an item is taken, the next item on that side of the shelf can be accessed (see example). Once an item is taken, it can't be returned to the shelves.

You are given the values of all items. Your task is to find the maximal damage the princess' tantrum ofm shrieks can inflict on the collection of porcelain.

Input

The first line of input data contains two integersn (1 ≤ n ≤ 100) andm (1 ≤ m ≤ 10000). The nextn lines contain the values of the items on the shelves: the first number gives the number of items on this shelf (an integer between1 and 100, inclusive), followed by the values of the items (integers between1 and 100, inclusive), in the order in which they appear on the shelf (the first number corresponds to the leftmost item, the last one — to the rightmost one). The total number of items is guaranteed to be at least m.

Output

Output the maximal total value of a tantrum of m shrieks.

Sample test(s)
Input
2 3
3 3 7 2
3 4 1 5
Output
15
Input
1 3
4 4 3 1 2
Output
9
Note

In the first case there are two shelves, each with three items. To maximize the total value of the items chosen, one can take two items from the left side of the first shelf and one item from the right side of the second shelf.

In the second case there is only one shelf, so all three items are taken from it — two from the left side and one from the right side.


題目大意:給n層數字,一共要取m次,每次取數字只能從任意一層的最左或最右取

題目分析:預處理出每層取j (j <= m)個的最大值,之後就是個普通的多重揹包問題


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int sum[105], ma[105], dp[10005];

int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++)
    {
        int num;
        scanf("%d", &num);
        for(int j = 1; j <= num; j++)
        {
            int tmp;
            scanf("%d", &tmp);
            sum[j] = sum[j - 1] + tmp;  //計算字首和
        }
        //預處理
        memset(ma, 0, sizeof(ma));
        for(int j = 0; j <= num; j++)
            for(int k = 0; k <= j; k++)
                ma[j] = max(ma[j], sum[k] + sum[num] - sum[num + k - j]);
        printf("\n");
        //多重揹包
        for(int j = m; j >= 1; j--)
            for(int k = 1; k <= min(j, num); k ++)
                dp[j] = max(dp[j], dp[j - k] + ma[k]);
    }
    printf("%d\n", dp[m]);
}

相關文章