Day 11 C. Make It Good

给定一个整数数组,目标是找到需要删除的最短前缀,使得剩余数组可以经过一定次数的从两端取数操作变为非递减数组。题目提供了示例和输入输出格式,并给出了一些测试用例。

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

Problem:
You are given an array a consisting of n integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from a to make it a good array. Recall that the prefix of the array a=[a1,a2,…,an] is a subarray consisting several first elements: the prefix of the array a of length k is the array [a1,a2,…,ak] (0≤k≤n).

The array b of length m is called good, if you can obtain a non-decreasing array c (c1≤c2≤⋯≤cm) from it, repeating the following operation m times (initially, c is empty):

select either the first or the last element of b, remove it from b, and append it to the end of the array c.
For example, if we do 4 operations: take b1, then bm, then bm−1 and at last b2, then b becomes [b3,b4,…,bm−3] and c=[b1,bm,bm−1,b2].

Consider the following example: b=[1,2,3,4,4,2,1]. This array is good because we can obtain non-decreasing array c from it by the following sequence of operations:

take the first element of b, so b=[2,3,4,4,2,1], c=[1];
take the last element of b, so b=[2,3,4,4,2], c=[1,1];
take the last element of b, so b=[2,3,4,4], c=[1,1,2];
take the first element of b, so b=[3,4,4], c=[1,1,2,2];
take the first element of b, so b=[4,4], c=[1,1,2,2,3];
take the last element of b, so b=[4], c=[1,1,2,2,3,4];
take the only element of b, so b=[], c=[1,1,2,2,3,4,4] — c is non-decreasing.
Note that the array consisting of one element is good.

Print the length of the shortest prefix of a to delete (erase

(1)编GtTt)现算文“testtxt”该文本文件中的内容如下所示: Good now, sit down, and tell me, he that knows. Why this same strict and most observant watch So nightly toils the subject of the land, And why such daily cast of brazen cannon, And foreign mart for implements of war, Why such impress of shipwrights, whose sore task Does not divide the Sunday from the week; 第5部分编程测试题 81 What might be toward, that this sweaty haste Doth make the night joint-labourer with the day: Who is't that can inform me? (2)将读取的文本进行分词后放入列表lw中(注意处理大小写和除标点符)并按厅16个单词的格式输出结果,如图 5-2 所示。 调信的结果为: sod nou sit domm and tell me he that knows why this sime strict and mast lstrvant watch so nightlz toils the subiect of the land and sto maoh aily caat of nzen cannen and foreign micrt for implements of war why ruch iwprest of shipnrigits whecs shr it does not divide the sunday from the weok what might be torand that this iweety doth make the nlght oint-lab urer viih the day wno itt that ca inlers me 图5-2输出结果1 (3)根据列表lw的内容生成字典dic(键为单词,值为单词个数)。 (4)根据字典dic,分别输出单词ofand、such、why的个数,按图 5-3所示的格式输出结果。 列表 海结更为: 买,有4个 and有4个 auch有2个 he有3个 图5-3输出结果2 (5)将写程文件名“zhpy”保存算M盘根目录。 请写出代码并说明代码的意思
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值