An abandoned sentiment from past

本文探讨了一种特殊的整数序列替换问题,通过合理的元素填充使序列不呈上升趋势。介绍了输入输出格式,并提供了两种实现思路及代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. An abandoned sentiment from past
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
Input
4 2
11 0 0 14
5 4
Output
Yes
Input
6 1
2 3 0 8 9 10
5
Output
No
Input
4 1
8 94 0 4
89
Output
Yes
Input
7 7
0 0 0 0 0 0 0
1 2 3 4 5 6 7
Output
Yes
Note

In the first sample:

  • Sequence a is 11, 0, 0, 14.
  • Two of the elements are lost, and the candidates in b are5 and 4.
  • There are two possible resulting sequences: 11, 5, 4, 14 and11, 4, 5, 14, both of which fulfill the requirements. Thus the answer is "Yes".

In the second sample, the only possible resulting sequence is 2, 3, 5, 8, 9, 10, which is an increasing sequence and therefore invalid.


题意:输入数组a ,b。将数组b中的数填入到数组a中元素为0的位置(一个数只能填一次),如果数组a是递增的就输出No,否则就是Yes。

思路:

一开始想的用深搜来解决,结果耽误了一个半小时。。。仔细想想其实就是一个小技巧,只有k>1,一定就是yes,因为那k个数可以任意摆放,一定有不上升的顺序。然后再单独处理k==1时的情况。

代码:


#include<iostream>
#include<algorithm>
using namespace std;
bool flag;
int main()
{
    int n, k;
    int mark;
    while(cin >> n >> k)
    {
        flag=0;
        int a[205], b[205];
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            if (a[i] == 0)
            {
                mark = i;
            }
        }
        for (int i = 0; i < k; i++)
            cin >> b[i];
        if (k > 1)
        {
             cout << "Yes" << endl;
        }
        else
        {
            a[mark]=b[0];
            for (int i =1; i<n;i++)
            {
                if (a[i]<a[i-1])
                {
                    flag =1;
                    break;
                }
            }
            if (flag)
                cout << "Yes" << endl;
            else
                cout << "No" << endl;
        }
    }
    return 0;
}



dfs

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
using namespace std;
int n,k,a[105],b[105],c[105];
bool flag,ok[205];
bool work()
{
    for(int i=1;i<=n;i++)
    {
        if(a[i]<a[i-1]) return true;
    }
    return false;
}
void dfs(int i)
{
    if(flag) return;
    for(int j=1;j<=k;j++)
    {
        if(!ok[b[j]])
        {
            ok[b[j]]=true;
            a[c[i]]=b[j];
            if(i==k)
            {
                if(work())
                {
                    flag=true;
                    return;
                }
            }
            else
            {
                dfs(i+1);
            }
            ok[b[j]]=false;
           // a[c[i]]=b[j];
        }
    }
}
int main()
{
    scanf("%d%d",&n,&k);
    int i,num=0;
    a[0]=0;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]==0)
        {
            num++;
            c[num]=i;
        }
    }
    for(i=1;i<=k;i++)
    {
        scanf("%d",&b[i]);
    }
    flag=false;
    memset(ok,false,sizeof(ok));
    dfs(1);
    if(flag) printf("Yes\n");
    else printf("No\n");
    return 0;
}


心得:

读准题目很重要。。。方法也很重要。。。。唉。。。自己能力是真的不行。。。这么一个水题,捯饬了一个半小时、。。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值