Nordic Collegiate Programming Contest 2019

A. Alphabet Animals

You are playing a game in which a group of players take turns saying animal names. The animal name you say when it is your turn must start with the same letter as the previously said animal ends with and it must not have been said previously in this round of the game. If there is no valid name or you cannot come up with one you are eliminated.

Given the last animal name said before your turn and a list of all names not yet used, can you make it through this turn? If so, can you make sure to eliminate the next player?Wikimedia Commons

Input

The first line of input contains a single word, the animal that theprevious player just said. The next line contains a single integer n (0 ≤ n ≤ 10⁵), the number of valid unused animal names. Each of the following n lines contains one valid unused animal name.

All animal names (including the one the previous player said) are unique and consist of at least 1 and at most 20 lower case letters‘a’-‘z’.

Output

If there is any animal name you can play that eliminates the next player, output the first such name from the input list, followed by an exclamation mark. Otherwise, if there is any animal name that you can play, output the first such name. Otherwise, output a question mark (in this case you will just have to make up a fake name in the hope that the others will trust you that this is a real animal).

輸出時每行末尾的多餘空格,不影響答案正確性

樣例輸入1複製

pig
2
goat
toad

樣例輸出1複製

goat

樣例輸入2複製

dog
2
snake
emu

樣例輸出2複製

?

樣例輸入3複製

hare
3
bee
cat
eagle

樣例輸出3複製

eagle!

題意:

單詞接龍,現在輪到你了,給出上一位玩家的單詞和你可以用的單詞,是否可能將下一位選手淘汰(接不了)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
const double PI = acos(-1.0);
const int N = 1e5 + 10;

string s[N];
vector<int>vec;
int vis[30];

int main()
{
    int n;
    string pre;
    cin >> pre;
    int len = pre.size();
    memset(vis, 0, sizeof(vis));
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
    {
        cin >> s[i];
        vis[s[i][0] - 'a']++;
        if(s[i][0] == pre[len - 1])
        {
            vec.push_back(i);
        }
    }
    int siz = vec.size();
    if(siz == 0)
    {
        cout<<"?"<<'\n';
        return 0;
    }
    int id = vec[0];
    bool flag = 0;
    for(int i = 0; i < siz; ++i)
    {
        int len = s[vec[i]].size();
//        cout<<s[vec[i]]<<'\n';
        vis[s[vec[i]][0] - 'a']--;
        if(!vis[s[vec[i]][len - 1] - 'a'])
        {
            id = vec[i];
            flag = 1;
            break;
        }
        vis[s[vec[i]][0] - 'a']++;
    }
    cout<<s[id];
    if(flag)
        cout<<"!";
    cout<<'\n';
    vec.clear();
    return 0;
}

B. Building Boundaries

Maarja wants to buy a rectangular piece of land and then construct three buildings on that land.

The boundaries of the buildings on the ground must have rectangularsizes a₁ × b₁, a₂ × b₂, and a₃ × b₃. They can touch each other but theymay not overlap. They can also be rotated as long as their sides arehorizontal and vertical.

What is the minimum area of land Maarja has to buy?

 

Input

The input consists of multiple test scenarios. The first line of inputcontains a single integer t (1 ≤ t ≤ 1000), the number of scenarios.Then follow the t scenarios. Each scenario consists of a single line,containing six integers a₁, b₁, a₂, b₂, a₃ and b₃(1 ≤ a₁, b₁, a₂, b₂, a₃, b₃ ≤ 10⁹).

Output

For each test scenario, output the minimum area of land such that Maarjacan construct the three buildings.

輸出時每行末尾的多餘空格,不影響答案正確性

樣例輸入複製

2
2 3 2 2 1 1
2 4 5 1 2 3

樣例輸出複製

12
21

題意:

給三個矩形,把他們放在一個矩形裏面,求矩形最小佔地面積

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-12;
const double PI = acos(-1.0);
const int N = 455;

struct node
{
    ll x, y;
}s[3][2];

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        for(int i = 0; i < 3; ++i)
        {
            scanf("%lld%lld", &s[i][0].x, &s[i][0].y);
            s[i][1].x = s[i][0].y;
            s[i][1].y = s[i][0].x;
        }
        ll minn = inf;
        for(int i = 0; i < 3; ++i)
        {
            for(int j = i + 1; j < 3; ++j)
            {
                int k;
                for(int p = 0; p < 3; ++p)
                {
                    if(i != p && j != p)
                    {
                        k = p;
                        break;
                    }
                }
                for(int o = 0; o < 2; ++o)
                {
                    for(int p = 0; p < 2; ++p)
                    {
                        for(int q = 0; q < 2; ++q)
                        {
                            minn = min(minn, max(s[k][q].x, (s[i][o].x + s[j][p].x)) * (max(s[i][o].y, s[j][p].y) + s[k][q].y));
                            minn = min(minn, (s[i][o].x + max(s[j][p].x, s[k][q].x)) * max(s[i][o].y, s[j][p].y + s[k][q].y));
                            minn = min(minn, (s[i][o].x + s[j][p].x + s[k][q].x) * max(s[i][o].y, max(s[j][p].y, s[k][q].y)));
                        }
                    }
                }
            }
        }
        cout<<minn<<'\n';
    }
    return 0;
}

 C. Cocoa Coalition

Alice and Bob decide to share a chocolate bar, which is an n by m rectangular grid of chocolate cells. They decide that Alice should get (a < n ⋅ m) pieces and that Bob should get (b = n ⋅ m − a) pieces. To split the chocolate bar, they repeatedly take a singlepiece of chocolate and break it either horizontally or vertically,creating two smaller pieces of chocolate. See Figure 1for an example.

What is the minimum number of splits that Alice and Bob need to perform in order to split the n-by-m chocolate bar into two piles consisting of a and b chocolate cells?

 

Input

The input consists of a single line, containing the three integers n, m and a (1 ≤ n, m ≤ 10⁶, 1 ≤ a < n ⋅ m).

Output

Output the minimum number of splits needed to achieve the desireddivision of chocolate.

輸出時每行末尾的多餘空格,不影響答案正確性

樣例輸入1複製

3 10 9

樣例輸出1複製

1

樣例輸入2複製

10 10 71

樣例輸出2複製

3

題意:

在 n * m 塊巧克力中切出 a 塊巧克力,只能橫着切或豎着切,問最少切幾刀

思路:

(1)若 a % n == 0 || a % m == 0,一刀即可

(2)若 a 可以分解爲兩正數之積,且這兩個數一個小於 n 並且一個小於 m ,切2刀

(3)其餘情況都是3刀

注意:不一定是小塊好切(比如 n = 4, m = 10, a = 13, 答案是切出27塊來,切兩刀),因此 a 和 n * m - a 要比較一下

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
const double PI = acos(-1.0);
const int N = 105;

ll n, m, a;

ll solve(ll a)
{
    ll ans;
    if(a % n == 0 || a % m == 0)
    {
        ans = 1;
    }
    else
    {
        bool flag = 0;
        for(ll i = 1; i <= sqrt(a); ++i)
        {
            if(a % i == 0)
            {
                if(a / i < n && i < m)
                {
                    flag = 1;
                    break;
                }
                if(a / i < m && i < n)
                {
                    flag = 1;
                    break;
                }
            }
        }
        if(flag)
            ans = 2;
        else
            ans = 3;
    }
    return ans;
}

int main()
{
    while(~scanf("%lld%lld%lld", &n, &m, &a))
    {
        cout<<min(solve(a),solve(n * m - a))<<'\n';
    }
    return 0;
}

 G. Hot Hike

In order to pass time during your vacation, you decided to go on a hike to visit a scenic lake up in the mountains. Hiking to the lake will take you a full day, then you will stay there for a day to rest and enjoy the scenery, and then spend another day hiking home, for a total of three days. However, the accursed weather this summer is ridiculously warm and sunny, and since severe dehydration is not at the top of your priority list you want to schedule the three-day trip during some days where the two hiking days are the least warm. In particular you want to minimize the maximum temperature during the two hiking days.

 

Given the current forecast of daily maximum temperatures during your vacation, what are the best days for your trip?

Input

The first line of input contains an integer n (3 ≤ n ≤ 50), the length of your vacation in days. Then follows a line containing n integers t1, t2, …, tn ( − 20 ≤ ti ≤ 40), where ti is the temperature forecast for the ith day of your vacation.

Output

Output two integers d and t, where d is the best day to start your trip, and t is the resulting maximum temperature during the two hiking days. If there are many choices of d that minimize the value of t, then output the smallest such d.

輸出時每行末尾的多餘空格,不影響答案正確性

樣例輸入1複製

5
23 27 31 28 30

樣例輸出1複製

2 28

樣例輸入2複製

4
30 20 20 30

樣例輸出2複製

1 30

簽到

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
const double PI = acos(-1.0);
const int N = 55;

int t[N];

int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        int id = 0;
        int minn = 50;
        for(int i = 1; i <= n; ++i)
        {
            scanf("%d", &t[i]);
            if(i >= 3)
            {
                int tmp = max(t[i - 2], t[i]);
                if(tmp < minn)
                {
                    minn = tmp;
                    id = i - 2;
                }
            }
        }
        cout<<id<<' '<<minn<<'\n';
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章