### 输入样例:

4 1 2 4 -1
4 7 6 -2 -3


### 输出样例:

43


#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 1000002;
int arr1[MAXN], arr2[MAXN];
int main()
{
int n, m;
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%d", &arr1[i]);
scanf("%d", &m);
for(int i = 0; i < n; ++i)
scanf("%d", &arr2[i]);
sort(arr1, arr1 + n, greater<int>());
sort(arr2, arr2 + n, greater<int>());
int f = 0, e1 = n - 1, e2 = n - 1;
long long ans = 0;
while(f < n && arr1[f] > 0 && arr2[f] > 0)
ans += arr1[f] * arr2[f], f++;
while(e1 >= f && arr1[e1] < 0 && arr2[e2] < 0)
ans += arr1[e1] * arr2[e2], e1--, e2--;
printf("%lld\n", ans);
return 0;
}

### 输入样例:

4 4
51 100 1000
36 110 300
6 14 32
5 18 40
0 1 2 3


### 输出样例:

1:1 1:2 1:3 1:4


#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 225;
struct infi{
int ID;
int g, m, p;
double avgg, avgm;
int r[4];
}national[MAXN];
struct ans{
int r, cal;
}Ans[MAXN];
bool cmp1(struct infi a, struct infi b);
bool cmp2(struct infi a, struct infi b);
bool cmp3(struct infi a, struct infi b);
bool cmp4(struct infi a, struct infi b);
int main()
{
int n, m;
int nation;
scanf("%d%d", &n, &m);
for(int i = 0; i < n; ++i)
{
scanf("%d%d%d",&national[i].g, &national[i].m, &national[i].p);
national[i].avgg = 1.0 * national[i].g / national[i].p;
national[i].avgm = 1.0 * national[i].m / national[i].p;
national[i].ID = i;
}
sort(national, national + n, cmp1);
int tmp = national[0].g, r0 = 2, r = 1;
national[0].r[0] = 1;
for(int i = 1; i < n; ++i)
{
if(tmp != national[i].g) r = r0, tmp = national[i].g;
national[i].r[0] = r, r0++;
}
sort(national, national + n, cmp2);
tmp = national[0].m, r0 = 2, r = 1;
national[0].r[1] = 1;
for(int i = 1; i < n; ++i)
{
if(tmp != national[i].m) r = r0, tmp = national[i].m;
national[i].r[1] = r, r0++;
}
sort(national, national + n, cmp3);
double tmpavg = national[0].avgg;
r0 = 2, r = 1, national[0].r[2] = 1;
for(int i = 1; i < n; ++i)
{
if(tmpavg != national[i].avgg) r = r0, tmpavg = national[i].avgg;
national[i].r[2] = r, r0++;
}
sort(national, national + n, cmp4);
tmpavg = national[0].avgm;
r0 = 2, r = 1, national[0].r[3] = 1;
for(int i = 1; i < n; ++i)
{
if(tmpavg != national[i].avgm) r = r0, tmpavg = national[i].avgm;
national[i].r[3] = r, r0++;
}
for(int i = 0; i < m; ++i)
{
scanf("%d", &nation);
int index = 0, tmpr = MAXN;
for(int j = 0; j < n; ++j)
{
if(national[j].ID == nation)
{
nation = j;
break;
}
}
for(int j = 0; j < 4; ++ j)
if(tmpr > national[nation].r[j]) index = j, tmpr = national[nation].r[j];
Ans[i].r = tmpr;
if(index == 0) Ans[i].cal = 1;
else if(index == 1) Ans[i].cal = 2;
else if(index == 2) Ans[i].cal = 3;
else if(index == 3) Ans[i].cal = 4;
}
for(int i = 0; i < m; ++i)
{
if(i) printf(" ");
printf("%d:%d", Ans[i].r, Ans[i].cal);
}
return 0;
}
bool cmp1(struct infi a, struct infi b)
{
return a.g > b.g;
}
bool cmp2(struct infi a, struct infi b)
{
return a.m > b.m;
}
bool cmp3(struct infi a, struct infi b)
{
return a.avgg > b.avgg;
}
bool cmp4(struct infi a, struct infi b)
{
return a.avgm > b.avgm;
}

2015年胡润研究院的调查显示，截至2014年9月，个人资产在600万元以上高净值人群达290万人。假设给出N个人的个人资产值，请快速找出资产排前M位的大富翁。

### 输入样例:

8 3
8 12 7 3 20 9 5 18


### 输出样例:

20 18 12


#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXN = 1000002;
int arr[MAXN];
void Swap(int &a, int &b);
int main()
{
int n, m;
cin>>n>>m;
m = m < n ? m : n;
for(int i = 0; i < n; ++i)
scanf("%d",&arr[i]);
for(int i = 0; i < m; ++i)
{
for(int j = n - 1; j > i; --j)
{
if(arr[j] > arr[j - 1])
Swap(arr[j], arr[j - 1]);
}
}
for(int i = 0; i < m; ++i)
{
if(i) printf(" ");
printf("%d",arr[i]);
}
return 0;
}
void Swap(int &a, int &b)
{
int tmp = a;
a = b;
b = tmp;
}

PAT排名汇总   (25分)

### 输入样例:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85


### 输出样例:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4


1）首先，在一个考场的按要求排序

2）之后，将一个考场的排序与各个考场合并过的信息排序

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAXN = 30003;
struct infi{
char ID[15];
int plant, score;
int r;
bool operator <(const struct infi p) const{
if(score != p.score)
return score > p.score;
else
return strcmp(ID, p.ID) < 0;
}
}peo[MAXN], tmp[303], tmpMer[MAXN];
int Merge(int l, int m);
int main()
{
int n, m, l = 0;
scanf("%d", &n);
for(int plant = 1; plant <= n; ++plant)
{
scanf("%d", &m);
for(int i = 0; i < m; ++i)
{
scanf("%s %d", tmp[i].ID, &tmp[i].score);
tmp[i].plant = plant;
} //以上读入信息
sort(tmp, tmp + m);
int r0 = 1, r = 1;
int s = tmp[0].score;
for(int i = 0; i < m; ++i)
{
if(tmp[i].score != s) r = r0, s = tmp[i].score;
tmp[i].r = r, r0++;
} //以上是对考场排序
l = Merge(l, m); //此处是合并到本次考试中
}
int r0 = 1, r = 1;
int s = peo[0].score;
printf("%d\n", l);
for(int i = 0; i < l; ++i)
{
if(peo[i].score != s) r = r0, s = peo[i].score;
printf("%s %d %d %d\n", peo[i].ID, r, peo[i].plant, peo[i].r);
r0++;
}
return 0;
}
int Merge(int l, int m)
{
int s1 = 0, s2 = 0, index = 0;
while(s1 < l && s2 < m)
{
if(peo[s1].score > tmp[s2].score)
{
tmpMer[index] = peo[s1];
s1++, index++;
}
else if(peo[s1].score < tmp[s2].score)
{
tmpMer[index] = tmp[s2];
s2++, index++;
}
else
{
if(strcmp(peo[s1].ID, tmp[s2].ID) < 0)
tmpMer[index] = peo[s1], s1++, index++;
else tmpMer[index] = tmp[s2], s2++, index++;
}
}
while(s1 < l)
{
tmpMer[index] = peo[s1];
s1++, index++;
}
while(s2 < m)
{
tmpMer[index] = tmp[s2];
s2++, index++;
}
for(int i = 0; i < l + m; ++i)
{
peo[i] = tmpMer[i];
}
return l + m;
}

PAT Judge   (25分)

The ranklist of PAT is generated from the status list, which shows the scores of the submittions. This time you are supposed to generate the ranklist for PAT.

### Input Specification:

Each input file contains one test case. For each case, the first line contains 3 positive integers,NN (≤104\le 10^4), the total number of users, KK (≤5\le 5), the total number of problems, and MM (≤105\le 10^5), the total number of submittions. It is then assumed that the user id's are 5-digit numbers from 00001 toNN, and the problem id's are from 1 to KK. The next line contains KK positive integers p[i] (i=1, ..., KK), where p[i] corresponds to the full mark of the i-th problem. Then MM lines follow, each gives the information of a submittion in the following format:

user_id problem_id partial_score_obtained


where partial_score_obtained is either −1-1 if the submittion cannot even pass the compiler, or is an integer in the range [0,p[problem_id]]. All the numbers in a line are separated by a space.

### Output Specification:

For each test case, you are supposed to output the ranklist in the following format:

rank user_id total_score s[1] ... s[K]


where rank is calculated according to the total_score, and all the users with the sametotal_score obtain the samerank; and s[i] is the partial score obtained for thei-th problem. If a user has never submitted a solution for a problem, then "-" must be printed at the corresponding position. If a user has submitted several solutions to solve one problem, then the highest score will be counted.

The ranklist must be printed in non-decreasing order of the ranks. For those who have the same rank, users must be sorted in nonincreasing order according to the number of perfectly solved problems. And if there is still a tie, then they must be printed in increasing order of their id's. For those who has never submitted any solution that can pass the compiler, or has never submitted any solution, they must NOT be shown on the ranklist. It is guaranteed that at least one user can be shown on the ranklist.

### Sample Input:

7 4 20
20 25 25 30
00002 2 12
00007 4 17
00005 1 19
00007 2 25
00005 1 20
00002 2 2
00005 1 15
00001 1 18
00004 3 25
00002 2 25
00005 3 22
00006 4 -1
00001 2 18
00002 1 20
00004 1 15
00002 4 18
00001 3 4
00001 4 2
00005 2 -1
00004 2 0


### Sample Output:

1 00002 63 20 25 - 18
2 00005 42 20 0 22 -
2 00007 42 - 25 - 17
2 00001 42 18 18 4 2
5 00004 40 15 0 25 -


#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
const int MAXNP = 10002;
const int MAXNS = 6;
struct infi{
int ID;
int total, solve;
int Pid[MAXNS];
bool Psol[MAXNS], summit;
bool operator <(const struct infi s1) const{
bool order = true;
if(total != s1.total) order = total > s1.total;
else if(solve != s1.solve) order = solve > s1.solve;
else order = ID < s1.ID;
return order;
}
}stu[MAXNP]; //定义每个考生的信息，其中的成员分别存储考号，总分，解题数，每一题得分，每题的提交情况，是否提交
int Score[MAXNS]; //每题的分数
int main( )
{
int n, k, m;
int id, pid, sco, index = 1, rank = 1;
scanf("%d%d%d", &n, &k, &m);
for(int i = 0; i <= n; ++i)
{
stu[i].ID = i;
stu[i].total = stu[i].solve = 0;
stu[i].summit = false;
for(int j = 0; j <= k; ++j)
stu[i].Pid[j] = -2, stu[i].Psol[j] = false;
}
for(int i = 1; i <= k; ++i)
scanf("%d",&Score[i]);
for(int i = 0; i < m; ++i)
{
scanf("%d%d%d", &id, &pid, &sco);
if(stu[id].Pid[pid] < sco) 如果该考生的某题过去分数少于现在得分，则更新（注意题中编译出错的情况）
{
if(sco == Score[pid]) stu[id].solve++; //表示解决此题
if(sco == -1) sco = 0; //编译出错
stu[id].Pid[pid] = sco; //更新分数
if(!stu[id].Psol[pid]) stu[id].Psol[pid] = true; //此题是否提交过
stu[id].summit = true; //该考生本次考试提交过代码
}
}
for(int i = 1; i <= n; ++i) //得到总分
{
for (int j = 1; j <= k; ++j)
if(stu[i].Psol[j])
stu[i].total += stu[i].Pid[j];
}
sort(stu + 1, stu + n + 1);
sco = stu[1].total;
for(int i = 1; i <= n && stu[i].summit; ++i) //输出
{
if(sco != stu[i].total) rank = index;
index++;
printf("%d %05d %d", rank, stu[i].ID, stu[i].total);
for(int j = 1; j <= k; ++j)
if(stu[i].Psol[j]) printf(" %d", stu[i].Pid[j]);
else printf(" -");
printf("\n");
sco = stu[i].total;
}
return 0;
}

Insert or Merge   (25分)

According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

### Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integerNN (≤100\le 100). Then in the next line, NN integers are given as the initial sequence. The last line contains the partially sorted sequence of theNN numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

### Output Specification:

For each test case, print in the first line either "Insertion Sort" or "Merge Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

### Sample Input 1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0


### Sample Output 1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0


### Sample Input 2:

10
3 1 2 8 7 5 9 4 0 6
1 3 2 8 5 7 4 9 0 6


### Sample Output 2:

Merge Sort
1 2 3 8 4 5 7 9 0 6


#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAXN = 102;
int num1[MAXN], num2[MAXN];
bool Merge(int n);
int main()
{
int n;
cin>>n;
for(int i = 0; i < n; ++i)
cin>>num1[i];
for(int i = 0; i < n; ++i)
cin>>num2[i];
if(Merge(n))
{
cout<<"Merge Sort"<<endl;
for(int i = 0; i < n - 1; ++i)
cout<<num1[i]<<' ';
cout<<num1[n-1]<<endl;
}
else
{
cout<<"Insertion Sort"<<endl;
for(int i = 1; i < n; ++i)
{
if(num2[i - 1] > num2[i])
{
int tmp = num2[i];
for(; i >0 && tmp < num2[i - 1]; --i)
num2[i] = num2[i - 1];
num2[i] = tmp;
break;
}
}
for(int i = 0; i < n - 1; ++i)
cout<<num2[i]<<' ';
cout<<num2[n - 1]<<endl;
}
return 0;
}
bool Merge(int n)
{
int i;
bool isMerge = false;
for(int setp = 1; true; ++setp)
{
int len = 1 << setp;
int f = 0, e = len;
if(e > n)
{
sort(num1, num1 + n);
break;
}
while(f < n)
{
sort(num1 + f,num1 + e);
f += len, e += len;
if(e > n) e = n;
}
if(isMerge) break;
for(i = 0; i < n; ++i)
if(num1[i] != num2[i]) break;
if(i >= n) isMerge = true;
}
return isMerge;
}

Insertion or Heap Sort   (25分)

According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

### Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integerNN (≤100\le 100). Then in the next line, NN integers are given as the initial sequence. The last line contains the partially sorted sequence of theNN numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

### Output Specification:

For each test case, print in the first line either "Insertion Sort" or "Heap Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

### Sample Input 1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0


### Sample Output 1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0


### Sample Input 2:

10
3 1 2 8 7 5 9 4 6 0
6 4 5 1 0 3 2 7 8 9


### Sample Output 2:

Heap Sort
5 4 3 1 0 2 6 7 8 9


#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXN = 102;
int num1[MAXN], num2[MAXN];
bool HeapSort(int n);
void Swap(int &a, int &b);
int main()
{
int n;
cin>>n;
for(int i = 1; i <= n; ++i)
cin>>num1[i];
for(int i = 1; i <= n; ++i)
cin>>num2[i];
if(HeapSort(n))
{
cout<<"Heap Sort"<<endl;
for(int i = 1; i < n; ++i)
cout<<num1[i]<<' ';
cout<<num1[n]<<endl;
}
else
{
cout<<"Insertion Sort"<<endl;
for(int i = 2; i <= n; ++i)
{
if(num2[i - 1] > num2[i])
{
int tmp = num2[i];
for(; i >1 && tmp < num2[i - 1]; --i)
num2[i] = num2[i - 1];
num2[i] = tmp;
break;
}
}
for(int i = 1; i < n; ++i)
cout<<num2[i]<<' ';
cout<<num2[n]<<endl;
}
return 0;
}
bool HeapSort(int n)
{
int i;
bool isHeap = false;
for(int index = n >> 1; index > 0; --index)
while(n > 1)
{
Swap(num1[1], num1[n]);
n--;
if(isHeap) break;
for(i = 1; i <= n; ++i)
if(num1[i] != num2[i]) break;
if(i > n) isHeap = true;
}
return isHeap;
}
{
while((index << 1) <= n)
{
int i = index;
if(num1[index << 1] > num1[i])
{
int tmp = num1[index << 1];
index <<= 1;
if(index + 1 <= n && num1[index + 1] > tmp)
index++;
Swap(num1[index], num1[i]);
}
else if((index << 1) + 1 <= n && num1[(index << 1) + 1] > num1[index])
{
Swap(num1[index], num1[(index << 1) + 1]);
index = (index << 1) + 1;
}
else
{
break;
}
}
}
void Swap(int &a, int &b)
{
int tmp = a;
a = b;
b = tmp;
}

Sort with Swap(0, i)   (25分)

Given any permutation of the numbers {0, 1, 2,..., N−1N-1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way:

Swap(0, 1) => {4, 1, 2, 0, 3}
Swap(0, 3) => {4, 1, 2, 3, 0}
Swap(0, 4) => {0, 1, 2, 3, 4}


Now you are asked to find the minimum number of swaps need to sort the given permutation of the firstNN nonnegative integers.

### Input Specification:

Each input file contains one test case, which gives a positive NN (≤105\le 10^5) followed by a permutation sequence of {0, 1, ..., N−1N-1}. All the numbers in a line are separated by a space.

### Output Specification:

For each case, simply print in a line the minimum number of swaps need to sort the given permutation.

### Sample Input:

10
3 5 7 2 6 4 9 0 8 1


### Sample Output:

9


#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXN = 100002;
int num[MAXN], Index[MAXN];
void Swap(int &a, int &b);
int main()
{
int n;
int cnt = 0;
scanf("%d", &n);
for(int i = 0; i < n; ++i)
{
scanf("%d", &num[i]); //数字序列
Index[num[i]] = i; //数字所在序列的下标
}
for(int i = 0;i < n; ++i)
{
while(Index[0] != 0) //下标为零的位置不是零，则交换
{
int index = Index[Index[0]]; //在下标为零的数字的位置
Swap(num[index], num[Index[0]]);//数字和零交换到正确的位置
Index[Index[0]] = Index[0]; //并且更新数字在序列中的位置
Index[0] = index;
cnt++; //交换一次
}
for(; i < n && num[i] == i; ++i); //寻找是否还有使序列无序的元素
if(i < n) //如果有，则与零交换
{
Swap(num[0], num[i]);
Index[num[0]] = 0;
Index[0] = i;
cnt++;
}
}
printf("%d\n",cnt);
return 0;
}
void Swap(int &a, int &b)
{
int tmp = a;
a = b;
b = tmp;
}

#### 浙大PAT 7-05. 魔法优惠券 （解题思路）

2014-04-03 19:04:27

#### 7-06. 奥运排行榜（25）（数学 ZJU_PAT）

2014-11-23 09:07:18

#### 5-3 奥运排行榜 (25分)

2017-08-07 17:05:54

#### 5-40 奥运排行榜 (25分)

2016-08-30 12:08:08

#### 为奥运喝彩

2008-05-07 20:55:00

#### 【在线为奥运健儿加油，喝彩】CSDN网友奥运大联欢(一)

2008-08-06 19:18:00

#### 庆祝奥运

2008-08-08 20:12:00

#### 我的奥运,我的2008

2008-03-24 01:08:00

#### 奥运直播

2008-08-06 09:49:00

#### PAT7-06. 奥运排行榜

2014-06-12 09:51:15