An abandoned sentiment from past 814A

来源:互联网 发布:番茄时间管理 知乎 编辑:程序博客网 时间:2024/06/07 20:59

这个题一开始的思路是找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 sequenceb, whose length k equals the number of lost elements ina (i.e. the number of zeros). Hitagi is to replace each zero ina 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 ina 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 fromb is used exactly once, and the resulting sequence isnot 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 sequencea and b respectively.

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

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

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

Output

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

Input
4 211 0 0 145 4
Output
Yes
Input
6 12 3 0 8 9 105
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;}


原创粉丝点击