Codeforces Round #382 (Div. 2)B. Urbanization 贪心
題目鏈接
http://codeforces.com/contest/735/problem/B
題面
Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided to ask his help in the question of urbanization. There are n people who plan to move to the cities. The wealth of the i of them is equal to ai. Authorities plan to build two cities, first for n1 people and second for n2 people. Of course, each of n candidates can settle in only one of the cities. Thus, first some subset of candidates of size n1 settle in the first city and then some subset of size n2 is chosen among the remaining candidates and the move to the second city. All other candidates receive an official refuse and go back home.
To make the statistic of local region look better in the eyes of their bosses, local authorities decided to pick subsets of candidates in such a way that the sum of arithmetic mean of wealth of people in each of the cities is as large as possible. Arithmetic mean of wealth in one city is the sum of wealth ai among all its residents divided by the number of them (n1 or n2 depending on the city). The division should be done in real numbers without any rounding.
Please, help authorities find the optimal way to pick residents for two cities.
輸入
The first line of the input contains three integers n, n1 and n2 (1?≤?n,?n1,?n2?≤?100?000, n1?+?n2?≤?n) — the number of candidates who want to move to the cities, the planned number of residents of the first city and the planned number of residents of the second city.
The second line contains n integers a1,?a2,?...,?an (1?≤?ai?≤?100?000), the i-th of them is equal to the wealth of the i-th candidate.
輸出
Print one real value — the maximum possible sum of arithmetic means of wealth of cities' residents. You answer will be considered correct if its absolute or relative error does not exceed 10?-?6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
樣例輸入
2 1 1
1 5
樣例輸出
6.00000000
題意
一共有n個數(shù),第i個數(shù)是a[i],現(xiàn)在你需要選出n1個數(shù)和n2個數(shù),使得那n1個數(shù)的和除以n1加上n2個數(shù)的和除以n2的值最大。
題解
貪心,如果n1>n2,那么交換
然后選擇最大的n1個數(shù)為n1集合,然后次大的n2個數(shù)為n2集合。
代碼
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5+7; int n,n1,n2,nn1,nn2; double a[maxn]; int main() {scanf("%d%d%d",&n,&n1,&n2);if(n1>n2)swap(n1,n2);nn1=n1,nn2=n2;for(int i=0;i<n;i++)scanf("%lf",&a[i]);sort(a,a+n);reverse(a,a+n);double sum1=0,sum2=0;for(int i=0;i<n;i++){if(n1){sum1+=a[i];n1--;}else if(n2){sum2+=a[i];n2--;}}double ans = (sum1/nn1)+(sum2/nn2);printf("%.12f\n",ans); }總結(jié)
以上是生活随笔為你收集整理的Codeforces Round #382 (Div. 2)B. Urbanization 贪心的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谷歌宣布在量子计算机纠错技术取得重要突破
- 下一篇: 涉嫌逃税64亿元 意大利检方立案调查美国