每个输入包含一个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N 表示月饼的种类数、以及不超过 500(以万吨为单位)的正整数 D 表示市场最大需求量。随后一行给出 N 个正数表示每种月饼的库存量(以万吨为单位);最后一行给出 N 个正数表示每种月饼的总售价(以亿元为单位)。数字间以空格分隔。
voidquick_sort(int l, int r) { if(l >= r) return; int i = l - 1, j = r + 1; double x = a[(l + r) / 2]; while(i < j) { do i ++; while(a[i] > x); do j --; while(a[j] < x); if(i < j) { swap(a[i], a[j]); swap(v[i], v[j]); swap(w[i], w[j]); } } quick_sort(l, j); quick_sort(j + 1, r); }
intmain() { scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++ ) scanf("%lf", &v[i]); for(int i = 1; i <= n; i ++ ) { scanf("%lf", &w[i]); a[i] = w[i] / v[i]; } quick_sort(1, n);
double res = 0; for(int i = 1; i <= n; i ++ ) { if(m >= v[i]) { res += w[i]; m -= v[i]; } else { res += (w[i] * m / v[i]); m -= v[i]; break; } } printf("%.2lf\n", res);
return0; }
7-25小字辈
本题给定一个庞大家族的家谱,要请你给出最小一辈的名单。
输入格式:
输入在第一行给出家族人口总数 N(不超过 100 000 的正整数) —— 简单起见,我们把家族成员从 1 到 N 编号。随后第二行给出 N 个编号,其中第 i 个编号对应第 i 位成员的父/母。家谱中辈分最高的老祖宗对应的父/母编号为 -1。一行中的数字间以空格分隔。
intmain() { int res = 0; scanf("%d%d%d", &n, &m, &k); for(int i = 1; i <= n; i ++ ) { scanf("%s%d", id[i], &grade[i]); if(grade[i] >= m) res += 50; elseif(grade[i] >= 60) res += 20; }
mysort();
// 处理名次 for(int i = k; i <= n; i ++ ) { if(grade[i] == grade[i + 1]) k ++; else break; } // 答案输出 printf("%d\n", res); if(k > 0) { int t = 1; printf("%d %s %d\n", t, id[t], grade[t]); for(int i = 2; i <= k; i ++ ) { if(grade[i] == grade[t]) { printf("%d %s %d\n", t, id[i], grade[i]); } else { t = i; printf("%d %s %d\n", t, id[i], grade[i]); } } } return0; }
int n, m; // f储存情侣, p储存客人, int f[N], p[N], a[N]; vector<int> dogs;
intmain() { scanf("%d", &n); for(int i =0; i < n; i ++ ) { int a, b; scanf("%d%d", &a, &b); f[a] = b; f[b] = a; } scanf("%d", &m); for(int i = 0; i < m; i ++ ) { int x; scanf("%d", &x); p[i] = x; a[x] ++; a[f[x]] ++; } for(int i = 0; i < m; i ++ ) { if(a[p[i]] != 2) dogs.push_back(p[i]); } sort(dogs.begin(), dogs.end()); // 答案输出 int flag = 0; printf("%d\n", dogs.size()); for(int i = 0; i < dogs.size(); i ++ ) { if(flag) printf(" "); flag = 1; printf("%05d", dogs[i]); } return0; }
输入首先在第一行给出一个正整数 N(1<N≤105),为当地人口数。随后 N 行,每行给出一个人名,格式为:名 姓(带性别后缀),两个字符串均由不超过 20 个小写的英文字母组成。维京人后裔是可以通过姓的后缀判断其性别的,其他人则是在姓的后面加 m 表示男性、f 表示女性。题目保证给出的每个维京家族的起源人都是男性。
随后一行给出正整数 M,为查询数量。随后 M 行,每行给出一对人名,格式为:名1 姓1 名2 姓2。注意:这里的姓是不带后缀的。四个字符串均由不超过 20 个小写的英文字母组成。
15 chris smithm adam smithm bob adamsson jack chrissson bill chrissson mike jacksson steve billsson tim mikesson april mikesdottir eric stevesson tracy timsdottir james ericsson patrick jacksson robin patricksson will robinsson 6 tracy tim james eric will robin tracy tim april mike steve bill bob adam eric steve tracy tim tracy tim x man april mikes
Given a sequence of K integers { N1, N2, …, N**K }. A continuous subsequence is defined to be { N**i, N**i+1, …, N**j } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input Specification:
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.
Output Specification:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
Sample Input:
1 2
10 -10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
1
10 1 4
思路:
暴力枚举出来所有的子序列,并求和。
如果和大于m就更新一下m和右端点。
最后直接输出最大值m和左右端点即可。
注:有一个点就是左端点的更新,每次循环的时候置t = 1,如果从这个 i 开始的子序列的和大于前面的以后更新一下 L ,然后置 t = 0,在这层循环里当再次更新时只需要更新右端点即可。
也可以用动态规划,f[i] 表示以第 i 个数字结尾的子序列的和的最大值。状态转移只有这个数字选和不选,f[i] = max(f[i], a[i]);
intmain() { int flag = 1; scanf("%d", &n); for(int i = 0; i < n; i ++ ) { scanf("%d", &a[i]); if(a[i] >= 0) flag = 0; } int l = 0, r = 0, m = -1e9; int t = 1; for(int i = 0; i < n; i ++ ) { int sum = 0; t = 1; for(int j = i; j < n; j ++ ) { sum += a[j]; if(sum > m) { if(t) { l = i; t = 0; } m = sum; r = j; } } } if(flag) printf("0 %d %d\n", a[0], a[n - 1]); else { printf("%d %d %d\n", m, a[l], a[r]); } return0; }
7-32 链表翻转
题目大意:
给定长度为L 的链表,翻转每K个长度的链表,再输出重排以后的链表。
比如1234567890,K = 4,翻转之后应该为4321876590。
Reversing Linked List
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤105) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.
Then N lines follow, each describes a node in the format:
1
Address Data Next
where Address is the position of the node, Data is an integer, and Next is the position of the next node.
Output Specification:
For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.
intmain() { // 处理输入 scanf("%d%d%d", &add, &n, &k); for(int i = 0; i < n; i ++ ) { int a, b, c; scanf("%d%d%d", &a, &b, &c); p[a].data = b; p[a].next = c; } // 找出原序列 int cnt = 0; while(add != -1) { add1[++ cnt] = add; add = p[add].next; } // 翻转序列 int t = 0, tem = k; n = cnt; // 在链表上的点的数量 cnt = 0; while(tem <= n) { for(int i = tem; i >= t + 1; i -- ) add2[++ cnt] = add1[i]; t = tem; tem += k; } for(int i = t + 1; i <= n; i ++ ) add2[++ cnt] = add1[i]; // 输出答案 int flag = 0; for(int i = 1; i < cnt; i ++ ) { if(flag) printf("\n"); flag = 1; printf("%05d %d %05d", add2[i], p[add2[i]], add2[i + 1]); } if(flag) printf("\n"); printf("%05d %d -1", add2[cnt], p[add2[cnt]]); return0; }
7-33 判断出栈序列
题目大意:
给定一个长度为n的序列按123···n入栈,判断出栈序列是否成立,m是栈的大小,k是询问数量。
Pop Sequence
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, …, N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.
Input Specification:
Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.
Output Specification:
For each pop sequence, print in one line “YES” if it is indeed a possible pop sequence of the stack, or “NO” if not.
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a “-“ will be put at the position. Any pair of children are separated by a space.
Output Specification:
For each test case, print in one line all the leaves’ indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.
intmain() { scanf("%d", &n); getchar(); for(int i = 0; i < n; i ++ ) { T[i].data = i; char xl,xr; scanf("%c %c", &xl, &xr); getchar(); if(xl == '-') T[i].l = -1; else { T[i].l = xl - '0'; p[T[i].l] = 1; } if(xr == '-') T[i].r = -1; else { T[i].r = xr - '0'; p[T[i].r] = 1; } } int root = -1; for(int i = 0; i < n; i ++ ) if(!p[i]) root = i; int flag = 0; q.push(root); while(!q.empty()) { int t = q.front(); q.pop(); if(T[t].l == -1 && T[t].r == -1) { if(flag) printf(" "); flag = 1; printf("%d", t); } if(T[t].l != -1) q.push(T[t].l); if(T[t].r != -1) q.push(T[t].r); } return0; }
7-36 后序遍历
题目大意:
按栈的操作输入弹出遍历一棵树的中序遍历,输出该树的后序遍历。
Tree Traversals Again
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.
Figure 1
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: “Push X” where X is the index of the node being pushed onto the stack; or “Pop” meaning to pop one node from the stack.
Output Specification:
For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
1 2 3 4 5 6 7 8 9 10 11 12 13
6 Push 1 Push 2 Push 3 Pop Pop Push 4 Pop Pop Push 5 Push 6 Pop Pop
We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other?
Input Specification:
Each input file contains one test case. For each test case, the first line contains N (2≤N≤104), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is given in the format:
1
I c1 c2
where I stands for inputting a connection between c1 and c2; or
1
C c1 c2
where C stands for checking if it is possible to transfer files between c1 and c2; or
1
S
where S stands for stopping this case.
Output Specification:
For each C case, print in one line the word “yes” or “no” if it is possible or impossible to transfer files between c1 and c2, respectively. At the end of each case, print in one line “The network is connected.” if there is a path between any pair of computers; or “There are k components.” where k is the number of connected components in this network.
Sample Input 1:
1 2 3 4 5 6 7 8
5 C 3 2 I 3 2 C 1 5 I 4 5 I 2 4 C 3 5 S
Sample Output 1:
1 2 3 4
no no yes There are 2 components.
Sample Input 2:
1 2 3 4 5 6 7 8 9 10
5 C 3 2 I 3 2 C 1 5 I 4 5 I 2 4 C 3 5 I 1 3 C 1 5 S
voiddfs(int x) { for(int i = 0; i < n; i ++ ) { if(!st[i] && g[x][i]) { st[i] = 1; printf(" %d", i); dfs(i); } } } intmain() { scanf("%d%d", &n, &m); for(int i = 0; i < m; i ++ ) { int a, b; scanf("%d%d", &a, &b); g[a][b] = g[b][a] = 1; } // DFS for(int i = 0; i < n; i ++ ) { if(!st[i]) { st[i] = 1; printf("{ %d", i); dfs(i); printf(" }\n"); } } // BFS memset(st, 0, sizeof st); queue<int> q; for(int i = 0; i < n; i ++ ) { if(!st[i]) { q.push(i); st[i] = 1; printf("{"); while(!q.empty()) { int t = q.front(); q.pop(); for(int j = 0; j < n; j ++ ) { if(!st[j] && g[t][j]) { st[j] = 1; q.push(j); } } printf(" %d", t); } printf(" }\n"); } } return0; }
7-42 DFS
题目大意:
有个100 X 100的正方形,中心点是(0,0),右上角是(50,50),中心有个直径为15的圆形岛,在正方形内给出n个可以踩的点,判断是否可以由中心岛踩某些点而到达岸边。给出每次跳跃的距离。
Saving James Bond - Easy Version
This time let us consider the situation in the movie “Live and Let Die” in which James Bond, the world’s most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape – he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head… Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).
Assume that the lake is a 100 by 100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him whether or not he can escape.
Input Specification:
Each input file contains one test case. Each case starts with a line containing two positive integers N (≤100), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the (x,y) location of a crocodile. Note that no two crocodiles are staying at the same position.
Output Specification:
For each test case, print in a line “Yes” if James can escape, or “No” if not.
voidfloyd() { for(int k = 1; k <= n; k ++ ) for(int i = 1; i <= n; i ++ ) for(int j = 1; j <= n; j ++ ) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } intmain() { scanf("%d%d", &n, &m); // 初始化 for(int i = 1; i <= n; i ++ ) for(int j = 1; j <= n; j ++ ) if(i == j) d[i][j] = 0; else d[i][j] = INF; for(int i = 1; i <= m; i ++ ) { int a, b, w; scanf("%d%d%d", &a, &b, &w); d[a][b] = d[b][a] = min(d[a][b], w); } floyd(); // minnum储存最小的点坐标,maxres储存对应的咒语长度 // flag 对应是否存在不可到达的点 int flag = 0; int minnum = 1, maxres = INF; for(int i = 1; i <= n; i ++ ) { int x = -1; for(int j = 1; j <= n; j ++ ) { if(d[i][j] > x) { x = d[i][j]; } } if(x == INF) flag = 1; if(maxres > x) { maxres = x; minnum = i; } } if(flag) printf("0"); else printf("%d %d", minnum, maxres); return0; }
7-44 关键路径
题目大意:
给定一个有向图,第一行n表示点的数量(0 ~ n -1编号)m表示边得数量,然后m行每行给出一条边的起点、终点、权值。求从起点到终点的的最短时间,如果不存在就输出Impossible。
How Long Does It Take
Given the relations of all the activities of a project, you are supposed to find the earliest completion time of the project.
Input Specification:
Each input file contains one test case. Each case starts with a line containing two positive integers N (≤100), the number of activity check points (hence it is assumed that the check points are numbered from 0 to N−1), and M, the number of activities. Then M lines follow, each gives the description of an activity. For the i-th activity, three non-negative numbers are given: S[i], E[i], and L[i], where S[i] is the index of the starting check point, E[i] of the ending check point, and L[i] the lasting time of the activity. The numbers in a line are separated by a space.
Output Specification:
For each test case, if the scheduling is possible, print in a line its earliest completion time; or simply output “Impossible”.
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 integer N (≤100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N 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.
intmain() { scanf("%d", &n); for(int i = 0; i < n; i ++ ) scanf("%d", &a[i]); for(int i = 0; i < n; i ++ ) scanf("%d", &q[i]);
// 判断是否为插入排序 int t, flag = 1; for(int i = 1; i < n; i ++ ) if(q[i] < q[i - 1]) { t = i; break; } for(int i = t; i < n; i ++ ) if(q[i] != a[i]) flag = 0; // 处理下一次迭代 // 插入排序 if(flag) { int k = q[t]; sort(q, q + t + 1); int te = 0; printf("Insertion Sort\n"); for(int i = 0; i < n; i ++ ) { if(te) printf(" "); te = 1; printf("%d", q[i]); } } // 归并排序 else { int len = 2; int k = 1; while(k) { k = 0; for(int i = 0; i < n; i ++ ) if(a[i] != q[i]) { k = 1; break; } for(int i = 0; i < n; i += len) { sort(a + i, a + min(i + len, n)); } len *= 2; } int te = 0; printf("Merge Sort\n"); for(int i = 0; i < n; i ++ ) { if(te) printf(" "); te = 1; printf("%d", a[i]); } } return0; }
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 integer N (≤100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N 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 resulting 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.
voiddown(int u) { int t = u; if(u * 2 <= ans && q[u * 2] > q[t]) t = u * 2; if(u * 2 + 1 <= ans && q[u * 2 + 1] > q[t]) t = u * 2 + 1; if(u != t) { swap(q[u], q[t]); down(t); } } voidup(int u) { while(u / 2) { if(q[u] > q[u / 2]) swap(q[u], q[u / 2]); u /= 2; } } intmain() { scanf("%d", &n); for(int i = 1; i <= n; i ++ ) scanf("%d", &a[i]); for(int i = 1; i <= n; i ++ ) scanf("%d", &q[i]);
// 判断是否为插入排序 int t, flag = 1; for(int i = 2; i <= n; i ++ ) if(q[i] < q[i - 1]) { t = i; break; } for(int i = t; i <= n; i ++ ) if(q[i] != a[i]) flag = 0; // 处理下一次迭代 // 插入排序 if(flag) { int k = q[t]; sort(q + 1, q + t + 1); int te = 0; printf("Insertion Sort\n"); for(int i = 1; i <= n; i ++ ) { if(te) printf(" "); te = 1; printf("%d", q[i]); } } // 堆排序 else { int k = 1; for(int i = n; i >= 1; i -- ) { if(q[i] < q[1]) { k = i; break; } } ans = k - 1; swap(q[1], q[k]); down(1); int te = 0; printf("Heap Sort\n"); for(int i = 1; i <= n; i ++ ) { if(te) printf(" "); te = 1; printf("%d", q[i]); } } return0; }
7-48 PAT Judge
题目大意:
排序题,给定m次提交,最后按总分、满分题目数、id排名输出。
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. 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, N (≤104), the total number of users, K (≤5), the total number of problems, and M (≤105), the total number of submissions. It is then assumed that the user id’s are 5-digit numbers from 00001 to N, and the problem id’s are from 1 to K. The next line contains K positive integers p[i] (i=1, …, K), where p[i] corresponds to the full mark of the i-th problem. Then M lines follow, each gives the information of a submission in the following format:
1
user_id problem_id partial_score_obtained
where partial_score_obtained is either −1 if the submission 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:
1
rank user_id total_score s[1] ... s[K]
where rank is calculated according to the total_score, and all the users with the same total_score obtain the same rank; and s[i] is the partial score obtained for the i-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.
Given any permutation of the numbers {0, 1, 2,…, N−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:
Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers.
Input Specification:
Each input file contains one test case, which gives a positive N (≤105) followed by a permutation sequence of {0, 1, …, N−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.
给定一个n和k,接下来n行,每行是一个人的姓名,年龄,资产,然后给k个询问,每个询问有三个数据,m,l ,r。m是输出数量,和一个年龄范围[ l , r ],输出这个年龄范围内所有人按资产,年龄,姓名排序,输出前m个,如果不足m个,就输出有的,如果这个范围内没有人就输出None。
Forbes magazine publishes every year its list of billionaires based on the annual ranking of the world’s wealthiest people. Now you are supposed to simulate this job, but concentrate only on the people in a certain range of ages. That is, given the net worths of N people, you must find the M richest people in a given range of their ages.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤105) - the total number of people, and K (≤103) - the number of queries. Then N lines follow, each contains the name (string of no more than 8 characters without space), age (integer in (0, 200]), and the net worth (integer in [−106, 106]) of a person. Finally there are K lines of queries, each contains three positive integers: M (≤ 100) - the maximum number of outputs, and [A**min, Amax] which are the range of ages. All the numbers in a line are separated by a space.
Output Specification:
For each query, first print in a line Case #X: where X is the query number starting from 1. Then output the M richest people with their ages in the range [A**min, Amax]. Each person’s information occupies a line, in the format Name Age Net_Worth.
The outputs must be in non-increasing order of the net worths. In case there are equal worths, it must be in non-decreasing order of the ages. If both worths and ages are the same, then the output must be in non-decreasing alphabetical order of the names. It is guaranteed that there is no two persons share all the same of the three pieces of information. In case no one is found, output None.
Case #1: Alice 18 88888 Billy 24 5888 Bob_Volk 24 5888 Dobby 24 5888 Case #2: Joe_Mike 32 3222 Zoe_Bill 35 2333 Williams 30 -22 Case #3: Anny_Cin 95 999999 Michael 5 300000 Alice 18 88888 Cindy 76 76000 Case #4: None
29 Red Alder Ash Aspen Basswood Ash Beech Yellow Birch Ash Cherry Cottonwood Ash Cypress Red Elm Gum Hackberry White Oak Hickory Pecan Hard Maple White Oak Soft Maple Red Oak Red Oak White Oak Poplan Sassafras Sycamore Black Walnut Willow
Ash 13.7931% Aspen 3.4483% Basswood 3.4483% Beech 3.4483% Black Walnut 3.4483% Cherry 3.4483% Cottonwood 3.4483% Cypress 3.4483% Gum 3.4483% Hackberry 3.4483% Hard Maple 3.4483% Hickory 3.4483% Pecan 3.4483% Poplan 3.4483% Red Alder 3.4483% Red Elm 3.4483% Red Oak 6.8966% Sassafras 3.4483% Soft Maple 3.4483% Sycamore 3.4483% White Oak 10.3448% Willow 3.4483% Yellow Birch 3.4483%
voiddfs(int x) { st[x] = 1; for(int i = 1; i <= n; i ++ ) { int t = g[x][i]; if(!st[i] && t == 1) dfs(i); } } intmain() { scanf("%d%d", &n, &m); for(int i = 0; i < m; i ++ ) { int a, b; scanf("%d%d", &a, &b); g[a][b] = g[b][a] = 1; d[a] ++; d[b] ++; } // flag 为 1 表示图中无奇数度的结点 int flag = 1; for(int i = 1; i <= n; i ++ ) if(d[i] % 2) flag = 0; // 判断图是否连通 int ans = 0; for(int i = 1; i <= n; i ++ ) { if(!st[i]) { dfs(i); ans ++; } } if(ans == 1 && flag == 1) printf("1"); else printf("0"); return0; }
boolcmp(int a, int b) { return a > b; } intmain() { scanf("%d", &n); for(int i = 1; i <= n; i ++ ) scanf("%d", &q[i]); scanf("%d", &m); for(int i = 1; i <= m; i ++ ) scanf("%d", &g[i]); sort(q + 1, q + 1 + n, cmp); sort(g + 1, g + 1 + m, cmp); int head = 1, tail1 = n, tail2 = m; int sum = 0; while(q[head] * g[head] > 0) { sum += q[head] * g[head]; head ++; } while(head <= min(tail1, tail2) && q[tail1] * g[tail2] > 0) { sum += q[tail1] * g[tail2]; tail1 --; tail2 --; } printf("%d", sum); return0; }
Given a sequence of positive integers and another positive integer p. The sequence is said to be a “perfect sequence” if M≤m×p where M and m are the maximum and minimum numbers in the sequence, respectively.
Now given a sequence and a parameter p, you are supposed to find from the sequence as many numbers as possible to form a perfect subsequence.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers N and p, where N (≤105) is the number of integers in the sequence, and p (≤109) is the parameter. In the second line there are N positive integers, each is no greater than 109.
Output Specification:
For each test case, print in one line the maximum number of integers that can be chosen to form a perfect subsequence.