首页 > 代码库 > URAL - 1901 Space Elevators
URAL - 1901 Space Elevators
题目:
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.
Example
input | output |
---|---|
6 6 1 2 3 3 4 5 |
5 2 5 1 3 4 3 |
思路:先排序,然后贪心的选人获得最大运行次数。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define MP make_pair 6 #define PB push_back 7 typedef long long LL; 8 typedef pair<int,int> PII; 9 const double eps=1e-8; 10 const double pi=acos(-1.0); 11 const int K=1e6+7; 12 const int mod=1e9+7; 13 14 15 int n,mx,v[100005],vis[100005],ans[100005]; 16 int main(void) 17 { 18 scanf("%d%d",&n,&mx); 19 for(int i=1;i<=n;i++) 20 scanf("%d",&v[i]); 21 sort(v+1,v+1+n); 22 int sum=1,l=1,r=n; 23 while(l<r) 24 { 25 while(l<r && v[l]+v[r]<=mx) l++; 26 if(l>=r)break; 27 ans[sum++]=v[l],vis[l++]=1; 28 ans[sum++]=v[r],vis[r--]=1; 29 } 30 for(int i=n;i;i--)if(!vis[i]) 31 ans[sum++]=v[i]; 32 sum=0; 33 for(int i=1;i<=n;i++) 34 if(ans[i]+ans[i+1]>mx)sum++; 35 else sum++,i++; 36 printf("%d\n",sum); 37 for(int i=1;i<=n;i++) 38 printf("%d ",ans[i]); 39 return 0; 40 }
URAL - 1901 Space Elevators
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。