首页 > 其他分享 >1101. Quick Sort (25)

1101. Quick Sort (25)

时间:2022-11-25 15:05:00浏览次数:68  
标签:Sort leftmax cout qqsot 25 答案 ans Quick rightmin


1101. Quick Sort (25)


时间限制



200 ms



内存限制



65536 kB



代码长度限制



16000 B



判题程序



Standard



作者



CAO, Peng


There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. Given N distinct positive integers after a run of partition, could you tell how many elements could be the selected pivot for this partition?

For example, given N = 5 and the numbers 1, 3, 2, 4, and 5. We have:

  • 1 could be the pivot since there is no element to its left and all the elements to its right are larger than it;
  • 3 must not be the pivot since although all the elements to its left are smaller, the number 2 to its right is less than it as well;
  • 2 must not be the pivot since although all the elements to its right are larger, the number 3 to its left is larger than it as well;
  • and for the similar reason, 4 and 5 could also be the pivot.
    Hence in total there are 3 pivot candidates.
    Input Specification:
    Each input file contains one test case. For each case, the first line gives a positive integer N (<= 105). Then the next line contains N distinct positive integers no larger than 109. The numbers in a line are separated by spaces.
    Output Specification:
    For each test case, output in the first line the number of pivot candidates. Then in the next line print these candidates in increasing order. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line. Sample Input: 5 1 3 2 4 5 Sample Output: 3 1 4 5
N个数
对于这N个数,那些左边都小于等于它,右边都大于等于它的是属于要输出的,进入ans中
ans排好序从小到大
输出ans中有几个数+换行
输出ans[0] ans[1]……+换行(PS即使没有数也要换行(否则一个测试点错误),如果有数,每两个数要空格,最后的数后面没有空格)
这次pat2015考试中是B题,由于以前对于qsort些起来不熟,以为要写qsort没有认真看。这题就是这次pat中连错误代码都没有提交的。考完仔细一看,根本不用qsort 的partition评测结果

时间

结果

得分

题目

语言

用时(ms)

内存(kB)

用户

9月14日

21:24

答案正确

​25​

​1101​

​C++ (g++ 4.7.2)​

43

1708

​datrilla​

测试点

测试点

结果

用时(ms)

内存(kB)

得分/满分

0

答案正确

1

308

12/12

1

答案正确

40

1708

2/2

2

答案正确

24

564

4/4

3

答案正确

43

1024

3/3

4

答案正确

42

692

2/2

5

答案正确

42

820

2/2

#include<iostream>   
#include<vector>
using namespace std;
int main()
{
int N, i,leftmax_rightmin;
cin >> N;
vector<int>qqsot(N);
vector<bool>flag(N, true);
vector<int>ans;
for (i = 0; i < N; i++)
{
cin >> qqsot[i];
if (0 == i)leftmax_rightmin = qqsot[i];
else if (leftmax_rightmin > qqsot[i])
flag[i] = false;
else leftmax_rightmin = qqsot[i];
}
for (i = N - 1, leftmax_rightmin = qqsot[N - 1]; i >= 0; i--)
{
if (leftmax_rightmin<qqsot[i])
flag[i] = false;
else
{
leftmax_rightmin = qqsot[i];
if (flag[i]) ans.push_back(qqsot[i]);
}
}
N = ans.size();
cout << N-- << endl;
if (!ans.empty())
{
cout << ans[N];
while(N--)cout << " " << ans[N];
}
cout << endl;
system("pause");
return 0;
}

评测结果

时间

结果

得分

题目

语言

用时(ms)

内存(kB)

用户

9月12日

23:36

答案正确

​25​

​1101​

​C++ (g++ 4.7.2)​

52

1836

​datrilla​

测试点

测试点

结果

用时(ms)

内存(kB)

得分/满分

0

答案正确

1

308

12/12

1

答案正确

52

1836

2/2

2

答案正确

24

564

4/4

3

答案正确

44

948

3/3

4

答案正确

42

824

2/2

5

答案正确

42

820

2/2

#include<iostream>   
#include<vector>
#include<algorithm>
using namespace std;
bool anscmp(const int &A, const int &B){return A < B; }
int main()
{
int N, i,rightmin,leftmax;
cin >> N;
vector<int>qqsot(N);
vector<bool>flag(N,true);
vector<int>ans;
for (i = 0; i < N; i++)
{
cin >> qqsot[i];
if (0 == i)leftmax = qqsot[i];
else if(leftmax > qqsot[i])
flag[i]=false;
else leftmax =qqsot[i];
}
for (i = N-1; i >=0; i--)
{
if (N - 1 == i)rightmin = qqsot[i];
else if(rightmin<qqsot[i])
flag[i]=false;
else rightmin = qqsot[i];
if (flag[i])
ans.push_back(qqsot[i]);
}
N = ans.size();
cout <<N<<endl;
if (!ans.empty())
{
sort(ans.begin(), ans.end(), anscmp);
cout << ans[0];
for (i = 1; i < N; i++)cout <<" "<< ans[i];
}
cout << endl;
system("pause");
return 0;
}

评测结果

时间

结果

得分

题目

语言

用时(ms)

内存(kB)

用户

9月12日

20:59

答案正确

​25​

​1101​

​C++ (g++ 4.7.2)​

51

2452

​datrilla​

测试点

测试点

结果

用时(ms)

内存(kB)

得分/满分

0

答案正确

1

308

12/12

1

答案正确

51

2452

2/2

2

答案正确

25

948

4/4

3

答案正确

44

1716

3/3

4

答案正确

42

1588

2/2

5

答案正确

42

1592

2/2

#include<iostream>   
#include<vector>
#include<algorithm>
using namespace std;
bool anscmp(const int &A, const int &B){return A < B; }
int main()
{
int N, i;
cin >> N;
vector<int>qqsot(N);
vector<int>rightmin(N);
vector<int>leftmax(N);
vector<int>ans;
for (i = 0; i < N; i++)
{
cin >> qqsot[i];
if (0 == i)leftmax[i] = qqsot[i];
else leftmax[i] = leftmax[i - 1] > qqsot[i] ? leftmax[i - 1] : qqsot[i];
}
for (i = N-1; i >=0; i--)
{
if (N - 1 == i)rightmin[i] = qqsot[i];
else rightmin[i] = rightmin[i + 1]<qqsot[i] ? rightmin[i+1] : qqsot[i];
if (leftmax[i]<=qqsot[i]&&rightmin[i]>=qqsot[i])
ans.push_back(qqsot[i]);
}
N = ans.size();
cout <<N<<endl;
if (!ans.empty())
{
sort(ans.begin(), ans.end(), anscmp);
cout << ans[0];
for (i = 1; i < N; i++)cout <<" "<< ans[i];
}
cout << endl;
system("pause");
return 0;
}

标签:Sort,leftmax,cout,qqsot,25,答案,ans,Quick,rightmin
From: https://blog.51cto.com/datrilla/5886730

相关文章

  • Quick BI、帆软Fine BI等BI产品,优势详细介绍
    数据价值成为诸多企业越发关注的重点价值,在此背景下助力企业挖掘数据价值的嗯一系列商业BI产品如雨后春笋般纷纷出现。市面上涌现出诸多名目繁多的商业BI产品,令人目不暇接,其......
  • 1006. Sign In and Sign Out (25)
    1006.SignInandSignOut(25)时间限制400ms内存限制65536kB代码长度限制16000B判题程序......
  • 1009. Product of Polynomials (25)
    1009.ProductofPolynomials(25)时间限制400ms内存限制65536kB代码长度限制16000B判题程序......
  • 1002. A+B for Polynomials (25)
    1002.A+BforPolynomials(25)时间限制400ms内存限制65536kB代码长度限制16000B判题程序......
  • 1059. Prime Factors (25)
    1059.PrimeFactors(25)时间限制50ms内存限制65536kB代码长度限制16000B判题程序Stand......
  • 2022-11-25Acwing每日一题
    本系列所有题目均为Acwing课的内容,发表博客既是为了学习总结,加深自己的印象,同时也是为了以后回过头来看时,不会感叹虚度光阴罢了,因此如果出现错误,欢迎大家能够指出错误,我......
  • 11.25.2
    #include<stdio.h>intmain(){ inta,n,i; unsignedlonglongsum=0; scanf("%d",&n); a=2; for(i=1;i<=n;i++) { sum+=a; a=a*10+a%10; } printf("%llu",sum)......
  • 数据卡顿怎么办,瓴羊Quick BI强劲数据引擎来帮忙
    ​大数据成为企业数字化转型发展热门词汇,大数据也是企业实现迭代升级及创新发展的重要资产。想要真正有效挖掘随着企业经营管理而不断产生的海量数据,需要依托瓴羊QuickBI的......
  • 简单便捷的在线表格工具,瓴羊Quick BI广受好评
    国内企业开启数字化转型发展之路,通过瓴羊QuickBI等为代表的商业BI工具可有效提升企业数字化工作水平,通过其高效的资源整合能力、数据可视化能力等,助力数据分析人员提升工作......
  • CF1251E2
    非常神的贪心,先要发现以下两个性质:要花钱收买的一些人,那么肯定是在一开始就收买他们。按照\(m\)升序排序,那么处理\(m=x\)时,\(m=1\simx-1\)的人一定都投了票,不管是......