CF1042C Array Product(贪心,模拟)
题目描述
You are given an array aa consisting of nn integers. You can perform the following operations with it:
- 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;
- 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(贪心,模拟)的更多相关文章
- CF1042C Array Product 分类讨论+贪心
考虑有无负数(负数的个数为奇视作“有”,否则为“无”)和有无零 无负数无零,全部合并即可 无负数有零,那么把零合并起来,删掉零 有负数无零,把最大的负数找出来,删掉,合并剩余的数 有负数有零,把零和最 ...
- CodeForces ---596B--Wilbur and Array(贪心模拟)
Wilbur and Array Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Su ...
- Array Product(模拟)
Array Product http://codeforces.com/problemset/problem/1042/C You are given an array aa consisting o ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- 贪心+模拟 ZOJ 3829 Known Notation
题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...
- CodeForces-721D-Maxim and Array(优先队列,贪心,分类讨论)
链接: https://vjudge.net/problem/CodeForces-721D 题意: Recently Maxim has found an array of n integers, ...
- UVA 10714 Ants 蚂蚁 贪心+模拟 水题
题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
- CodeForces - 730A 贪心+模拟
贪心策略: 1.只有一个最大值,选着第二大的一起参加比赛减分. 2.有奇数个最大值,选择三个进行比赛. 3.偶数个最大值,选择两个进行比赛. 为什么不把最大值全部选择? 因为最多只能选五个,有可能选择 ...
随机推荐
- !function()是干什么的?
叹号后面跟函数!function和加号后面跟函数+function都是跟(function(){})();这个函数是一个意思,都是告诉浏览器自动运行这个匿名函数的,因为!+()这些符号的运算符是最高的 ...
- hdu 2196 叶子节点最长距离(树DP)
http://www.cnblogs.com/kuangbin/archive/2012/08/28/2659915.html 求每个节点到叶子节点的最长距离 需要保存每个节点到叶子节点距离的最大值和 ...
- IO流之缓冲流
缓冲流 Java中提高了一套缓冲流,它的存在,可提高IO流的读写速度 缓冲流,根据流的分类字节缓冲流与字符缓冲流. 字节缓冲流 字节缓冲流根据流的方向,共有2个 l 写入数据到流中,字节缓冲输出流 ...
- Java入门到精通——框架篇之Spring源码分析Spring两大核心类
一.Spring核心类概述. Spring里面有两个最核心的类这是Spring实现最重要的部分. 1.DefaultListableBeanFactory 这个类位于Beans项目下的org.spri ...
- linux定期任务cron
做个给服务器定期检测的python程序,要python跑起来自己检测时间再执行?我想到了用cron服务. 遇到了个问题python没写绝对路径,没有执行,改了绝对路径就好了.其实人家配置文件开头写了个 ...
- 【阿里云产品公测】性能测试服务PTS初体验
作者:阿里云用户装甲兵 第一步,在九大产品免费公测 页面,点击性能测试服务PTS的申请公测按钮: 点击申请公测后,正确填写个人信息,免费申请性能测试服务PTS: 填写完成后,点击申请提交,耐心等待,一 ...
- ArcMap没有工具条和菜单栏的解决方法
问题: 在安装ArcGIS10后,打开竟然没有菜单栏和工具栏,安装10.1又有,卸了重装10还是没有 解决办法: 在ArcMap的状态栏上双击,在弹出的自定义对话框中,在Toolbars工具栏选择Ma ...
- centos apache 腾讯云ssl证书配置
首先向证书机构申请https证书,会得到证书和私钥,这里我以腾讯云证书安装为例(非常简单) 分两步走 1.申请 点击腾讯云控制台->产品模块下的ssl证书管理->点击申请证书(免费的,不要 ...
- Siebel 集成中的“发布-订阅”与“阅读”
将 Siebel 应用程序中存储的数据提供给企业中的其他应用程序时,通常需要遵循以下两种基本模式之一: 发布-订阅 阅读 “发布-订阅”是一种机制,根据该机制,一个系统(发布者)将更改或更新的数据提供 ...
- 【Leetcode】【Easy】Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...