[Luo Gu P1094] souvenir packet

## souvenir packet ##

Title Description

New Year's Day approaching, students will be responsible for Lele souvenir New Year's party of issuance of work. So the students to the party souvenir value obtained is relatively balanced, he wanted to buy souvenirs are grouped according to price, but only up each consisting of two memorabilia and souvenirs in each price can not exceed a given integer. To ensure that all souvenirs finished in the shortest possible time, Lele desired minimum number of packets.

Your task is to write a program to find the minimum number of packets all packets program one of the least number of packets output.

Input format:
the input file contains n + 2 group.in lines:
Line 1 comprises an integer w, the upper limit of souvenirs and the price of each group.
Conduct a second integer n, denotes the total element number memorabilia available to G
of 3 ~ n + 2 lines contains a positive integer Pi (5 <= Pi <= w) corresponding Prices souvenirs.

Output Format:
Output file group.out only one line, comprising an integer, i.e., a minimum number of packets.

Input Sample # 1:
100
. 9
90
20 is
20 is
30
50
60
70
80
90
Output Sample # 1:
6
explained
50% of the data satisfies: 1 <= n <= 15
100% data satisfying: 1 <= n <= 30000,80 <= w <= 200

Very simple, an a comparison sorted, if exceeded w, direct election of the large number, if less than w, you know

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[31000];
int main()
{
    int w,n;
    scanf("%d%d",&w,&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    int x=1,y=n,sum=0;
    while(x<=y)
    {
        if(a[x]+a[y]<=w)sum++,x++,y--;
        else sum++,y--;
    }
    printf("%d\n",sum);
    return 0;
}

Guess you like

Origin www.cnblogs.com/candy067/p/11401996.html