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 | 43 | 1708 |
测试点
测试点 | 结果 | 用时(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 | 52 | 1836 |
测试点
测试点 | 结果 | 用时(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 | 51 | 2452 |
测试点
测试点 | 结果 | 用时(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>标签:Sort,leftmax,cout,qqsot,25,答案,ans,Quick,rightmin From: https://blog.51cto.com/datrilla/5886730
#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;
}