题目大意:定义函数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. 3.QT计算机实战

    mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { c ...

  2. JavaScript 基础 if switch 弹窗 运算符

    脚本语言最重要的几个部分: 数据类型 运算符 控制语句 数组  方法(函数) 一.基础知识 关键字:系统定义 有意义的名字如 background link 等 标识符:自己定 比如class的名字a ...

  3. Codeforces 987A. Infinity Gauntlet(手速题,map存一下输出即可)

    解法: 1.先将对应的字符串存入map. 2.然后将输入的串的second置为空. 3.输出6-n,输出map中的非空串. 代码: #include <bits/stdc++.h> usi ...

  4. Java文件(io)编程——文件字符流的使用

    案例1: 读取一个文件并写入到另一个文件中,char[] 来中转. 首先要在E盘下创建一个文本文档,命名为test.txt,输入一些字符串. public class Demo_5 { public ...

  5. 前端学习之路——scss篇

    一.什么是SASS SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护. 二.安装和使用 Sass依赖于ruby环境,所以装sass之前先 ...

  6. SpringCloud学习笔记(2)----Spring Cloud Netflix之Eureka的使用

    1.  Spring Cloud Netflix Spring Cloud Netflix 是Spring Cloud 的核心子项目,是对Netflix公司一系列开源产品的封装.它为Spring Bo ...

  7. c++string类的简单介绍

    #include "iostream" #include "string" using namespace std; /*@author:浅滩 *family: ...

  8. git远程仓库变更

    查看自己的远程仓库 git remote -v 远程仓库变更 git remote remove origin //移出现有的远程仓库的地址 git remote add origin http:// ...

  9. pycharm修改提示

  10. npm包的上传npm包的步骤,与更新和下载步骤

    官网: ======================================================= 没有账号可以先注册一个,右上角点击“Sign Up",有账号直接点击“ ...