我是靠谱客的博主 聪慧犀牛,最近开发中收集的这篇文章主要介绍Codeforces Round #378 (Div. 2) -- C. Epidemic in Monstropolis (贪心模拟),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

大体题意:

有n个怪兽在排队,告诉你刚开始每个怪兽的体重,只有体重大的怪兽能吃体重小的怪兽!并且只有相邻的怪兽才能吃,吃掉怪兽后体重增加被吃怪兽的体重,告诉你最后的体重序列,问是否存在这样一种吃法! 存在输出 吃的过程!否则输出NO

思路:

这个题在109个样例WA掉了!  思路就偏了!

简单贪心模拟好了!

其实思路也很简单! 因为他只能吃相邻的怪兽,所以序列最后一个怪兽的体重 一定是刚开始怪兽最后几个互相吃来得到的!因此我们可以用vector 来模拟这个队列!

从最后一个怪兽开始找,找到所有体重之和 为 最后结果的最后一个体重时,如果这个区间内的怪兽有一个和其他不相等,那么就可以产生最终的怪兽! 从最大的怪兽开始无论怎样吃都能得到最终的结果!

这样在把这个区间删掉即可!

详细见代码:

#include <bits/stdc++.h>
#define ps push_back
#define mr make_pair
#define fi first
#define se second
#define Max(a,b) (a) > (b) ? (a) : (b)
#define Min(a,b) (a) > (b) ? (b) : (a)
using namespace std;
const int maxn = 500 + 7;
vector<int>v;
int b[maxn];
vector<pair<int,string> >ans;
int main(){
    int n;
    scanf("%d",&n);
    for (int i = 0; i < n; ++i){
        int x;
        scanf("%d",&x);
        v.ps(x);
    }
    int k;
    scanf("%d",&k);
    for (int i = 0; i < k; ++i){
        scanf("%d", b+i);
    }
    if (n == 1){
        if (k != 1 || v[0] != b[0]) return 0 * puts("NO");
    }
    int uber = k-1;
    bool ok = 0;
    while(1){
        int i;
        int len = v.size();
        int sum = 0;
        int flag = 0;
        int M = 0,Mxp;
        for (i = len-1; ~i; --i){
            sum += v[i];
            if (M < v[i]){
                M = v[i];
                Mxp = i;
            }
            if (sum == b[uber]){
                flag = 1;
                break;
            }
        }
        if (!flag){ ok = 0; break; }
        flag = 0;
        for (int j = i; j < len; ++j){
            if (v[j] != v[i]){
                flag = 1;
                break;
            }
        }
        if (!flag && len-i > 1){ ok = 0; break; }
        int dir = 0;
        for (int j = i; j < len; ++j){
            if (len-i == 1)break;
            if (v[j] != M)continue;
            if (j == i){
                if (v[j+1] < v[j]){ Mxp = i; dir = 1;break; }

            }
            else if (j == len-1){
                if (v[j-1] < v[j]) { Mxp = len-1;dir =- 1;break; }
            }
            else {
                if (v[j+1] < v[j]) { Mxp = j;dir = 1;break;}
                if (v[j-1] < v[j]) { Mxp = j;dir = -1;break;}
            }
        }
        if (dir == 1){
            for (int j = Mxp+1; j < len; ++j){
                ans.ps(mr(Mxp+1,"R"));
            }
            for (int j = Mxp-1; j >= i; --j){
                ans.ps(mr(j+2,"L"));
            }
        }
        else {

            for (int j = Mxp-1; j >= i; --j){
                ans.ps(mr(j+2,"L"));
            }
            for (int j = Mxp+1; j < len; ++j){
                ans.ps(mr(i+1,"R"));
            }
        }

        for (int j = i; j < len; ++j) v.pop_back();
        --uber;
        if (uber == -1){
            if ((int)v.size()){ok = 0; break;}
            else {ok = 1; break;}
        }
        if (v.empty()){
            if (~uber) { ok = 0; break; }
            else {ok = 1; break; }
        }
    }
    if (!ok) puts("NO");
    else if (v.empty()){
        puts("YES");
        for (int i = 0; i < ans.size(); ++i) printf("%d %sn",ans[i].fi, ans[i].se.c_str());
    }
    return 0;
}


C. Epidemic in Monstropolis
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city.

Soon, monsters became hungry and began to eat each other.

One monster can eat other monster if its weight is strictly greater than the weight of the monster being eaten, and they stand in the queue next to each other. Monsters eat each other instantly. There are no monsters which are being eaten at the same moment. After the monsterA eats the monster B, the weight of the monster A increases by the weight of the eaten monster B. In result of such eating the length of the queue decreases by one, all monsters after the eaten one step forward so that there is no empty places in the queue again. A monster can eat several monsters one after another. Initially there were n monsters in the queue, the i-th of which had weight ai.

For example, if weights are [1, 2, 2, 2, 1, 2] (in order of queue, monsters are numbered from 1 to 6 from left to right) then some of the options are:

  1. the first monster can't eat the second monster because a1 = 1 is not greater than a2 = 2;
  2. the second monster can't eat the third monster because a2 = 2 is not greater than a3 = 2;
  3. the second monster can't eat the fifth monster because they are not neighbors;
  4. the second monster can eat the first monster, the queue will be transformed to [3, 2, 2, 1, 2].

After some time, someone said a good joke and all monsters recovered. At that moment there were k (k ≤ n) monsters in the queue, the j-th of which had weight bj. Both sequences (a and b) contain the weights of the monsters in the order from the first to the last.

You are required to provide one of the possible orders of eating monsters which led to the current queue, or to determine that this could not happen. Assume that the doctor didn't make any appointments while monsters were eating each other.

Input

The first line contains single integer n (1 ≤ n ≤ 500) — the number of monsters in the initial queue.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial weights of the monsters.

The third line contains single integer k (1 ≤ k ≤ n) — the number of monsters in the queue after the joke.

The fourth line contains k integers b1, b2, ..., bk (1 ≤ bj ≤ 5·108) — the weights of the monsters after the joke.

Monsters are listed in the order from the beginning of the queue to the end.

Output

In case if no actions could lead to the final queue, print "NO" (without quotes) in the only line.

Otherwise print "YES" (without quotes) in the first line. In the next n - k lines print actions in the chronological order. In each line print x — the index number of the monster in the current queue which eats and, separated by space, the symbol 'L' if the monster which stays the x-th in the queue eats the monster in front of him, or 'R' if the monster which stays the x-th in the queue eats the monster behind him. After each eating the queue is enumerated again.

When one monster eats another the queue decreases. If there are several answers, print any of them.

Examples
input
6
1 2 2 2 1 2
2
5 5
output
YES
2 L
1 R
4 L
3 L
input
5
1 2 3 4 5
1
15
output
YES
5 L
4 L
3 L
2 L
input
5
1 1 1 3 3
3
2 1 6
output
NO
Note

In the first example, initially there were n = 6 monsters, their weights are [1, 2, 2, 2, 1, 2] (in order of queue from the first monster to the last monster). The final queue should be [5, 5]. The following sequence of eatings leads to the final queue:

  • the second monster eats the monster to the left (i.e. the first monster), queue becomes [3, 2, 2, 1, 2];
  • the first monster (note, it was the second on the previous step) eats the monster to the right (i.e. the second monster), queue becomes[5, 2, 1, 2];
  • the fourth monster eats the mosnter to the left (i.e. the third monster), queue becomes [5, 2, 3];
  • the finally, the third monster eats the monster to the left (i.e. the second monster), queue becomes [5, 5].

Note that for each step the output contains numbers of the monsters in their current order in the queue.




最后

以上就是聪慧犀牛为你收集整理的Codeforces Round #378 (Div. 2) -- C. Epidemic in Monstropolis (贪心模拟)的全部内容,希望文章能够帮你解决Codeforces Round #378 (Div. 2) -- C. Epidemic in Monstropolis (贪心模拟)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部