【CodeForces - 798D】Mike and distribution (思维构造,贪心,黑科技)

题干:

Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions.

To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≤ pi ≤ n for 1 ≤ i ≤ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to  because it will be to easy to find sequence P if he allowed you to select too many elements!

Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity!

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the sequences.

On the second line there are n space-separated integers a1, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.

On the third line there are also n space-separated integers b1, ..., bn (1 ≤ bi ≤ 109) — elements of sequence B.

Output

On the first line output an integer k which represents the size of the found subset. k should be less or equal to .

On the next line print k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the elements of sequence P. You can print the numbers in any order you want. Elements in sequence Pshould be distinct.

Example

Input

5
8 7 4 8 3
4 2 5 3 7

Output

3
1 4 5

题目大意:

麦克有两个只包含正整数的数组 A = [a1, a2, ..., an] 和 B = [b1, b2, ..., bn] ,长度都为 n 。

他现在想要选出 k 个数 P = [p1, p2, ..., pk] 满足 1 ≤ pi ≤ n 并且数组 P 中的数字互不相等。要求P数组满足: 2·(ap1 + ... + apk) 比数组 A 的和大,并且 2·(bp1 + ... + bpk) 比数组 B的和大。当然, k 必须小于等于   ,否则 P 数组太容易选出来了。

请你给出一个合法的方案。

解题报告:

     这题显然要贪心,,但是贪心还是有技巧的。。首先我们发现选择的数量当然是越多越好,所以我们肯定要选满n/2+1个数。

     预处理一波,读入的时候a和b捆绑读入,,因为他俩要选就是一起选。。这很套路了不说了。

     首先按照a数组排序,,然后把所有的数字分成两组,,转化问题变成:使得选出的数分别在两个数组中的和,大于剩下没有选的数的和。我们发现这只是个充分条件,,并不是充分必要条件,也就是说条件加强了 ,但是我们发现对于这道题加强这一步条件后恰好可以构造出一组解。

     我们将其两两相邻的数分成一组,每次从这一组中取出一个就行了,所以我们贪心处理只要拿b值较大的那一个即可,因为a肯定是满足递减的也就是不管我们这两个拿哪一个a,都可以干掉下一组的选择的那个a。

    这就很美滋滋了,,仔细观察可以发现,其实对于a数组是跨组贪心的,,也就是我们是这一组取一个值,让他大于下一组的值;对于b数组是本组贪心的,,也就是我们选择的这一个  一定是本组的最优。

    所以对于这种贪心策略,我们只需要再找一个大于第一组的选择的a就行了(因为不然没有人压过他),对于这种特例我们这样处理:排好序后第一个结构体一定是我们选择的,然后从第二个元素开始往后分组,,这样就有一个好处那就是可以保证后面的贪心一定是正确的,并且我们也有一个元素可以压过第一组的a。。所以贪心结束。写代码就完事了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
struct Node {int a,b;int pos;
} node[MAX];
bool cmp (Node a,Node b) {if(a.a != b.a) return a.a > b.a;else return a.b > b.b;
}
int ans[MAX];
int main()
{int n;cin>>n;for(int i = 1; i<=n; i++) scanf("%d",&node[i].a),node[i].pos = i;for(int i = 1; i<=n; i++) scanf("%d",&node[i].b);sort(node+1,node+n+1,cmp);int tot = 0;ans[++tot] = node[1].pos;for(int i = 2; i<=n; i+=2) {if(node[i].b >= node[i+1].b) ans[++tot] = node[i].pos;else ans[++tot] = node[i+1].pos;}//if(n%2==0) ans[++tot] = n;printf("%d\n",tot);for(int i = 1; i<=tot; i++) printf("%d%c",ans[i],i == tot ? '\n' : ' ');return 0 ;}

总结:

  这题思维很巧妙啊。。分成两组,,将问题转化成一个充分条件的问题去进行求解,因为这样方便我们构造贪心。还是要从条件入手啊,,告诉你选n/2个数了,分析一下,,也只能分成两组去考虑 这么做了。

20191005陈题重做

再来介绍一种黑科技:

random_shuffle(R+1,R+n+1);可以在O(n)的时间内获得一个随机排列。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,up;
struct Node {int a,b,id;
} R[MAX];
ll suma,sumb;
bool ok() {ll ta = 0,tb = 0;for(int i = 1; i<=up; i++) {ta += R[i].a;tb += R[i].b;	}if(ta*2>suma && tb*2>sumb) return 1;else return 0;
}
int main()
{cin>>n;for(int i = 1; i<=n; i++) cin>>R[i].a,R[i].id=i,suma += R[i].a;for(int i = 1; i<=n; i++) cin>>R[i].b,sumb += R[i].b;up = n/2+1;printf("%d\n",up);while(1) {random_shuffle(R+1,R+n+1);if(ok()) break;}for(int i = 1; i<=up; i++) printf("%d ",R[i].id);return 0 ;
}

 

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

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

相关文章

erlang odbc mysql参数_Erlang在Redhat 5.3下使用unixODBC连接Oracle数据库的配置

个人在安装配置时遇到一些麻烦&#xff0c;特此记录如下&#xff1a; 环境 数据库服务器操作系统&#xff1a;Windows 2003 数据库&#xff1a;Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 erlang运行的服务器操作系统&#xff1a;Redhat 5.3 erlang:Erlang R1…

【POJ - 2533】Longest Ordered Subsequence(四种方法解决最长上升子序列 含二分优化版本)

题干&#xff1a; Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 41944 Accepted: 18453 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any se…

halcon图片上传到mysql_C# 10个线程并发执行Halcon图像算法 报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”...

如题&#xff0c;这个问题本人已经纠结了快三个工作日了。本人不同WinFrom程序一起动就会开启10个线程&#xff0c;并发对10张图片进行算法处理&#xff0c;问题是只要程序一起动就会报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”异常。本人试过将8个线程停掉…

【HDU - 1025】Constructing Roads In JGShining's Kingdom(dp最长上升子序列模型 + 二分优化)

题干&#xff1a; Constructing Roads In JGShinings Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29933 Accepted Submission(s): 8496 Problem Description JGShinings kingdom consists of 2n…

java list详解_Java 中 list 用法案例详解

LIST是个容器接口可以理解为动态数组&#xff0c;传统数组必须定义好数组的个数才可以使用&#xff0c;而容器对象无须定义好数组下标总数&#xff0c;用add()方法即可添加新的成员对象&#xff0c;他可以添加的仅仅只能为对象&#xff0c;不能添加基本数据类型&#xff0c;容器…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐的组合数+(取模,数学,思维)

题干&#xff1a; 小乐乐得知一周有7天之后就对7产生了兴趣。 小乐乐得到了两堆数字数字时连续的。 第一堆包含[1,n]n个数字&#xff0c;第二堆包含[1,m]m个数字。 小乐乐想要从两堆中各挑选出一个整数x,y&#xff0c;使得x,y的和为7的倍数。 请问小乐乐有多少种组合的方式…

java 写文件 属性吗_使用JAVA读写Properties属性文件

自己定义一个属性文件&#xff1a;例如prop.propertiesbaseFilePathD\:/kuanter/resourcetesxabcd我们要做的第一步就是要将文件读取到Properties类对象中&#xff0c;由于load有一个参数是InputStream&#xff0c;所以我们可以用 InputStream的子类FileInputStream将属性文件读…

【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】 小乐乐算数字(水题,快速幂,lowbit)

题干&#xff1a; 小乐乐最喜欢玩数字了。 小乐乐最近迷上了2这个整数&#xff0c;他觉得2的幂是一种非常可爱的数字。 小乐乐想知道整数x的最大的 2的幂 &#xff08;2^y&#xff09;的因子。 y为整数。 输入描述: 输入整数x。(1<x<1e18) 输出描述: 输出整数x的最…

java date 相差_java 比较时间相差多少分钟

/** * 返回二个时间相差的分分钟数,如果一个为空&#xff0c;返回为0&#xff1b; * param startDate&#xff0c;比如08&#xff1a;09 * param endDate&#xff0c;如18&#xff1a;09 * return */ public static int getMinutesDiff(String startDate,String endDate){ int …

【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】小乐乐吃糖豆 (fIb博弈)

题干&#xff1a; 小乐乐是一个比较喜欢吃糖豆的小孩子&#xff0c;小乐乐的哥哥大乐乐也同样爱吃糖豆。 作为一个小孩子&#xff0c;他们永远觉得谁吃掉了最后一个糖豆&#xff0c;谁吃的糖豆最多。 为了公平起见小乐乐与大乐乐商量吃糖豆的规则如下&#xff1a; 1. …

html5 sse java_html5----sse实现服务端推送数据给前端

案例基于thinkPHP框架&#xff1a;服务端方法&#xff1a;public function ssefun(){ob_implicit_flush();header(Content-Type: text/event-stream);header(Cache-Control: no-cache);$itime();echo retry:1000.PHP_EOL;//每秒执行一次echo "data: The server time is: {…

【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】小乐乐切割方块(思维,水题)

题干&#xff1a; 小乐乐的作业本是2n*2n的方格本。 某天小乐乐的童鞋&#xff0c;想要考验一下小乐乐。 他将小乐乐的一张方格纸中的某个格子(x,y)涂成黑色&#xff0c; 小乐乐能否在将4*4的方格本沿着方格边缘且切割线与黑色方格不存在公共交点的情况下将方格本切割成两…

*【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐打游戏(bfs,双元bfs,思维)

题干&#xff1a; 小乐乐觉得学习太简单了&#xff0c;剩下那么多的时间好无聊&#xff0c;于是便想打游戏。 最近新出了一个特别火的游戏&#xff0c;叫吃猪&#xff0c;小乐乐准备玩一玩。 吃猪游戏很简单&#xff0c;给定一个地图&#xff0c;大小为n*m&…

java map 结构体_java之mapstruct的应用

一、MapStruct是一个代码生成器&#xff0c;简化了不同的Java Bean之间映射的处理&#xff0c;所以映射指的就是从一个实体变化成一个实体。例如我们在实际开发中&#xff0c;DAO层的实体和一些数据传输对象(DTO)&#xff0c;大部分属性都是相同的&#xff0c;只有少部分的不同…

【CodeForces - 340D】Bubble Sort Graph (思维,nlogn最长上升子序列类问题)

题干&#xff1a; Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (lets call it G) i…

java nio有哪些功能_如何真正理解java中的NIO?

从历史进程来看可能会比较好早期计算机性能较差的情况下反正你同时也没法同时处理很多io&#xff0c;不如开一个就阻塞在那边&#xff0c;程序员编程也省事&#xff0c;别的花里胡哨的骚操作还未必有这种方式性能高&#xff0c;这个叫做bio后面cpu内存性能上去了&#xff0c;磁…

【LightOJ - 1031】Easy Game (区间dp,博弈)

题干&#xff1a; You are playing a two player game. Initially there are n integer numbers in an array and player A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot tak…

java web svn_如何搭建svnadmin,一个简单的svnWEB页面

Svn Admin是一个Java开发的管理Svn服务器的项目用户的web应用。安装好Svn服务器端好&#xff0c;把Svn Admin部署好&#xff0c;就可以通过web浏览器管理Svn的项目&#xff0c;管理项目的用户&#xff0c;管理项目的权限。使得管理配置Svn简便&#xff0c;再也不需要每次都到服…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐下象棋(记忆化搜索dp,dfs)

题干&#xff1a; 小乐乐一天天就知道玩&#xff0c;这一天又想玩象棋。 我们都知道马走日。 现在给定一个棋盘&#xff0c;大小是n*m,把棋盘放在第一象限&#xff0c;棋盘的左下角是(0,0),右上角是(n - 1, m - 1); 小乐乐想知道&#xff0c;一个马从左下角(0, 0)开始&#…

java 递归 时间复杂度_递归到底是怎么实现的?它的时间复杂度怎么算?

递归到底是个啥&#xff1f;常听见的一句话就是&#xff1a;自己调用自己。按照这个说法&#xff0c;写个简单的递归自己推导一下的确可以&#xff0c;但是总是有点绕&#xff0c;推着推着自己把自己陷进去了。递归函数运行时&#xff0c;实际上会进行一个压栈(思考栈的特点&am…