题目大意:定义函数F(n,k)为[1,2,3,..n]中k个元素的子集中最小元素的数学期望。现在给你两个长度相等的数列A,B(A中元素严格大于B中元素),现在要你重新排列A,使得$\sum\limits _{i=1}^m F(A'[i],B[i])$最大。求排完后的A'。

解题思路:首先找规律,得出$F(n,k)=\frac{n+1}{k+1}$。

然后进行贪心,将B[i]中小的元素对应A[i]中大的元素。为什么这样做是对的?随便举四个正整数$x,y,a,b(x<y<a<b)$,比较$\frac{a+1}{x+1}+\frac{b+1}{y+1}$和$\frac{b+1}{x+1}+\frac{a+1}{y+1}$的大小即可得出。

如果没找出规律,则可以观察样例,发现B[i]越小A[i]越大,则也能得出正解。

于是我们把B[i]升序排序,A[i]降序排序,然后把A'[i]按照B[i]原来的位置排列即可。

观察样例发现字典序要最小,那么我们将B[i]的位置作为第二关键字,越大放在越前面,即可保证字典序最小。

时间复杂度$O(n\log_2 n)$。

C++ Code:

#include<algorithm>
#include<cstdio>
#include<functional>
using namespace std;
struct B{
int num,no;
bool operator <(const B& $1)const{
if(num!=$1.num)
return num<$1.num;
return no>$1.no;
}
}b[200005];
int a[200005],n,val[200005];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i)scanf("%d",&a[i]);
for(int i=1;i<=n;++i)scanf("%d",&b[i].num),b[i].no=i;
stable_sort(b+1,b+n+1);
stable_sort(a+1,a+n+1,greater<int>());
for(int i=1;i<=n;++i)
val[b[i].no] = a[i];
for(int i=1;i<=n;++i) printf("%d ",val[i]);
return 0;
}

[Codeforces 841C]Leha and Function的更多相关文章

  1. CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)

    /* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...

  2. 【CodeForces】841C. Leha and Function(Codeforces Round #429 (Div. 2))

    [题意]定义函数F(n,k)为1~n的集合中选择k个数字,其中最小数字的期望. 给定两个数字集A,B,A中任意数字>=B中任意数字,要求重组A使得对于i=1~n,sigma(F(Ai,Bi))最 ...

  3. Codeforces Round #429 (Div. 2/Div. 1) [ A/_. Generous Kefa ] [ B/_. Godsend ] [ C/A. Leha and Function ] [ D/B. Leha and another game about graph ] [ E/C. On the Bench ] [ _/D. Destiny ]

    PROBLEM A/_ - Generous Kefa 题 OvO http://codeforces.com/contest/841/problem/A cf 841a 解 只要不存在某个字母,它的 ...

  4. CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

    思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...

  5. CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26

    /* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f( ...

  6. 【Codeforces Round #429 (Div. 2) C】Leha and Function

    [Link]:http://codeforces.com/contest/841/problem/C [Description] [Solution] 看到最大的和最小的对应,第二大的和第二小的对应. ...

  7. Codeforces 837E. Vasya's Function

    http://codeforces.com/problemset/problem/837/E   题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)) ...

  8. Codeforces 841D Leha and another game about graph - 差分

    Leha plays a computer game, where is on each level is given a connected graph with n vertices and m  ...

  9. Codeforces 837E Vasya's Function - 数论

    Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = 0; f(a, b) = ...

随机推荐

  1. 推荐几个bootstrap 后端UI框架

    转载地址 https://blog.csdn.net/u013816448/article/details/81563051

  2. RocketMQ学习笔记(8)----RocketMQ的Producer API简介

    在RocketMQ中提供了三种发送消息的模式: 1.NormalProducer(普通) 2.OrderProducer(顺序) 3.TransactionProducer(事务) 下面来介绍一下pr ...

  3. ActiveMQ学习笔记(22)----ActiveMQ的优化和使用建议

    1. 什么时候使用ActiveMQ 1. 异步通信 2. 一对多通信 3. 做个系统的集成,同构,异构 4. 作为RPC的替代 5. 多个应用相互解耦 6. 作为事件驱动架构的幕后支撑 7. 为了提高 ...

  4. (2016北京集训十三)【xsy1532】网络战争 - 最小割树+树上倍增+KD树

    题解: 好题!! 这题似乎能上我代码长度记录的前五? 调试时间长度应该也能上前五QAQ 首先题目要求的明显就是最小割,当然在整个森林上求Q次最小割肯定是会GG的,所以我们需要一个能快速求最小割的算法— ...

  5. 开源 PHP 项目找队友、顾问、指导、贡献者等等

    介绍站点还没做,先直接甩代码链接了 https://github.com/litphp/litphp Lit是什么? Lit是我一直在撸的个人框架,按第一次上传代码来说历史 超过4年 了,从还能支持P ...

  6. Hibernate简单的保存操作

    1.这里面我想先说一下session对象的创建,这个是我们操纵数据库的核心对象,因此首先我们应该获取相应的session对象. public static Configuration cfg; pub ...

  7. 平凡主丛上的Yang-Mills理论

    本文是复旦大学由丁青教授的暑期课程“Yang-Mills理论的几何及其应用”所作笔记,会有少许修正. 所需基础: 多元微积分学 微分方程(常微分方程,数学物理方程) 曲线曲面论(初等微分几何) 以下是 ...

  8. 洛谷 P2027 bf

    P2027 bf 题目描述 bf是一种编程语言,全称为BrainFuck,因为题目名称不能太露骨,所以就简写成bf了. 这种语言的运行机制十分简单,只有一个大小为30000的有符号8位整数(范围[-1 ...

  9. 【LeetCode】Merge Intervals 题解 利用Comparator进行排序

    题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...

  10. Oracle 切割字符查询

    Oracle 切割字符查询 select * from view_psbaseinfo where DECODE('410782001125,411100000043', '', NULL, '410 ...