Codeforces Round #378 (Div. 2)C. Epidemic in Monstropolis

链接:http://codeforces.com/contest/733/problem/C

题意:给定一个长度为n的a数组和一个长度为k的b数组,要求将a数组中合并成b数组,每次合并只能合并相邻的两个并且大吃小,输出NO或YES及合并过程。

分析:首先确定几个NO的判定方法,因为是相邻合并那么肯定是一段a合并成一个b[j],那么如果这一段a中元素个数>1并且全部都是一样的值那么就不能合并,反之则一定能合并起来。YES的输出的话只有找到一个段中的最大值并且旁边有一个比它小的值,那么先向小的那边合并然后再先右边合并就行了。

代码:

#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const db eps=1e-5;
const int N=510;
const int M=1e5+10;
const ll MOD=1000000007;
const int mod=1000000007;
const int MAX=1000000010;
const double pi=acos(-1.0);
int n,k,a[N],b[N],c[N];
int pd() {
    int i,j,now=1,sum=0,bo;
    for (i=1;i<=n;i++) {
        if (sum+a[i]>b[now]) return 0;
        sum+=a[i];
        if (sum==b[now]) c[now]=i,sum=0,now++;
    }
    for (i=1;i<=k;i++) {
        bo=0;
        if (c[i]-c[i-1]==1) bo=1;
        for (j=c[i-1]+1;j<=c[i];j++)
        if (a[j]!=a[c[i-1]+1]) bo=1;
        if (!bo) return 0;
    }
    return 1;
}
void deal() {
    int i,j,h,mx;
    for (i=1;i<=k;i++) {
        h=mx=0;
        for (j=c[i-1]+1;j<=c[i];j++)
        if (a[j]>mx) mx=a[j];
        for (j=c[i-1]+1;j<=c[i];j++)
        if (a[j]==mx&&((j>c[i-1]+1&&a[j-1]!=mx)||(j<c[i]&&a[j+1]!=mx))) break ;
        if (j>c[i-1]+1&&a[j-1]!=mx) {
            for (h=j-c[i-1];h>1;h--) printf("%d L\n", i-1+h);
            for (h=j+1;h<=c[i];h++) printf("%d R\n", i);
        } else if (j<c[i]&&a[j+1]!=mx) {
            for (h=j+1;h<=c[i];h++) printf("%d R\n", i-1+j-c[i-1]);
            for (h=j-c[i-1];h>1;h--) printf("%d L\n", i-1+h);
        }
    }
}
int main()
{
    int i,j;
    ll sum1=0,sum2=0;
    scanf("%d", &n);
    for (i=1;i<=n;i++) scanf("%d", &a[i]),sum1+=(ll)a[i];
    scanf("%d", &k);
    for (i=1;i<=k;i++) scanf("%d", &b[i]),sum2+=(ll)b[i];
    memset(c,0,sizeof(c));
    if (sum1!=sum2||!pd()) printf("NO\n");
    else {
        printf("YES\n");deal();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值