当前位置:网站首页>ZOJ——4104 Sequence in the Pocket(思维问题)
ZOJ——4104 Sequence in the Pocket(思维问题)
2022-06-24 15:36:00 【51CTO】
原题链接: https://zoj.pintia.cn/problem-sets/91827364500/problems/91827370499

测试样例:
Sample Input
2
4
1 3 2 4
5
2 3 3 5 5
Sample Output
2
0
样例解释:
对于第一个样本测试用例,将第三个元素移到最前面(因此序列变为{2,1,3,4}),然后将第二个元素移到最前面(因此序列变为{1, 2, 3,4})。现在,序列是不递减的。
对于第二个样本测试用例,由于已经对序列进行了排序,因此不需要任何操作。
题意: 给你一个整数序列,你可以进行任意次数的操作将序列中的某个元素移到序列前,问你至少要经过多少次才能够是这个序列不递减。
解题思路: 这个题目是真的很简单,我们的目的是要使得序列不递减,那么如果碰到递减的怎么办?我们是不是要按照顺序来排好使得该序列不递减,但是不是每个都要操作呢?那必然不是,我们最终的序列状态即为我们排好序的序列,那么我们用这个排好序的序列与原序列从后往前进行对比(从后往前对比是因为我们的操作是放到序列前面),判断哪些是不用操作的就好,那么其他的就是要通过移到序列最前面来使得序列是非递减的。OK,我们具体看代码。
AC代码:
/*
*
*/
//POJ不支持
//i为循环变量,a为初始值,n为界限值,递增
//i为循环变量, a为初始值,n为界限值,递减。
using
namespace
std;
const
int
inf
=
0x3f3f3f3f;
//无穷大
const
int
maxn
=
1e5;
//最大值。
typedef
long
long
ll;
typedef
long
double
ld;
typedef
pair
<
ll,
ll
>
pll;
typedef
pair
<
int,
int
>
pii;
//*******************************分割线,以上为自定义代码模板***************************************//
int
t,
n,
a[
maxn],
b[
maxn];
int
main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(
cin
>>
t){
while(
t
--){
cin
>>
n;
rep(
i,
0,
n
-
1){
cin
>>
a[
i];
b[
i]
=
a[
i];
}
sort(
b,
b
+
n);
//排序
int
pos
=
n
-
1,
sum
=
0;
//sum统计我们要移到前面的次数
per(
i,
n
-
1,
0){
if(
a[
i]
==
b[
pos])
pos
--;
//不需要往前移
else
sum
++;
//操作前移
}
cout
<<
sum
<<
endl;
}
}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
边栏推荐
- Here comes Wi Fi 7. How strong is it?
- 我与“Apifox”的网络情缘
- Design of CAN bus controller based on FPGA (Part 2)
- 运营商5G用户渗透远远比4G慢,5G的普及还得看中国广电
- Precautions for using JMeter suite to build a pressure test environment
- Arrays API
- 手机同花顺股票开户安全吗!
- 存在安全隐患 路虎召回部分混动揽运
- Leetcode 139. Mot break word Split (medium)
- Remember: never use UTF-8 in MySQL
猜你喜欢

MongoDB入門實戰教程:學習總結目錄

Solution of intelligent all in one machine in expressway service area

使用阿里云RDS for SQL Server性能洞察优化数据库负载-初识性能洞察

构建Go命令行程序工具链

Still worried about missing measurements? Let's use Jacobo to calculate the code coverage

60 个神级 VS Code 插件!!
![[my advanced OpenGL learning journey] learning notes of OpenGL coordinate system](/img/21/48802245fea2921fd5e4a9a2d9ad18.jpg)
[my advanced OpenGL learning journey] learning notes of OpenGL coordinate system

设备通过国标GB28181接入EasyCVR平台,出现断流情况该如何解决?

Build go command line program tool chain

Jenkins 镜像无法更新插件中心的3种解决方法
随机推荐
The decline of China's product managers: starting from the nostalgia for jobs
Two problems of qtreewidget returning as DLL in singleton mode
60 个神级 VS Code 插件!!
Poor remote code execution in Alien Swarm
Industry cases of successful digital transformation
How to implement SQLSERVER database migration in container
PHP export data as excel table
The 30 pictures bring the network protocol layer by layer to life. It's really fragrant!
VNC Viewer方式的远程连接树莓派
Implement Domain Driven Design - use ABP framework - domain logic & application logic
Nifi from introduction to practice (nanny level tutorial) - environment
国产芯片的赶超,让美国手机芯片龙头高通害怕了,出招应对竞争
【云原生 | Kubernetes篇】Kubernetes基础入门(三)
Wechat official account debugging and natapp environment building
Install the imagemagick7.1 library and the imageick extension for PHP
Precautions for using JMeter suite to build a pressure test environment
How to easily realize online karaoke room and sing "mountain sea" with Wang Xinling
Easynvr has been connected to the third-party supervision platform. How to achieve local Internet access
The cold winter can't stop the determination to enter the big factory. The Android interview has a complete knowledge structure, and everything you need to master in the interview is here!
Cap: multiple attention mechanism, interesting fine-grained classification scheme | AAAI 2021