Luo Gu P1094 [NOIP2007] souvenir packet

Topic Portal


Analysis: To make the gift more evenly divided parts as little as possible, and the value of the gift does not exceed the upper limit, it is easy to think of small to large.

Then let the smallest and the largest in a group, if not the smallest and the largest in a group, you can only make a maximum of his own group.

 

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

using namespace std;

int n,a[30005];
int cnt,m;
bool vis[30005];

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    int i=1,j=n;
    while(i<=j)
    {
        if(a[i]+a[j]<=m)
        {
            cnt++;
            i++;
            j--;
        }
        else
        {
            cnt++;
            j--;
        }
    }
    printf("%d\n",cnt);
    return 0;
} #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

int n,a[30005];
int cnt,m;
bool vis[30005];

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    int i=1,j=n;
    while(i<=j)
    {
        if(a[i]+a[j]<=m)
        {
            cnt++;
            i++;
            j--;
        }
        else
        {
            cnt++;
            j--;
        }
    }
    printf("%d\n",cnt);
    return 0;
} 
View Code

 

Guess you like

Origin www.cnblogs.com/Hoyoak/p/11348234.html