An abandoned sentiment from past 814A

这个题一开始的思路是找b的全排列,往A里面插入,如果找到就停止,这一种方法过了。暴力全排列之前一定要先排序!!!记住记住

做完以后想起来了一种简单方法。(为什么总是这么后知后觉)

A few years ago, Hitagi encountered a giant crab, who stole the whole of her body weight. Ever since, she tried to avoid contact with others, for fear that this secret might be noticed.

To get rid of the oddity and recover her weight, a special integer sequence is needed. Hitagi's sequence has been broken for a long time, but now Kaiki provides an opportunity.

Hitagi's sequence a has a length of n. Lost elements in it are denoted by zeros. Kaiki provides another sequence b, whose length k equals the number of lost elements in a (i.e. the number of zeros). Hitagi is to replace each zero in a with an element from b so that each element in b should be used exactly once. Hitagi knows, however, that, apart from 0, no integer occurs in a and b more than once in total.

If the resulting sequence is not an increasing sequence, then it has the power to recover Hitagi from the oddity. You are to determine whether this is possible, or Kaiki's sequence is just another fake. In other words, you should detect whether it is possible to replace each zero in a with an integer from b so that each integer from b is used exactly once, and the resulting sequence is not increasing.

Input

The first line of input contains two space-separated positive integers n (2 ≤ n ≤ 100) and k (1 ≤ k ≤ n) — the lengths of sequence a and b respectively.

The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 200) — Hitagi's broken sequence with exactly k zero elements.

The third line contains k space-separated integers b1, b2, ..., bk (1 ≤ bi ≤ 200) — the elements to fill into Hitagi's sequence.

Input guarantees that apart from 0, no integer occurs in a and b more than once in total.

Output

Output "Yes" if it's possible to replace zeros in a with elements in b and make the resulting sequence not increasing, and "No" otherwise.

Input
4 2
11 0 0 14
5 4
Output
Yes
Input
6 1
2 3 0 8 9 10
5
Output
No
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 200+10;
int a[maxn];
int b[maxn];
int c[maxn];
int m,n;
bool judge()
{

    sort(b,b+n);
    do
    {
        for(int i = 0; i < m; i++)
            c[i] = a[i];

        int cout1 = 0;
        int flag = 0;
        for(int i = 0; i < m; i++)
        {
            if(c[i] == 0)
            {
                c[i] = b[cout1];
                cout1++;
            }
        }

        for(int i = 0; i < m - 1; i++)
        {
            if(c[i] >= c[i+1])
            {
                flag = 1;
                break;
            }
        }
        if(flag)
        {
            return true;
        }
    }
    while(next_permutation(b,b + n));
    return false;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>m>>n;

    for(int i = 0; i < m; i++)
        scanf("%d",&a[i]);
    for(int i = 0; i < n; i++)
    {
        scanf("%d",&b[i]);
    }

    if(judge())
        printf("Yes\n");
    else
        printf("No\n");

    return 0;
}

附上简答的做法
#include <bits/stdc++.h>

using namespace std;
const int MAXN = 10086;
int a[MAXN], b[MAXN];
int main()
{
    int n, k;
    cin>>n>>k;
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    for(int i = 0; i < k; i++)
    scanf("%d", &b[i]);
    if(k  > 1)
        return 0*printf("Yes\n");
    for(int i = 0; i < n; i++)
    if(a[i] == 0) 
       a[i] = b[0];
    int flag = 1;
    for(int i = 0; i < n - 1; i++)
    {
        if(a[i] > a[i+1])
            flag = 0;
    }
    if(flag)
    printf("No\n");
    else printf("Yes\n");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值