传送门:Problem C

https://www.cnblogs.com/violet-acmer/p/9682082.html

题意:

  给你n个数,定义有两种操作

  ① 1 i j : (i != j) 将a[i]从数列中移除,且a[j] <- a[i]*a[j]

  ② 2 i : 将a[i]从数列中移除,此操作最多使用一次

  注意:将数移除后剩余数的编号并未改变,依旧为初始时的输入顺序

  在经过n-1次操作后使剩余的数最大

题解:

  使用操作②的情况:

  (1) : 数列中含有0

  (2) : 负数个数为奇数个

  n-1个操作的最终结果为负数个数为偶数个,数列中没有0元素(当然除去全是0元素这一情况)

踩坑:

  如果此数列中有负数为奇数个,且含有0元素

  处理方法:

  (1)先将所有0元素通过操作①使其个数变为一个;

  (2)在通过操作①用最大的负数乘以0,去除一个负数两两相乘后对结果贡献最小的负数,使负数个数为偶数个;

  (3)通过②操作去除0元素

AC代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=2e5+; int n;
int a[maxn];
int neg,zero,pos;
int pos0[maxn];
int posNeg;//the position of the max negative number
bool vis[maxn]; void Reader()
{
scanf("%d",&n);
neg=zero=pos=;
posNeg=;
memset(vis,false,sizeof vis);
for(int i=;i <= n;++i)
{
scanf("%d",a+i);
if(a[i] > )
pos++;
else if(a[i] < )
{
neg++;
posNeg=(posNeg == || a[i] > a[posNeg] ? i:posNeg);
}
else
{
zero++;
pos0[zero]=i;
}
}
}
void Process()
{
int ope=;//操作数
if(zero != )//跳出此if语句后 zero == 0 || zero == 1
{
while(zero != )
{
printf("1 %d %d\n",pos0[zero],pos0[zero-]);
vis[pos0[zero]]=true;
zero--;
ope++;
}
}
if(ope == n-)
return ;
if(neg&)//如果负数个数为奇数个,通过去除一个值最大的奇数或乘以0来使奇数个数变为偶数
{
if(zero == )//如果存在0,则让值最大的负数乘以0
{
printf("1 %d %d\n",posNeg,pos0[]);//posNeg与pos0[1]顺序不可颠倒
ope++;
neg--;
if(ope < n-)
{
printf("2 %d\n",pos0[]);
vis[pos0[]]=true;
ope++;
}
}
if(ope == n-)
return ;
if(neg&)//如果 zero == 0,则需要通过删除值最大的负数来使负数个数变为偶数
{
printf("2 %d\n",posNeg);
neg--;
ope++;
}
vis[posNeg]=true;
}
else if(zero == && ope < n-)
{
printf("2 %d\n",pos0[]);
vis[pos0[]]=true;
ope++;
}
if(ope == n-)
return ; int num[maxn];
int index=;
for(int i=;i <= n;++i)
if(vis[i] != true)
num[++index]=i;
for(int i=;i < index;++i)
printf("1 %d %d\n",num[i],num[i+]);
}
int main()
{
Reader();
Process();
}

Codeforces Round #510 (Div. 2)(C)的更多相关文章

  1. Codeforces Round #510 (Div. 2)

    Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 #include<iostream> usi ...

  2. Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)

    http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...

  3. Codeforces Round #510 (Div. 2) B. Vitamins

    B. Vitamins 题目链接:https://codeforces.com/contest/1042/problem/B 题意: 给出几种药,没种可能包含一种或多种(最多三种)维生素,现在问要吃到 ...

  4. Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)

    D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...

  5. Codeforces Round #510 (Div. 2)(B)

    传送门:Problem B https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 如果可以通过喝果汁将维生素A,B,C全部摄取,求最小花费,如 ...

  6. Codeforces Round #510 (Div. 2)(A)

    传送门:Problem A https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 公园里有n个沙滩,a[i]表示第i个沙滩初始人数,现有m个人 ...

  7. codeforces 1042d//Petya and Array// Codeforces Round #510 (Div. 2)

    题意:给出一个数组,求其中和小于t的区间数. 先计算前缀和数组sum[i].对当前的sum[i],查询树状数组中有几个比(sum[i]-t)大的数,那么用sum[i]减它就是一个合法区间.再将当前的s ...

  8. codeforces 1042c// Array Product// Codeforces Round #510(Div. 2)

    题意:给出一个数组,2种操作:.1:x*y然后x消失,2:除掉x(2操作最多只能进行一次).问最大的结果的一种操作方式.逻辑题,看能不能想全面. 1先数好0,正,负的数量,zero,pos,neg.如 ...

  9. Codeforces Round #510 Div. 2 Virtual Participate记

    这场打的顺手到不敢相信.如果不是vp的话估计肯定打不到这个成绩. A:最大显然,最小的话每次暴力给最小的+1. #include<iostream> #include<cstdio& ...

随机推荐

  1. [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!

    测试mysqld启动mysql server的时候,报如下错误: 2015-12-17 00:46:02 10785 [ERROR] Fatal error: Please read "Se ...

  2. apacheTomcat

    Window+R ------>cmd || Window PowerShell apacheTomcat\bin> ./startup.sh

  3. Leetcode——258.各位相加【水题】

    给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所 ...

  4. Distances to Zero CodeForces - 803B (二分)

    题目链接:https://vjudge.net/problem/CodeForces-803B#author=0 题意: 给你一个数组,其中至少包括一个0,求每一个元素距离最近一个0的距离是多少. 样 ...

  5. 基础-Math.floor与parseInt区别

    Math.floor只能对一个数向下取整,不能解析字符串 如: Math.floor(1.5) // 1 Math.floor(-2.1) // -3 Math.floor("3" ...

  6. centos7编译安装zabbix(附带编译安装lnmp)

    先把防火墙和selinux关闭: sytemctl stop firewalld setenforce 0 1.yum安装依赖: yum -y install wget openssl* gcc gc ...

  7. Maven -Maven配置tomcat插件 两种

    Maven Tomcat插件现在主要有两个版本,tomcat-maven-plugin和tomcat7-maven-plugin,使用方式基本相同. tomcat-maven-plugin 插件官网: ...

  8. Export OracleDB Schema To Doc

    叶正盛先生做的mini工具. https://github.com/rgqancy/DBExportDoc 使用中遇到过两个小问题: 1.数据库的表名不能带.(我一个很初级的同事建立的数据库是demo ...

  9. 记Git报错-refusing to merge unrelated histories

    记Git报错-refusing to merge unrelated histories   系统:win7 git版本: 2.16.2.windows.1 问题 1.本地初始化了git仓库,放了一些 ...

  10. [转帖]中国公有云2018H1市场占有率

    IDC:阿里云中国第一 市场份额为2到9名总和   https://news.cnblogs.com/n/617838/ 1 月 21 日,市场研究机构 IDC 日前公布 2018 年上半年中国公有云 ...