pat 1113. Integer Set Partition (25)
2017-02-18 21:48
489 查看
https://www.patest.cn/contests/pat-a-practise/1113
#include <cstdio>
#include <algorithm>
using namespace std;
int a[100001],k,n;
void quicksort(int l,int r) {
swap(a[l], a[(l + r) / 2]);
if (l > r) return;
int pivot = a[l];
int i = l, j = r;
while (i < j) {
while (i < j && a[j] >= pivot) j--;
a[i] = a[j];
while (i < j&& a[i] <= pivot) i++;
a[j] = a[i];
}
a[i] = pivot;
if (i == k) return;
if (i < k) {
quicksort(i+1, r);
}
else {
quicksort(l, i - 1);
}
}
int main()
{
scanf("%d",&n);
k = n / 2;
for (int i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
//quicksort(0,n-1); //One case TLE
//sort(a, a + n);
nth_element(a, a + n / 2, a + n);
int s1 = 0, s2 = 0;
for (int i = 0; i < n; i++)
{
if(i<k) s1+=a[i];
else s2 += a[i];
}
printf("%d %d\n", n & 0x01, s2 - s1);
return 0;
}
#include <cstdio>
#include <algorithm>
using namespace std;
int a[100001],k,n;
void quicksort(int l,int r) {
swap(a[l], a[(l + r) / 2]);
if (l > r) return;
int pivot = a[l];
int i = l, j = r;
while (i < j) {
while (i < j && a[j] >= pivot) j--;
a[i] = a[j];
while (i < j&& a[i] <= pivot) i++;
a[j] = a[i];
}
a[i] = pivot;
if (i == k) return;
if (i < k) {
quicksort(i+1, r);
}
else {
quicksort(l, i - 1);
}
}
int main()
{
scanf("%d",&n);
k = n / 2;
for (int i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
//quicksort(0,n-1); //One case TLE
//sort(a, a + n);
nth_element(a, a + n / 2, a + n);
int s1 = 0, s2 = 0;
for (int i = 0; i < n; i++)
{
if(i<k) s1+=a[i];
else s2 += a[i];
}
printf("%d %d\n", n & 0x01, s2 - s1);
return 0;
}
相关文章推荐
- solution Of Pat 1113. Integer Set Partition (25)
- PAT (Advanced Level) Practise 1113 Integer Set Partition (25)
- 【PAT】【Advanced Level】1113. Integer Set Partition (25)
- PAT 甲级 1113. Integer Set Partition (25)
- PAT (Advanced Level)1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25) PAT 甲级
- PAT (Advanced Level) Practise 1113 Integer Set Partition (25)
- PAT (Advanced Level) 1113. Integer Set Partition (25)
- PAT_A 1113. Integer Set Partition (25)
- PAT - 甲级 - 1113. Integer Set Partition (25) (排序)
- 1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25)[数学逻辑题]
- 1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25)
- PAT A1113 integer set partition (25)
- 1113. Integer Set Partition
- Integer Set Partition (25)解题报告
- 【PAT】1113. Integer Set Partition
- PAT 1063. Set Similarity (25)(set的使用)
- pat 1113. Integer Set Partition