【 题集 】 CF #277.5 (Div. 2) 更新ing...

来源:互联网 发布:linux select 编辑:程序博客网 时间:2024/06/05 17:29

    硬着头皮做了一下,没做完,明天继续。

    今天晚上看的一部电影叫做《消防员》,挺棒的,一直在找能让我感动的电影,这部电影带给我的,好久没有了的感觉、

   

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 thann.

Input

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 thei-th element of the array. The elements are numerated from 0 ton - 1 from left to right. Some integers may appear in the array more than once.

Output

In the first line print k (0 ≤ k ≤ n) — the number of swaps. Nextk lines must contain the descriptions of thek swaps, one per line. Each swap should be printed as a pair of integersi, j (0 ≤ i, j ≤ n - 1), representing the swap of elementsai andaj. 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 printi = 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.

Sample test(s)
Input
55 2 5 1 4
Output
20 34 2
Input
610 20 20 40 60 60
Output
0
Input
2101 100
Output
10 1

    这题就是叫你求最少的交换次数,使得该数列变为递增的数列,只要从第一位开始,找最大的数和它交换即可、

  

#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>using namespace std;#define N 3010int 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 fromn boys and m girls.

Input

The first line contains an integer n (1 ≤ n ≤ 100) — the number of boys. The second line contains sequencea1, a2, ..., an (1 ≤ ai ≤ 100), where ai is thei-th boy's dancing skill.

Similarly, the third line contains an integer m (1 ≤ m ≤ 100) — the number of girls. The fourth line contains sequenceb1, b2, ..., bm (1 ≤ bj ≤ 100), where bj is thej-th girl's dancing skill.

Output

Print a single number — the required maximum possible number of pairs.

Sample test(s)
Input
41 4 6 255 1 5 7 9
Output
3
Input
41 2 3 4410 11 12 13
Output
0
Input
51 1 1 1 131 2 3
Output
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 integers. Your task is to find the smallest and the largest of the numbers that have lengthm and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes.

Input

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.

Output

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).

Sample test(s)
Input
2 15
Output
69 96
Input
3 0
Output
-1 -1
   额, 这题其实很简单,题意是叫你求 位数为n 的每一位数加起来 等于 m ,当然,有很多种可能了, 然后你要求出最小的, 和最大的,如果不存在,就输出 - 1 -1,第一个样例中, 69 和96 都是两位数, 6 + 9 = 9 + 6 = 15;这题其实只要找一次就可以了,最小的倒序就是最大的(当然,要考虑前导0 ),求最小的数,从个位开始,如果能是9 就最好了,不是的话,就看最大能是几,一直到n - 1 位 。这么简单的题目,我的代码长度2000+ ,我也是被吓到了,被我搞的好复杂、、

 

#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");        }    }}


    其它题目, 明天再来、、、

0 0
原创粉丝点击