题解 CF814A 【An abandoned sentiment from past】

题目链接:https://www.luogu.com.cn/problem/CF814A


这道题不难,可以用贪心来做。
贪心思路:因为把a数组的0变成b数组的元素后,要使a数组不单调递增,
所以使b数组单调递减后,
再向a数组依次插进去,一定是最优解。


极不严谨 的证明:
(1)k>1且k为整数
∵b数组没有重复数,
∴排序后b数组只能为单调递减。
又∵a数组中有k个数为递减,
∴a数组不可能为递增数列。
(2) k = 1
显然a数组可能为递增序列。
(贪心主要针对第一种情况,第二种情况在实现贪心时会被顺便处理)


CODE:

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0)  //玄学加速

const int maxn = 3e2;  //个人习惯

int n, k, a[maxn], b[maxn], top = -1;
//定义n和k,a数组,b数组,top作后面b数组的下标
bool cmp(int x,int y){
    return x > y;
}  //从大到小sort的比较函数
int main ()
{
    IO;  //调用
    //freopen ("CF814A.in","r",stdin);
    //freopen ("CF814A.out","w",stdout);  //NOIP需要文件读写
    cin >> n >> k;
    for(int i = 0;i < n; i++)
        cin >> a[i];

    for(int i = 0;i < k; i++)
        cin >> b[i];

    sort(b, b + k, cmp);  //把b数组从大到小排序
    for(int i = 0;i < n; i++)
        if(a[i] == 0) a[i] = b[++top];  //把b数组中的每一个元素赋给a数组

    for(int i = 1;i < n; i++){
        if(a[i] <= a[i - 1]){
            cout << "Yes\n";
            return 0;
        }
    }  //判断是否单调递增
    cout << "No\n";
    return 0;
 } 
发布了9 篇原创文章 · 获赞 1 · 访问量 514

猜你喜欢

转载自blog.csdn.net/live_now/article/details/105024773