[Codeforces 841C]Leha and Function
题目大意:定义函数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的更多相关文章
- CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)
/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...
- 【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))最 ...
- 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 解 只要不存在某个字母,它的 ...
- CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)
思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...
- CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26
/* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f( ...
- 【Codeforces Round #429 (Div. 2) C】Leha and Function
[Link]:http://codeforces.com/contest/841/problem/C [Description] [Solution] 看到最大的和最小的对应,第二大的和第二小的对应. ...
- 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)) ...
- 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 ...
- 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) = ...
随机推荐
- 27.boost多线程
#define _CRT_SECURE_NO_WARNINGS #include <boost/thread.hpp> #include <iostream> #include ...
- 10.ref regex unordered_set smartpoint
ref引用不可以复制的对象 void print(std::ostream &os, int i) { os << i << endl; } //ref引用不可以复制的 ...
- 《汇编语言(第三版)》cmp指令
# cmp指令 本质 cmp是比较指令,cmp的功能相当于减法. 格式 cmp 操作对象1,操作对象2 功能 计算操作对象1-操作对象2,但并不保存结果,可以根据flag标志寄存器来判断结果. 正向判 ...
- mybatis批量插入、批量更新和批量删除
转载 https://www.jianshu.com/p/041bec8ae6d3
- 解决无法启动“start web server”:
1.提示1080端口被占用: Cmd——Netstat -ano——找到端口号为1080的pid——打开任务管理器——干掉pid 2.inter error:your request was unsu ...
- git clone 和 git pull 代码无响应
记录一下今天 git 拉代码遇到的一些异常情况: 无论是项目目录下 git pull 还是直接 git clone 都不能正常拉代码: 异常情况1 ssh: connect to host gitee ...
- Python2.* object类............
class object: """ The most base type """ def __delattr__(self, name): ...
- 二进制与十进制的转化,bool str int 之间的转化,str的常用操作,
1.基础数据类型总览(7种) int: 整型,用于进行+-*/运算 str: 存储少量的数据;可加str,可乘数字,可切片,可进行9种常用操作 bool: True False 判断真假 list: ...
- js获取当前根目录的方法
function getRootPath_web() { //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp var curWwwP ...
- HDU 1667 The Rotation Game (A*迭代搜索)
题目大意:略 每次选择一个最大深度K,跑IDA* 估价函数H=8-中间8个格里出现次数最多的数的个数x,即把它填满这个数最少需要8-x次操作,如果dep+H>K,就跳出.. 深搜的时候暴力修改, ...