URAL 1901 Space Elevators(贪心)

1901. Space Elevators

Time limit: 1.0 second
Memory limit: 64 MB
Nowadays spaceships are never launched from the Earth's surface. There is a huge spaceport placed in the geostationary orbit and connected to the Earth with carbon nanotube cables. People and cargo are delivered to the orbit by elevators moving along these cables. It turned out that the space elevator is much more comfortable and cheaper than a spaceship.
Tomorrow a group of key employees of the “Akross” corporation will go to the spaceport with a secret mission. The spaceport management has reserved a special double elevator for the group. The Head of “Akross” demanded that at any given time the total importance of staff in the elevator must not exceed some fixed value. Under this condition, even in case of fatal accident the corporation will be able to recover. Employees enter the elevator in turns. The elevator is sent up if two people entered, or if only one person entered and the following person behind him is so significant for the corporation that it is impossible to send them together in one elevator.
The spaceport management wants to know the maximum number of elevator runs required to deliver all employees, so the right amount of oxygen cylinders and charged batteries can be prepared in advance.

Input

The first line contains integers  n and  s that are the amount of employees of “Akross” assigned to the mission, and the maximum total importance of two employees which can go together in the elevator (1 ≤  n ≤ 10 5; 1 ≤  s ≤ 10 9). The second line contains integers  v 1, …,  v n that are the importance of the employees (1 ≤  v i ≤  s).

Output

In the first line output the maximum amount of trips of the elevator. In the second line output the importance of staff in order from the first employee in the line to the last, for which the elevator will do this amount of trips. If there are several possible answers, output any of them.

Sample

input output
6 6
1 2 3 3 4 5
5
2 5 1 3 4 3

AC代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int a[100010],b[100010];
int main(){
	int n,s,i,c,sum,ans,l,r;
	while(~scanf("%d%d",&n,&s)){
		memset(b,0,sizeof(b));
		for(i=0;i<n;i++)
			scanf("%d",&a[i]);
		sort(a,a+n);
		l=0;
		r=n-1;
		c=0;
		while(l<r){
			if(a[l]+a[r]>s){
				b[c++]=a[l++];
				b[c++]=a[r--];
			}
			else{
				b[c++]=a[l++];
				b[c++]=a[l++];
			}
		}
		if(l==r)
			b[c++]=a[l];
		sum=0;
		for(i=0;i<c;i++){
			sum++;
			if(b[i]+b[i+1]<=s)
				i++;
		}
		printf("%d\n%d",sum,b[0]);
		for(i=1;i<n;i++)
			printf(" %d",b[i]);
		printf("\n");
	}
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值