1481. souvenirs packet (water issues)

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 finished in a souvenir between 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

  + N 2 comprising input lines:   
       the first line of each set price limit and souvenirs to include an integer w,.   
       Conduct a second integer n, the total number of items available to souvenirs.   
       3 ~ n + 2 of the lines contains a positive integer pi (5 <= pi <= w), corresponding Prices souvenirs.

Output

   Only output line, comprising an integer, i.e., the minimum number of packets.

Sample Input

100 9 90 20 20 30 50 60 70 80 90 

Sample Output

6 

Hint

50% of the data satisfies:. 1 <= n-<= 15   
100% data satisfying: 1 <= n <= 30000 , 80 <= w <= 200
#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#define ll long long
const int maxn = 5100;

int n, w, p[30000+8], l, r, id[30000+8], sign[30000+8], num;

int main()
{
    scanf("%d", &w);
    scanf("%d", &n);
    num = 0;
    memset(sign, 0, sizeof(sign));
    for(int i = 0; i<n; i++)
        scanf("%d", &p[i]);
    sort(p, p+n, greater<int>());
    int d = n-1;
    for(int i = 0; i<n; i++)id[i] = i;
    for(int i = 0; i<n; i++)
    {
        if(p[i]+p[d] <= w && !sign[i])
        {
            id[i] = d;
            sign[d] = sign[i] = 1;
            d--;
            num++;
        }
        else if(p[i]+p[d] > w && !sign[i])
        {
            sign[i] = 1;
            num++;
        }
    }
    printf("%d\n", num);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/RootVount/p/11248639.html