[NOIP 2007] souvenir packet

[NOIP 2007] souvenir packet

topic

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

Total n + 2 rows:

Line 1 comprises a first integer w, the upper limit for the price of each group and the souvenirs.

Conduct a second integer n, the total number of items available to memorabilia G.

3 to n + 2 lines contains a positive integer P i (5≤P i ≤w) corresponding Prices souvenirs.

Output Format

An integer, i.e., a minimum number of packets.

Sample input and output

Input # 1 copy
100
. 9
90
20 is
20 is
30
50
60
70
80
90
Output # 1 replication
6

Description / Tips

50% of the data meet: 1≤n≤15

100% data satisfies: 1≤n≤30000,80≤w≤200

analysis

(Some of these students do not always like, really annoying ....... ha ha joke, I did not have to do not know.)

Topics requirements
(the purchase price according to souvenir packet, but only up each group comprising two memorabilia, and the price of each of souvenirs and can not exceed a given integer.)

First reads, from small to large.
I think so, set a k1 = 1, k2 = n then begin to enumerate the two. Finally k1 == k2 represents the end of the enumeration;

1 so that each k1 + k2; if k1 + k2> given integer w. So that they can not synthesize a group, then let k2 (the big one) a separate group. ans ++, k2-; Continue
2 if k1 + k2 <= a given integer w, can synthesize it represents a group, k1 ++, k-; continued

It must be noted , each time k2--, k1 and k2 to determine the k2-- is not the same number, represent collided , ending the enumeration;
(pretending to be a picture of text: -> Number of Number of Number of Number of k1 k2 count <-
, then k1 k2 is already adjacent to the .k2-- have no further reduction of the energy).

Also note that if after k2- and k1 is not the same number, let k1 ++, then have to determine k2 is not equal to k1, is the case that they all point to the last number , this number can only own one set up.
(Text pretending to be a picture: -> count the number of k1 number number number number K2 <-
, after k2-, k2 came to the middle of that number, then k1 ++ so k1, k2 points to the same number, then the number. a separate group of friends, while the end of the enumeration)

Code

#include<iostream>

using namespace std;
#include<algorithm>

int w,n;
int a[30005];

bool cmp(int x,int y){
	return x > y;
}

int main(){
	cin>>w>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];

	}
	sort(a+1,a+1+n);


	int k1=1,k2=n;
	int ans=0;
	while(k1!=k2){
		
		//大的那个单独一组 
		if(a[k1] + a[k2] >w){
			ans++;
			k2--;
		}else{		//两个合成一组 
			ans++;
			k2--;

			if(k1 == k2)break;      
			else k1++;
			
			//剩单个 
			if(k1 == k2) {
				ans++;
				break;
			}
		}
	}
	return 0;
}
Published 75 original articles · won praise 1 · views 3659

Guess you like

Origin blog.csdn.net/A793488316/article/details/104548203