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,一经查实,立即删除!

相关文章

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;正数的原码…

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

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

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…

form表单多文件上传

1.html/jsp主页 <% page language"java" contentType"text/html; charsetUTF-8"pageEncoding"UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"…

JS学习记录(BOM部分)

BOOM部分 Screen <html lang"en"> <head><meta charset"UTF-8"><title>Screen</title> </head> <body></body> <script>console.log(screen.width);console.log(screen.height);console.log(screen…

在linux上安装jdk(转载)

软件环境&#xff1a; 虚拟机&#xff1a;VMware Workstation 10 操作系统&#xff1a;Ubuntu-12.04-desktop-amd64 JAVA版本&#xff1a;jdk-7u55-linux-x64 软件下载地址&#xff1a; JDK&#xff1a;http://www.oracle.com/technetwork/java/javase/downloads/index.html &a…

WebService大讲堂之Axis2(1):用POJO实现0配置的WebService

Axis2是一套崭新的WebService引擎&#xff0c;该版本是对Axis1.x重新设计的产物。Axis2不仅支持SOAP1.1和SOAP1.2&#xff0c;还集成了非常流行的REST WebService&#xff0c;同时还支持Spring、JSON等技术。这些都将在后面的系列教程中讲解。在本文中主要介绍了如何使用Axis2开…

Unity中UGUI之Canvas属性解读版本二

Canvas Render Modes(渲染模式) 1.在screen空间中渲染2.在world空间中渲染 Screen Space-Overlay 在这个渲染模式中&#xff0c;UI元素将在场景的上面。如果场景改变大小或改变分辨率&#xff0c;Canvas将自动改变大小去适配。 Screen Space-Camera 这和Screen Space-Overlay类…

用subline添加单引号和逗号,在sql中使用

subline Sublime编辑器批量在行首或者行尾添加字符串的方法&#xff1a; 1、选中要操作的行&#xff0c;全选就按CtrlA&#xff1b; 2、使用快捷键CtrlShiftL&#xff0c;进入待操作状态&#xff1b; 3、使用左右方向键&#xff08;←或者→&#xff09;&#xff0c;批量操作…

[独库骑行之我们路过沙漠]塔克拉玛干的边缘

塔克拉玛干的边缘 在计划这段旅行时我最憧憬的路段就是横穿塔克拉玛干的沙漠公路&#xff0c;缘于影视里的茫茫大漠上金色的胡杨林的印象。 由于时间关系&#xff0c;我们没有走那条沙漠公路。倒是从沙雅到阿拉尔之间有部分沙漠公路&#xff0c;但完全没有这种诗情画意&#xf…