Los greedy algorithm topic souvenir packet Valley P1094
topic:
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.
Resolution:
This question
And P1090
look alike
But one is
Minimum
Is a minimum number of additions
There is a certain limit
So different
Here is the analysis:
This question
First, the first sort
With minimal
And the largest ratio
A ratio of a down
Find the words
Since count on a pile
Otherwise put
That number removed
Very simple
code show as below:
#include<bits/stdc++.h>
#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
int n,a[30001],m,d,sum=0;
int main()
{
freopen("P1094_6.in","r",stdin);
freopen("P1094_6.out","w",stdout);
bool flag=true,flag2[30001];
memset(flag2,0,sizeof(flag2));
cin>>m;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
flag=true;
for(int j=n;j>i;j--)
{
if(a[i]+a[j]<=m&&flag2[i]==0&&flag2[j]==0)
{
flag2[i]=1;
flag2[j]=1;
//cout<<a[i]<<" "<<a[j]<<endl;
flag=false;
d=j;
break;
}
}
if(flag==false)
{
for(int j=d;j<=n-1;j++)
a[j]=a[j+1];
n--;
}
//else cout<<a[i]<<endl;;
sum++;
}
cout<<sum<<endl;
return 0;
}