题目描述

You are given an array aa consisting of nn integers. You can perform the following operations with it:

  1. Choose some positions ii and jj ( 1 \le i, j \le n, i \ne j1≤i,j≤n,i≠j ), write the value of a_i \cdot a_jai​⋅aj​ into the jj -th cell and remove the number from the ii -th cell;
  2. Choose some position ii and remove the number from the ii -th cell (this operation can be performed no more than once and at any point of time, not necessarily in the beginning).

The number of elements decreases by one after each operation. However, the indexing of positions stays the same. Deleted numbers can't be used in the later operations.

Your task is to perform exactly n - 1n−1 operations with the array in such a way that the only number that remains in the array is maximum possible. This number can be rather large, so instead of printing it you need to print any sequence of operations which leads to this maximum number. Read the output format to understand what exactly you need to print.

题目大意:

有n个数,组成一个数列,我们可以进行两种操作:

1 l r

表示 r=l∗r,同时删去l

2 w

表示删掉w这个位置的数(只能使用一次)

现在,你可以进行n−1次操作,并要求你在进行n−1次操作后使剩下的最后一个数最大。操作不会改变数字的序号(也就是删除一个数后后面的不会前移)。因为答案可能很大,我们不用输出最终结果,只需输出任意一种方案

思路:

贪心题。。

我们知道一点,任何一个0乘进来都没有什么用

因为任何一个数乘0都是0,一定不会最优

所以0是废物

同时负数呢?

负负得正

所以我们可以留下偶数个负数

如果负数是奇数个,那么很遗憾,为了不让最终结果是负数,必须要舍弃一个

乘积最大,所以要舍弃的,就是绝对值最小的那个

然后我们把要舍弃的先合并再舍弃

为什么呢?

比如说序列长为n,我的序列里全是0

如果你一个一个地删除,你就得删n次

而如果你两两合并呢?

答案是0,的确是最优情况了

Ok,不过细节不少,大家要多多注意

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rii register int i
#define rij register int j
using namespace std;
int n,a[],cnt,bj[],wz,zero[],minx=,bnt;
int main()
{
scanf("%d",&n);
for(rii=;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==)
{
bnt++;
zero[bnt]=i;
bj[i]=;
}
if(a[i]<)
{
cnt++;
if(-*a[i]<minx)
{
minx=-*a[i];
wz=i;
}
}
}
if(cnt%==)
{
bj[wz]=;
bnt++;
zero[bnt]=wz;
}
sort(zero+,zero+bnt+);
int pre=zero[];
for(rii=;i<=bnt;i++)
{
printf("1 %d %d\n",pre,zero[i]);
pre=zero[i];
}
if(bnt!=n&&bnt!=)
{
printf("2 %d\n",pre);
}
for(rii=;i<=n;i++)
{
if(bj[i]!=)
{
pre=i;
break;
}
}
for(rii=pre+;i<=n;i++)
{
if(bj[i]==)
{
printf("1 %d %d\n",pre,i);
pre=i;
}
}
}

CF1042C Array Product(贪心,模拟)的更多相关文章

  1. CF1042C Array Product 分类讨论+贪心

    考虑有无负数(负数的个数为奇视作“有”,否则为“无”)和有无零 无负数无零,全部合并即可 无负数有零,那么把零合并起来,删掉零 有负数无零,把最大的负数找出来,删掉,合并剩余的数 有负数有零,把零和最 ...

  2. CodeForces ---596B--Wilbur and Array(贪心模拟)

    Wilbur and Array Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Su ...

  3. Array Product(模拟)

    Array Product http://codeforces.com/problemset/problem/1042/C You are given an array aa consisting o ...

  4. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

  5. 贪心+模拟 ZOJ 3829 Known Notation

    题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...

  6. CodeForces-721D-Maxim and Array(优先队列,贪心,分类讨论)

    链接: https://vjudge.net/problem/CodeForces-721D 题意: Recently Maxim has found an array of n integers, ...

  7. UVA 10714 Ants 蚂蚁 贪心+模拟 水题

    题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...

  8. CodeForces 797C Minimal string:贪心+模拟

    题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...

  9. CodeForces - 730A 贪心+模拟

    贪心策略: 1.只有一个最大值,选着第二大的一起参加比赛减分. 2.有奇数个最大值,选择三个进行比赛. 3.偶数个最大值,选择两个进行比赛. 为什么不把最大值全部选择? 因为最多只能选五个,有可能选择 ...

随机推荐

  1. 04.Continue,和三元表达式的学习

    立即结束本次循环,判断循环条件,如果成立,则进入下一次循环,否则退出循环. 举例:运动员跑步喝水的例子 比如:我编写代码的时候,上个厕所,回来继续写代码 练习1: namespace _09.练习02 ...

  2. 理解JavaScript作用域

    这是一篇译文,这里贴上译文地址:http://www.zcfy.cc/article/understanding-scope-in-javascript-8213-scotch-4075.html 这 ...

  3. scss-声明变量与引用

    一.变量的声明 使用$符号可以标识一个变量 $bg-color: #FFFFFF; 二.变量的引用: 变量的引用有一个原则,那就是标准css属性值存在的地方,变量就可以存在. 当编译成css文件的时候 ...

  4. ES6新增的math,Number方法

    ES6新增的math,Number方法,下面总结了一些我觉得有用的 Nunber.isInteger()判断是否为整数,需要注意的是1,和1.0都会被认为是整数 console.log(Number. ...

  5. SQL注入和XSS攻击的原理

    8.4 Web跨站脚本攻击 8.4.1  跨站脚本攻击的原理(1) 跨站脚本在英文中称为Cross-Site Scripting,缩写为CSS.但是,由于层叠样式表 (Cascading Style ...

  6. 【Linux】文件操作函数(系统调用函数)

    重点在于学习--思路与方法 举一反三 一.文件描述符 系统分配给文件的数字编号 二.函数学习 P.S.Man命令使用方法 manual 前三个章节 命令:系统调用函数:库函数 man read //r ...

  7. Android性能优化之渲染篇

    下面是渲染篇章的学习笔记,部分内容和前面的性能优化典范有重合,欢迎大家一起学习交流! 1)Why Rendering Performance Matters 现在有不少App为了达到很华丽的视觉效果, ...

  8. 获取select下拉框的value以及文本内容

    select下拉框在项目开发中是经常用到的,特别是在联级菜单方面的应用更为广泛.但是,对于一些初学者来说,如何获取下拉框子节点option的value值和文本内容,还是有一点难度的.其他的就不说了,现 ...

  9. SQL获取本周,上周,本月,上月的开始时间和结束时间

    ),),--本周 ),),--上周 ),),--本月 ),),--上月 ),),--近半年 ),)--近一年 ), ,, ),)--本周开始时间 ), ,, ),)--本周结束时间 ),,, ),)- ...

  10. mysql导入csv格式文件

    今天测试导入csv格式文件,虽然简单但是如果不注意还是会出现错误,而且mysql在某些方面做的确实对新手不是很友好,记录一下:创建一个csv格式文件:[mysql@xxx1 ycrdb]$ more ...