Codeforces-429-2-C Leha and Function
来源:互联网 发布:安装软件赚钱联盟 编辑:程序博客网 时间:2024/06/04 19:02
题意:
给出两个串,第一个串称为A,第二个串称为B,两个串的长度相等,求的最大和, F(n, k)表示从1-n,你取出k的数字的子集, F(n, k)就等于你取出的k的数字中的最小值,而题目的要求就是让你重新给A排序,使得这个和值最大。
思路:我们每次从n个数字中取出k个数字时,最优的策略就是取n的前k个数字,这样肯定是最优的,那么给A重新排序的时候,我们可以这样想从n个数字中取出的数字越少和式肯定最大,所以此时我们只需要使得A串中的大的数对应B串中小的数,这样肯定是最大的
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN=200050;struct Dig{ int x; int idx;} d1[MAXN],d2[MAXN];bool cmp1(Dig d1,Dig d2){ return d1.x>d2.x;}bool cmp2(Dig d1,Dig d2){ return d1.x<d2.x;}bool cmp3(Dig d1,Dig d2){ return d1.idx<d2.idx;}int main(){ int n; scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%d",&d1[i].x); d1[i].idx=i; } sort(d1,d1+n,cmp1); for(int i=0; i<n; i++) { scanf("%d",&d2[i].x); d2[i].idx=i; } sort(d2,d2+n,cmp2); for(int i=0; i<n; i++) { d1[i].idx=d2[i].idx; } sort(d1,d1+n,cmp3); for(int i=0; i<n; i++) { printf("%d%c",d1[i].x,i==n-1?'\n':' '); } printf("\n"); return 0;}
阅读全文
0 0
- Codeforces-429-2-C Leha and Function
- codeforces Div.2 #429 B.Godsend C. Leha and Function
- 429c Leha and Function
- codeforces 841C Leha and Function
- Leha and Function CodeForces
- Leha and Function CodeForces
- Codeforces Round #429 (Div. 2):C、&840A、 Leha and Function
- C. Leha and Function
- C. Leha and Function(Round #429 (Div. 2)
- CodeForces 841C (C) Leha and Function 贪心
- codeforces #429(div2)C—Leha and Function(水题)
- Codeforces Round #429 (Div. 2) 840A Leha and Function(贪心)
- 【Codeforces 840 A. Leha and Function】& 构造
- codeforces 840A Leha and Function
- Codeforces 840A Leha and Function
- cf840A Leha and function
- A. Leha and Function
- Codeforces Round #429 (Div. 2) D. Leha and another game about graph
- Javascript函数形参与实参
- Android自定义控件——获取验证码倒计时
- date() strtotime() -1day 运用解题
- python-k均值聚类
- Maven(一)初识Maven
- Codeforces-429-2-C Leha and Function
- 线性代数(一):奇异值分解
- 十分钟学会使用贝塞尔弧线
- JSTL、EL、ONGL、Struts标签的区别与使用
- 读取properties文件工具类
- 第二周项目三-体验复杂度
- 算法
- java 使用类动态加载 实现模块化
- EJB项目启动报"javax.xml.ws.WebServiceException:Cannot find service endpoint target:"