A. Uniform String

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

You are given two integers n and k.

Your task is to construct such a string s of length n that for each i from 1 to k there is at least one i-th letter of the Latin alphabet in this string (the first letter is ‘a’, the second is ‘b’ and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.

You have to answer t independent queries.

Input
The first line of the input contains one integer t (1≤t≤100) — the number of queries.

The next t lines are contain queries, one per line. The i-th line contains two integers ni and ki (1≤ni≤100,1≤ki≤min(ni,26)) — the length of the string in the i-th query and the number of characters in the i-th query.

Output
Print t lines. In the i-th line print the answer to the i-th query: any string si satisfying the conditions in the problem statement with constraints from the i-th query.

Example
input
3
7 3
4 4
6 2

output
cbcacab
abcd
baabab

Note
In the first example query the maximum possible minimal frequency is 2, it can be easily seen that the better answer doesn’t exist. Other examples of correct answers: “cbcabba”, “ccbbaaa” (any permutation of given answers is also correct).

In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is 1).

In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is 3).

思路
很简单,看懂了题意就好,全英文我也没太看懂,凭借着做题经验半猜着就交了一发,直接AC。

我的代码

#include
using namespace std;
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n, k, num, c = 1;
scanf("%d%d", &n, &k);
num = n / k;
char ch = 'a' + (k-1);
for(int i = n; i > 0; i--) {
if(ch == 'a') printf("a");
else {
if(c < num) {
printf("%c", ch);
c++;
}else{
printf("%c", ch);
ch = ch - 1;
c = 1;
}
}
}
printf("\n");
}
return 0;
}

B. Teams Forming

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

There are n students in a university. The number of students is even. The i-th student has programming skill equal to ai.

The coach wants to form n/2 teams. Each team should consist of exactly two students, and each student should belong to exactly one team. Two students can form a team only if their skills are equal (otherwise they cannot understand each other and cannot form a team).

Students can solve problems to increase their skill. One solved problem increases the skill by one.

The coach wants to know the minimum total number of problems students should solve to form exactly n/2 teams (i.e. each pair of students should form a team). Your task is to find this number.

Input
The first line of the input contains one integer n (2≤n≤100) — the number of students. It is guaranteed that n is even.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤100), where ai is the skill of the i-th student.

Output
Print one number — the minimum total number of problems students should solve to form exactly n/2 teams.

Example.1
input

6
5 10 2 3 14 5

output

5

Example.2
input

2
1 100

output

99

Note
In the first example the optimal teams will be: (3,4), (1,6) and (2,5), where numbers in brackets are indices of students. Then, to form the first team the third student should solve 1 problem, to form the second team nobody needs to solve problems and to form the third team the second student should solve 4 problems so the answer is 1+4=5.

In the second example the first student should solve 99 problems to form a team with the second one.

思路
也很简单,大概意思就是说两两组队,组队的两人之间会有差值,要求全部人组完队后差值和最小,求这个差值总和。
很明显要从小到大排序,这样排下来的相邻两个的差值是最小的。但是还要考虑到有相同数值的人的位置,我的想法是用f数组来记录某值的人的位置,遍历整个a数组,如果当前值的f数组显示不为0即f[当前值]记录着另一与其值相等的位置,就将当前位置和另一与其相等值位置的数置为0,同时将f[当前值]置0表示没有了当前值。(特别要注意这里的赋值顺序,最好是先打标记记录要改变的值)最后再将整个a数组sort一下,即是已经去掉了两两同值对的数组了,然后从非0除开始加两两项的差值即可。

我的代码

#include
using namespace std;
int main() {
int n, a[110], sum = 0, f[110] = {0}, k;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if(f[a[i]]) {
int t = a[i];
a[f[a[i]]] = 0;
a[i] = 0;
f[t] = 0;
}else{
f[a[i]] = i;
}
}
sort(a+1, a+n+1);
for(k = 1; k <= n; k++)
if(a[k]) break;
for(int i = k; i <= n; i+=2)
sum += (a[i+1] - a[i]);
printf("%d\n", sum);
return 0;
}