Codeforces Round#196 前三题

A. Puzzles
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to prepare a goodbye present for her n students and give each of them a jigsaw puzzle (which, as wikipedia states, is a tiling puzzle that requires the assembly of numerous small, often oddly shaped, interlocking and tessellating pieces).

The shop assistant told the teacher that there are m puzzles in the shop, but they might differ in difficulty and size. Specifically, the first jigsaw puzzle consists of f1 pieces, the second one consists of f2 pieces and so on.

Ms. Manana doesn't want to upset the children, so she decided that the difference between the numbers of pieces in her presents must be as small as possible. Let A be the number of pieces in the largest puzzle that the teacher buys and B be the number of pieces in the smallest such puzzle. She wants to choose such n puzzles that A - B is minimum possible. Help the teacher and find the least possible value of A - B.

Input

The first line contains space-separated integers n and m (2 ≤ n ≤ m ≤ 50). The second line contains m space-separated integersf1, f2, ..., fm (4 ≤ fi ≤ 1000) — the quantities of pieces in the puzzles sold in the shop.

Output

Print a single integer — the least possible difference the teacher can obtain.

Sample test(s)
input
4 6
10 12 10 7 5 22
output
5
Note

Sample 1. The class has 4 students. The shop sells 6 puzzles. If Ms. Manana buys the first four puzzles consisting of 10, 12, 10 and 7 pieces correspondingly, then the difference between the sizes of the largest and the smallest puzzle will be equal to 5. It is impossible to obtain a smaller difference. Note that the teacher can also buy puzzles 1, 3, 4 and 5 to obtain the difference 5.

                题目大意:很好懂,输入n,m。给你m个数,让你选n个数,让里面的最小值与最大值的差距最小,输出最小差。

          解题思路:先对n个数排序,然后找相隔位置的差最小的。

          题目地址:A. Puzzles


AC代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[1002];

int main()
{
    int n,m,i;
    scanf("%d%d",&n,&m);
    for(i=0;i<m;i++)
        scanf("%d",&a[i]);
    sort(a,a+m);
    int res=1000000;
    for(i=m-1;i>=n-1;i--)
        if(a[i]-a[i-n+1]<res)
           res=a[i]-a[i-n+1];
    cout<<res<<endl;
    return 0;
}


B. Routine Problem
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Manao has a monitor. The screen of the monitor has horizontal to vertical length ratio a:b. Now he is going to watch a movie. The movie's frame has horizontal to vertical length ratio c:d. Manao adjusts the view in such a way that the movie preserves the original frame ratio, but also occupies as much space on the screen as possible and fits within it completely. Thus, he may have to zoom the movie in or out, but Manao will always change the frame proportionally in both dimensions.

Calculate the ratio of empty screen (the part of the screen not occupied by the movie) to the total screen size. Print the answer as an irreducible fraction p / q.

Input

A single line contains four space-separated integers abcd (1 ≤ a, b, c, d ≤ 1000).

Output

Print the answer to the problem as "p/q", where p is a non-negative integer, q is a positive integer and numbers p and q don't have a common divisor larger than 1.

Sample test(s)
input
1 1 3 2
output
1/3
input
4 3 2 2
output
1/4
Note

Sample 1. Manao's monitor has a square screen. The movie has 3:2 horizontal to vertical length ratio. Obviously, the movie occupies most of the screen if the width of the picture coincides with the width of the screen. In this case, only 2/3 of the monitor will project the movie in the horizontal dimension:

Sample 2. This time the monitor's width is 4/3 times larger than its height and the movie's frame is square. In this case, the picture must take up the whole monitor in the vertical dimension and only 3/4 in the horizontal dimension:


           题目大意:给你显示屏长比宽,然后需要放的录像的长比宽。可以任意收缩录像大小,长宽比不能变,放进显示屏里,问最小的空白比例是多少?

           解题思路:主要坑在了输出0的情况,开始没改这个然后以为是长宽可以变,然后就把长宽先处理了下,见代码。然后又WA,看了输出0不能直接输出0,需要输出0/x. 最后被黑了,想不通就直接睡了,当时不应该加if(a<b) a-b交换的情况。 今天想了下,真的不能换,不然你的头也要跟着转方向了。。。

            说下我的思路吧!if(a/b>c/d)这说明d必须和b对齐,然后d就变成了b,c就变成了c*b/d,然后就直接是这个式子(a-c*b/d)/a直接变成(ad-bc)/ad.然后另一种情况也相同的讨论,相同就输出0/1 TAT

            题目地址:B. Routine Problem

AC代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;

int gcd(int m,int n)
{
    int t;
    while(n)
    {
        t=m%n; m=n; n=t;
    }
    return m;
}
int main()
{
    int a,b,c,d;
    while(~scanf("%d%d%d%d",&a,&b,&c,&d))
    {
        /*if(a<b)  //因为多加了这个被黑了
        {
            int tmp=a;
            a=b;
            b=tmp;
        }
        if(c<d)
        {
            int tmp=c;
            c=d;
            d=tmp;
        }*/

        if((double)a/(double)b>(double)c/(double)d)
        {
            int p1=a*d-b*c;
            int p2=a*d;
            int q=gcd(p1,p2);
            p1=p1/q;
            p2=p2/q;
            printf("%d/%d\n",p1,p2);
        }
        else if((double)a/(double)b<(double)c/(double)d)
        {
            int p1=b*c-a*d;
            int p2=b*c;
            int q=gcd(p1,p2);
            p1=p1/q;
            p2=p2/q;
            printf("%d/%d\n",p1,p2);
        }
        else
        {
            printf("0/1\n");  //坑的地方
        }
    }
    return 0;
}

C. Quiz
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.

Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).

Input

The single line contains three space-separated integers nm and k (2 ≤ k ≤ n ≤ 109; 0 ≤ m ≤ n).

Output

Print a single integer — the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).

Sample test(s)
input
5 3 2
output
3
input
5 4 2
output
6
Note

Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.

Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.

Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though2000000020 mod 1000000009 is a smaller number.


            题目大意:给你n个题,答对一个题目加1分,然后计数器加1,如果错一个,计数器就变成了0.连续答对k个题,先加上这个题的1分,然后再乘上2,计数器清零。问给你一个m,k,求最少得到的分。

            解题思路:先把第k,2k,3k..这样的位置全部答错,意思就是没有翻倍的情况。如果m比这样所有的分加起来,就有翻倍的情况了。最后统计翻倍的和会发现是等比数列的和,需要用到快速幂。然后就是再加上后面没有翻倍的,记住%m+m)%m。避免出现负数的情况。

Codeforces Round#196 前三题_第1张图片

           题目地址:C. Quiz

AC代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
#define mo 1000000009
using namespace std;

__int64 fastmi(int p) //快速幂取模
{
    __int64 base=2;
    __int64 ans=1;
    while(p)
    {
        if(p&1)
            ans=(ans*base)%mo;
        base=(base*base)%mo;
        p>>=1;
    }
    return ans;
}

int main()
{
    int n,m,k;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        int score=(n/k)*(k-1)+n%k;  //第k,2k,3k...全部错
        if(m<=score)
            printf("%d\n",m);
        else
        {
            __int64 p=m-score;
            __int64 res=((n/k-p)*(k-1)+n%k)%mo;  //后面的不翻倍情况
            p++;
            __int64 tmp=fastmi(p);
            tmp=(tmp-2)*k%mo;  //前面的翻倍情况
            res=((res+tmp)%mo+mo)%mo;
            printf("%I64d\n",res);
        }
    }
    return 0;
}

十二点cf才开始的,节奏有点乱了。。

       

你可能感兴趣的:(codeforces)