硬着头皮做了一下,没做完,明天继续。
今天晚上看的一部电影叫做《消防员》,挺棒的,一直在找能让我感动的电影,这部电影带给我的,好久没有了的感觉、
A. SwapSort
In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another.
Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109), where ai is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once.
In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and swap the same pair of elements multiple times.
If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.
5 5 2 5 1 4
2 0 3 4 2
6 10 20 20 40 60 60
0
2 101 100
1 0 1
这题就是叫你求最少的交换次数,使得该数列变为递增的数列,只要从第一位开始,找最大的数和它交换即可、
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define N 3010
int tt[N];
int cn[N][2];
int main()
{
int n;
while(~scanf("%d",&n))
{
int cnt = 0;
for(int i = 0; i < n; i ++)
{
scanf("%d",&tt[i]);
}
for(int i = 0; i < n; i ++)
{
int minn = tt[i];
int k = i;
for(int j = i + 1; j < n; j ++)
{
if(tt[j] < minn)
{
minn = tt[j];
k = j;
}
}
if(k == i)
continue;
cn[cnt][0] = i;
cn[cnt ++][1] = k;;
swap(tt[i],tt[k]);
}
printf("%d\n", cnt);
for(int i = 0; i < cnt; i ++)
{
printf("%d %d\n", cn[i][0], cn[i][1]);
}
}
}
B. BerSU Ball
The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary! n boys and m girls are already busy rehearsing waltz, minuet, polonaise and quadrille moves.
We know that several boy&girl pairs are going to be invited to the ball. However, the partners' dancing skill in each pair must differ by at most one.
For each boy, we know his dancing skills. Similarly, for each girl we know her dancing skills. Write a code that can determine the largest possible number of pairs that can be formed from n boys and m girls.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of boys. The second line contains sequence a1, a2, ..., an (1 ≤ ai ≤ 100), where ai is the i-th boy's dancing skill.
Similarly, the third line contains an integer m (1 ≤ m ≤ 100) — the number of girls. The fourth line contains sequence b1, b2, ..., bm (1 ≤ bj ≤ 100), where bj is the j-th girl's dancing skill.
Print a single number — the required maximum possible number of pairs.
4 1 4 6 2 5 5 1 5 7 9
3
4 1 2 3 4 4 10 11 12 13
0
5 1 1 1 1 1 3 1 2 3
2
这题就是求出,最多的对数, 每一对必须满足两个数之差要小于等于1,我们可以对这两串数进行排序,再遍历一遍,就可以了、、、
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
int fbs(int a)
{
if(a < 0)
return - a;
else
return a;
}
int a[110];
int b[110];
int vis[110];
int main()
{
int n, m;
while(~scanf("%d",&n))
{
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; i ++)
scanf("%d",&a[i]);
sort(a, a + n);
scanf("%d",&m);
for(int i = 0; i < m; i ++)
scanf("%d",&b[i]);
sort(b, b + m);
int cnt = 0;
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < m; j ++)
{
if(fbs(a[i] - b[j]) <= 1 && !vis[j])
{
cnt ++;
vis[j] = 1;
break;
}
}
}
printf("%d\n", cnt);
}
}
C. Given Length and Sum of Digits...
You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes.
The single line of the input contains a pair of integers m, s (1 ≤ m ≤ 100, 0 ≤ s ≤ 900) — the length and the sum of the digits of the required numbers.
In the output print the pair of the required non-negative integer numbers — first the minimum possible number, then — the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers "-1 -1" (without the quotes).
2 15
69 96
3 0
-1 -1
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n, m;
int digit[110]; // 保存最小值
int tt[110]; // 保存最大值
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(digit, 0, sizeof(digit));
if(n == 1 && m == 0)
{
printf("0 0\n");
continue;
}
if(m == 0 || n * 9 < m) // 这个明显不符合题意
printf("-1 -1\n");
else
{
int tmp = m;
int cnt = 0;
while(tmp >= 1)
{
tmp = tmp /10;
cnt ++;
}
if(cnt <= n) // 突然感觉这个if 有点多余、、
{
for(int i = 0; i < n; i ++) // 其实就是一个贪心的过程
{
if(m == 0)
{
digit[i] = 0;
continue;
}
if(m > 9)
{
digit[i] = 9;
m = m - 9;
}
else
{
digit[i] = m;
m = 0;
}
//printf("dsds %d\n",digit[i]);
}
}
for(int i = 0; i < n; i ++)
{
tt[i] = digit[n - i - 1]; // 最小的倒过来,就是最大的(还要考虑前导0)
}
if(digit[n - 1] == 0) // 如果第一位是0,把它变成1后面的不是0的那位-1即可
{
digit[n - 1] = 1;
for(int i = n - 2; i >= 0; i --)
{
if(digit[i] != 0)
{
digit[i] --;
break;
}
}
}
if(tt[n - 1] == 0) // 同理
{
tt[n - 1] = 1;
for(int i = 0; i < n; i ++)
{
if(tt[i] != 0)
{
tt[i] --;
break;
}
}
}
for(int i = n - 1; i >= 0 ; i --)
printf("%d",digit[i]);
printf(" ");
for(int i = n - 1; i >= 0 ; i --)
printf("%d",tt[i]);
printf("\n");
}
}
}
其它题目, 明天再来、、、