ccnu_2016_summer_week1(3)

这套题就是简单的基础题,总共4道题,CF的AB题,各两题。
A. Pasha and Stick

Pasha has a wooden stick of some positive integer length n. He wants to perform exactly three cuts to get four parts of the stick. Each part must have some positive integer length and the sum of these lengths will obviously be n.

Pasha likes rectangles but hates squares, so he wonders, how many ways are there to split a stick into four parts so that it’s possible to form a rectangle using these parts, but is impossible to form a square.

Your task is to help Pasha and count the number of such ways. Two ways to cut the stick are considered distinct if there exists some integer x, such that the number of parts of length x in the first way differ from the number of parts of length x in the second way.
Input

The first line of the input contains a positive integer n (1 ≤ n ≤ 2·109) — the length of Pasha’s stick.
Output

The output should contain a single integer — the number of ways to split Pasha’s stick into four parts of positive integer length so that it’s possible to make a rectangle by connecting the ends of these parts, but is impossible to form a square.
Examples
Input

6

Output

1

Input

20

Output

4

Note

There is only one way to divide the stick in the first sample {1, 1, 2, 2}.

Four ways to divide the stick in the second sample are {1, 1, 9, 9}, {2, 2, 8, 8}, {3, 3, 7, 7} and {4, 4, 6, 6}. Note that {5, 5, 5, 5} doesn’t work.

【题目大意】
给定一个n,拆成4段,有几种拆法可以使这4根木棍组成一个矩形(不算正方形)
【解法】
先判断是奇数还是偶数,如果是奇数肯定不可能组成矩形,因为矩形周长一定是偶数。
如果是偶数的话,先除以二,得到矩形周长的一半L。
如果L是奇数,那么比L小的所有自然数,可以两两凑对。所以是(L-1)/2
如果L是偶数,那么比L小的所有自然数,除了L/2,其余的都可以两两凑对。所以是(L - 2)/2
【AC代码】

#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>

using namespace std;

int main(){
    int n;
    int weight = 0;
    while(scanf("%d", &n) != EOF){
        weight++;
        if(n & 1){
            printf("0\n");
        }
        else{
            n /= 2;
            if(n & 1){
                printf("%d\n", (n - 1) / 2);
            }
            else{
                printf("%d\n", (n - 2) / 2);
            }
        }
    }
    return 0;
}

B. Vika and Squares
Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar contains ai liters of paint of color i.

Vika also has an infinitely long rectangular piece of paper of width 1, consisting of squares of size 1 × 1. Squares are numbered 1, 2, 3 and so on. Vika decided that she will start painting squares one by one from left to right, starting from the square number 1 and some arbitrary color. If the square was painted in color x, then the next square will be painted in color x + 1. In case of x = n, next square is painted in color 1. If there is no more paint of the color Vika wants to use now, then she stops.

Square is always painted in only one color, and it takes exactly 1 liter of paint. Your task is to calculate the maximum number of squares that might be painted, if Vika chooses right color to paint the first square.
Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of jars with colors Vika has.

The second line of the input contains a sequence of integers a1, a2, …, an (1 ≤ ai ≤ 109), where ai is equal to the number of liters of paint in the i-th jar, i.e. the number of liters of color i that Vika has.
Output

The only line of the output should contain a single integer — the maximum number of squares that Vika can paint if she follows the rules described above.
Examples
Input

5
2 4 2 3 3

Output

12

Input

3
5 5 5

Output

15

Input

6
10 10 10 1 10 10

Output

11

Note

In the first sample the best strategy is to start painting using color 4. Then the squares will be painted in the following colors (from left to right): 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5.

In the second sample Vika can start to paint using any color.

In the third sample Vika should start painting using color number 5.

【题目大意】
给定一系列数。从任意一个数开始依次往后数,每数到一次这个位置上的数就减一。如果数到最后一个数,就接着从第一个数开始数。数到某一个位置上的数字是0。
【解法】
先找到最小的数mmin,可以确定的是,至少可以数mmin圈。然后在这n个数中寻找所有最小的数,并计算它们之间的间距。
整圈数加上距离最大的某一段,就是最后的结果。
【AC代码】

#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>

using namespace std;

long long a[400010];
//queue<int>Q;
int main(){
    int N;
//    printf("mmin = %d\n", mmin);
    while(scanf("%d", &N) != EOF){
        long long mmin = 0x3fffffff;
        for(int i = 0; i < N; i++){
            scanf("%d", &a[i]);
            a[N + i] = a[i];
            mmin = min(mmin, a[i]);
        }
//        for(int i = 0; i < 2 * N; i++){
//            printf("%d ", a[i]);
//        }
//        printf("mmin = %d\n", mmin);
        int temp1 = 0;
        int k;
        for(int i = 0; i < N; i++){
            if(a[i] == mmin){
                temp1 = i;
                k = i;
                break;
            }
        }
//        printf("k = %d\n", k);
        int ans = -1;
        for(int i = k + 1; i < 2 * N; i++){
            if(a[i] == mmin){
                if(i < N){
                    ans = max(ans, i - temp1 - 1);
                    temp1 = i;
                }
                else{
                    ans = max(ans, i - temp1 - 1);
                    break;
                }
            }
        }
//        printf("mmin * N = %d, ans = %d\n", mmin * N, ans);
        printf("%I64d\n", mmin * N + ans);
//        while(!Q.empty()){
//            Q.pop();
//        }
    }

    return 0;
}

C.USB Flash Drives
Sean is trying to save a large file to a USB flash drive. He has n USB flash drives with capacities equal to a1, a2, …, an megabytes. The file size is equal to m megabytes.

Find the minimum number of USB flash drives needed to write Sean’s file, if he can split the file between drives.
Input

The first line contains positive integer n (1 ≤ n ≤ 100) — the number of USB flash drives.

The second line contains positive integer m (1 ≤ m ≤ 105) — the size of Sean’s file.

Each of the next n lines contains positive integer ai (1 ≤ ai ≤ 1000) — the sizes of USB flash drives in megabytes.

It is guaranteed that the answer exists, i. e. the sum of all ai is not less than m.
Output

Print the minimum number of USB flash drives to write Sean’s file, if he can split the file between drives.
Examples
Input

3
5
2
1
3

Output

2

Input

3
6
2
3
2

Output

3

Input

2
5
5
10

Output

1

Note

In the first example Sean needs only two USB flash drives — the first and the third.

In the second example Sean needs all three USB flash drives.

In the third example Sean needs only one USB flash drive and he can use any available USB flash drive — the first or the second.

【题目大意】
给定n个U盘。这n个U盘有各自的大小。现在要存一个大小为m的文件,最少需要几个U盘。
【解法】
sort之后。累加判断即可。
【AC代码】

#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>

using namespace std;

int a[110];
int main(){
    int n, m;
    while(scanf("%d", &n) != EOF){
        scanf("%d", &m);
        for(int i = 0; i < n; i++){
            scanf("%d", &a[i]);
        }
        sort(a, a + n);
//        for(int i = 0; i < n; i++){
//            printf("%d ", a[i]);
//        }
//        printf("\n");
        int sum = 0;
        for(int i = n - 1; i >= 0; i--){
            sum += a[i];
            if(sum >= m){
                printf("%d\n", n - i);
                break;
            }
        }
    }


    return 0;
}

D.The Best Gift
Emily’s birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres.

In the bookshop, Jack decides to buy two books of different genres.

Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book.

The books are given by indices of their genres. The genres are numbered from 1 to m.
Input

The first line contains two positive integers n and m (2 ≤ n ≤ 2·105, 2 ≤ m ≤ 10) — the number of books in the bookstore and the number of genres.

The second line contains a sequence a1, a2, …, an, where ai (1 ≤ ai ≤ m) equals the genre of the i-th book.

It is guaranteed that for each genre there is at least one book of that genre.
Output

Print the only integer — the number of ways in which Jack can choose books.

It is guaranteed that the answer doesn’t exceed the value 2·109.
Examples
Input

4 3
2 1 3 1

Output

5

Input

7 4
4 2 3 1 2 4 3

Output

18

Note

The answer to the first test sample equals 5 as Sasha can choose:

the first and second books,
the first and third books,
the first and fourth books,
the second and third books,
the third and fourth books. 

【题目大意】
有n本书,每本书有1个类别。总共m个类别。问有几种不同的组合方式。
【解法】
累加每种类别的书各有多少本。两两相乘即可。
【AC代码】

#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>

using namespace std;

int genre[13];

int main(){
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF){
        memset(genre, 0, sizeof(genre));
        int temp;
        for(int i = 0; i < n; i++){
            scanf("%d", &temp);
            genre[temp]++;
        }
        int ans = 0;
        for(int i = 1; i <= m; i++){
            for(int j = i + 1; j <= m; j++){
//                printf("%d\n", genre[i] * genre[j]);
                ans += genre[i] * genre[j];
            }
        }

        printf("%d\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值