Practice_Codeforces Round #413 (Div. 2)

来源:互联网 发布:java insert的返回值 编辑:程序博客网 时间:2024/05/29 03:44

蒟蒻好久没有水题了~

窝又回来了!

这次还是A + B!


A. Carrot Cakes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In some game by Playrix it takes t minutes for an oven to bake k carrot cakes, all cakes are ready at the same moment t minutes after they started baking. Arkady needs at least n cakes to complete a task, but he currently don't have any. However, he has infinitely many ingredients and one oven. Moreover, Arkady can build one more similar oven to make the process faster, it would take d minutes to build the oven. While the new oven is being built, only old one can bake cakes, after the new oven is built, both ovens bake simultaneously. Arkady can't build more than one oven.

Determine if it is reasonable to build the second oven, i.e. will it decrease the minimum time needed to get n cakes or not. If the time needed with the second oven is the same as with one oven, then it is unreasonable.

Input

The only line contains four integers ntkd (1 ≤ n, t, k, d ≤ 1 000) — the number of cakes needed, the time needed for one oven to bake k cakes, the number of cakes baked at the same time, the time needed to build the second oven.

Output

If it is reasonable to build the second oven, print "YES". Otherwise print "NO".

Examples
input
8 6 4 5
output
YES
input
8 6 4 6
output
NO
input
10 3 11 4
output
NO
input
4 2 1 4
output
YES
Note

In the first example it is possible to get 8 cakes in 12 minutes using one oven. The second oven can be built in 5 minutes, so after 6minutes the first oven bakes 4 cakes, the second oven bakes 4 more ovens after 11 minutes. Thus, it is reasonable to build the second oven.

In the second example it doesn't matter whether we build the second oven or not, thus it takes 12 minutes to bake 8 cakes in both cases. Thus, it is unreasonable to build the second oven.

In the third example the first oven bakes 11 cakes in 3 minutes, that is more than needed 10. It is unreasonable to build the second oven, because its building takes more time that baking the needed number of cakes using the only oven.


题意:一个游戏中,一个炉子耗费t分钟做k个胡萝卜蛋糕,可以再造一个炉子同时工作,花费d分钟。现给定目标蛋糕数n,问是否有必要再造一个炉子。

题解:算出一个炉子做完所有蛋糕花的总时间t1,然后再算出做完一次的时间加上造炉子的时间t2,如果t1 <= t2,则没必要,输出NO,否则YES。

代码:

#include <iostream>using namespace std;int main(){    ios::sync_with_stdio(false);    cin.tie(0);    int n, t, k, d;    cin >> n >> t >> k >> d;    int t1 = (n / k + (n % k == 0 ? 0 : 1)) * t;    int t2 = t + d;    if (t1 <= t2)        cout << "NO" << endl;    else        cout << "YES" << endl;    return 0;}


B. T-shirt buying
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers piai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.

m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.

A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.

You are to compute the prices each buyer will pay for t-shirts.

Input

The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.

The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.

The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.

The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.

The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.

The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.

Output

Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.

Examples
input
5300 200 400 500 9111 2 1 2 32 1 3 2 162 3 1 2 1 1
output
200 400 300 500 911 -1 
input
21000000000 11 11 222 1
output
1 1000000000 

题意:有n件衣服,每件衣服前面和后面都有颜色,现有m个人来买衣服,买衣服的前提是衣服的前面或者后面有它们喜欢的颜色,当然,如果有多件衣服符合要求,当然是选择最便宜的那件了!

题解:我用的暴力法,用结构体数组存储衣服的价格以及前后颜色,并对价格进行排序,对输入的购买者需要的颜色,在结构体数组中进行枚举,如果找到,则输出答案,并将改价格标记为0。嗯,不幸的是,它超时了!!!

TLE代码:

#include <iostream>#include <algorithm>using namespace std;const int maxn = 200010;struct T_shirt{    int p;    int a;    int b;}T[maxn];bool cmp(const T_shirt & x, const T_shirt & y){    return x.p < y.p;}int main(){    ios::sync_with_stdio(false);    cin.tie(0);    int n, m, i, t;    cin >> n;    for (i = 1; i <= n; i++)        cin >> T[i].p;    for (i = 1; i <= n; i++)        cin >> T[i].a;    for (i = 1; i <= n; i++)        cin >> T[i].b;    sort(T+1, T+1+n, cmp);    cin >> m;    while (m--)    {        int ans = 0x3f3f3f3f;        cin >> t;        for (i = 1; i <= n; i++)            if((t == T[i].a || t == T[i].b) && T[i].p)            {                 ans = T[i].p;                 T[i].p = 0;                 break;            }        if (ans == 0x3f3f3f3f)            ans = -1;        if (m == 0)            cout << ans << endl;        else            cout << ans << " ";    }    return 0;}
(同时也测试了cin与printf,都是在第六组数据超时,看来关同步真有用)

学习了一下别人的做法,原来这题可以用stl中set快速查找删除,受教了。

codeforces #413 B T-shirt buying(set快速查找)

代码:

#include <iostream>#include <set>using namespace std;const int maxn = 200010;typedef long long ll;ll p[maxn], ans[maxn];set <ll> s[5];set <ll> :: iterator it;int main(){    ios::sync_with_stdio(false);    cin.tie(0);    int n, m, k = 0, t1, t2,i;    cin >> n;    for (i = 1; i <= n; i++)        cin >> p[i];    for (i = 1; i <= n; i++)    {        cin >> t1;        s[t1].insert(p[i]);    }     for (i = 1; i <= n; i++)    {        cin >> t1;        s[t1].insert(p[i]);    }    cin >> m;    for (i = 1; i <= m; i++)    {        cin >> t2;        if (s[t2].size() == 0)            ans[++k] = -1;        else        {            it = s[t2].begin();            t1 = *it;            ans[++k] = t1;            for (int j = 1; j <= 3; j++)            {                it = s[j].find(t1);//not find and return end                if (it != s[j].end())                    s[j].erase(it);            }        }    }    cout << ans[1];    for (int i = 2; i <= k; i++)        cout << " " << ans[i];    return 0;}

原代码中出现set<ll,less<ll> >s[5];没明白,改成set<ll>s[5];结果无影响。


The end.

2017年5月18日16:57:55

原创粉丝点击