CF Round410 D. Mike and distribution

D. Mike and distribution 构造法

798D - Mike and distribution

In the beginning, it's quite easy to notice that the condition " 2·(ap1 + ... + apk) is greater than the sum of all elements in A " is equivalent to " ap1 + ... + apk is greater than the sum of the remaining elements in A ".

Now, let's store an array of indices C with Ci = i and then sort it in decreasing order according to array A, that is we must have ACi ≥ ACi + 1.

Our answer will always have size . First suppose that N is odd. Add the first index to our set, that is make p1 = C1. Now, for the remaining elements, we will consider them consecutively in pairs. Suppose we are at the moment inspecting AC2k and AC2k + 1. If BC2k ≥ BC2k + 1 we make pk + 1 = C2k, else we make pk + 1 = C2k + 1.

Why does this subset work? Well, it satisfies the condition for B because each time for consecutive non-intersecting pairs of elements we select the bigger one, and we also add BC1 to the set, so in the end the sum of the selected elements will be bigger than the sum of the remaining ones.

It also satisfies the condition for A, because Ap1 is equal or greater than the complement element of p2 (that is — the index which we could've selected instead of p2 from the above procedure — if we selected C2k then it would be C2k + 1 and vice-versa). Similarly Ap2 is greater than the complement of p3 and so on. In the end we also add the last element from the last pair and this makes the sum of the chosen subset strictly bigger than the sum of the remaining elements.

The case when N is even can be done exactly the same as when N is odd, we just pick the last remaining index in the end.

The complexity is .

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 7;
int a[N], b[N], c[N], p[N/2];
bool cmp(int i, int j) {return a[i] > a[j];
}
int main()
{//ios::sync_with_stdio(0);int n;while(~scanf("%d", &n)) {for (int i = 1; i <= n; ++i) {scanf("%d", a +i);c[i] = i;}for (int i = 1; i <= n; ++i)scanf("%d", b + i);sort(c + 1, c + 1 + n, cmp);int k = n + 1 >> 1, cur = 0;p[++cur] = c[1];b[n + 1] = 0;//最边界小值 for (int i = 2; i <= n; i += 2)  //cur = k ?p[++cur] = b[c[i]] > b[c[i + 1]] ? c[i] : c[i+1];printf("%d\n%d", cur, p[1]); for (int i = 2; i <= cur; ++i)printf(" %d", p[i]);puts("");}return 0;
}

 

转载于:https://www.cnblogs.com/qinwenjie/p/7271339.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/268762.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

OOAD实践之路——真实案例解析OO理论与实践(二、第一项任务:特性列表)

查看本系列全部文章&#xff1a;《OOA&D实践之路——真实案例解析OO理论与实践》索引贴 第一份说明 当这个项目开始时&#xff0c;我们得到的关于我们要做的系统的唯一说明是一页Word文档&#xff0c;这是一份简单的不能再简单的说明。文档里只有一行字&#xff1a;我…

CSS3常用属性及用法

1.transition: 过渡属性&#xff0c;可以替代flash和javascript的效果 兼容性&#xff1a;Internet Explorer 9 以及更早的版本&#xff0c;不支持 transition 属性。 Chrome 25 以及更早的版本&#xff0c;需要前缀 -webkit-。 Safari 需要前缀 -webkit-。 div { transition: …

ADSL提速 从入门到精通

虽然现在的宽带速率已经很快了&#xff0c;但是大家还是希望在以下方面提升一下应用速度&#xff1a;电影BT下载时、在线影音播放时、FTP文件传送时等。广大网友也因此探寻出不少提升宽带速率的方法&#xff0c;那么&#xff0c;都有哪些简单可行的提速方法呢&#xff1f;它们的…

Swift开发图解入门

《论语卫灵公》有一段经典对白&#xff1a;『子贡问为仁。子曰&#xff1a;工欲善其事&#xff0c;必先利其器。……』。对于一个程序猿来说&#xff0c;好的工具不意味着一定能产生优质的代码。可是好的工具对提升开发效率的作用还是不言而喻的。想要用Swift做iOS开发。唯一可…

java 负数存储结构_负数在java中的存储和读取过程 | 学步园

问题描述&#xff1a;将-5存储在文本文件中&#xff0c;再读取出来显示到控制台;预备知识&#xff1a;1.在java中使用补码处理数字&#xff0c;而且byte(8)的数字在扩展成int(32)类型的时候&#xff0c;正数填充0&#xff0c;负数填充1;2.负数的转变过程&#xff0c;正数的原码…

爆笑:可怜的话剧演员

地点&#xff1a;中央戏剧学院的话剧排练场。   事件&#xff1a;一场文艺剧目的排练。   人物&#xff1a;一男一女。   女&#xff08;羞涩的&#xff09;&#xff1a;我看啊&#xff0c;咱俩的婚事儿&#xff0c;都是你妈逼的。   男&#xff08;憨笑著&#xff0…

获取文本中你须要的字段的 几个命令 grep awk cut tr sed

1,grep 2,awk 3,cut 4,tr 5,sed 实例1 获取本地IP地址 /sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6 | awk {print $2} | tr -d "addr:" 实例2 sed 的使用。去掉某字段 前后的值 grep "select" slow.log | grep "from" | s…

一升的眼泪 日记原文+剧照

作者 木藤亚也14岁——我的家人  “我不能活动&#xff0c;可是我想活着……”   然而&#xff0c;我怎么也想不到&#xff0c;就在我生日这一天&#xff0c;却发生了一场悲剧——玛丽被邻居家养的大狗“老虎”咬破头&#xff0c;死了。玛丽虽然身体小小的&#x…

[补档]暑假集训D5总结

%dalao 今天又有dalao来讲课&#xff0c;讲的是网络流网络流——从入门到放弃&#xff1a;7-29dalao讲课笔记——https://hzoi-mafia.github.io/2017/07/29/27/果然是从入门到放弃啊&#xff0c;dalao本来说好下午继续讲完的&#xff0c;然后——就没有然后了重要的是&#xff…

在多线程中使用UDP

在多线程中使用UDP来自&#xff1a;painboy, 时间&#xff1a;2004-8-8 11:34:40, ID&#xff1a;2754268 type TUdpThread class(TThread) private FData : PChar; //数据区 FBytes : Integer; //数据区大小 FFromIP : string; //UDP的…

主流浏览器Cssjs hack写法

参考&#xff1a; BROWSER HACKS 主流浏览器的Hack写法 转载于:https://www.cnblogs.com/huangtailang/p/7279634.html

java管道流文件的复制_JavaIO 总结笔记三 基本字节字符输入输出流和文件复制...

一、IO体系1.流主要分两大类&#xff1a;字节流 字符流2.在硬盘上的文件&#xff0c;都是以二进制字节形式存储的&#xff0c;所以不管啥文件&#xff0c;读写时都应该用字节流3.在java的早期版本中&#xff0c;的确只有字节流&#xff0c;没有字符流4.一个数字或字母占1个字节…

mysql+phpmyadmin配置流程

mysqlphpmyadmin配置流程&#xff1a;环境&#xff1a;Apachephp5mysql5下载包&#xff1a;phpMyAdmin-2.11.9.4-all-languages-utf-8-only.tar.gz不能下就到这里下载 [url]http://www.phpmyadmin.net/home_page/downloads.php[/url]一&#xff1a;安装配置1> 安装包&#x…

hadoop入门小知识点

注意各个主机之间的通信 文件的复制 scp指令 scp /etc/profile acm03:/etc 所有历史版本&#xff1a; archive.apache.org hdfs://acm01:9000 hdfs是个协议 然后 访问谁 哪个端口 统一资源定位符URL 客户端统统 找nameinode 访问它 启动 datanode hadoop-dae…

java关键字整理_【java基础知识整理】java基础语法之关键字

一、continue与breakcontinue与break基本都使用与循环中&#xff0c;但是continue指结束所有循环的当前循环&#xff0c;而break是指结束整个循环。下面用代码进行简单实例。使用continue结束当前循环this&#xff1a;this关键字只能在方法内部使用&#xff0c;表示对“调用方法…

快速查询DB Lock的方法

DBCC Opentran DBCC inputBuffer(id) KILL (id) sp_lock --找出產生Lock的源頭--Find locked processselect open_tran,* from sys.sysprocesses where blocked>0 --Find suspected processselect * from sys.sysprocesses where statussleeping and waittype0x0000 and ope…