Problem - D - Codeforces

Example
input
5
4
1 2 -1 2
3
1 1 -2
5
2 0 -2 2 -1
3
-2 -1 -1
3
-1 -2 -2
output
0 2
3 0
2 0
0 1
1 0

最近赛中敲不出代码, 赛后倒是镇静了, 我也醉了

简述下思路及变量意义:

这里采取从前到尾遍历,由于数据范围不能完全连乘2e5个的2, 所以我们采取计数方法,

num表示绝对值为2的个数, sign表示当前是正负数, min是从上一个0到现在连乘绝对值最小的负数

带有res的4个变量是截止到1~i的最优方案: res是乘积最大的2的个数, resl是连乘的左边界, resr是连乘的右边界, res0是前面最近所在0的下标+1

解释不清了, 看代码吧

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 2e5+10; LL a[N];
int main()
{
int t;
scanf("%d", &t);
while(t --)
{
int n;
scanf("%d", &n); LL sign = 1, num = 0;
LL res = 0, minn = -99, resmin = 1, res0 = 1;
LL resl=n+1, resr = n;
for(int i = 1; i <= n; i ++)
{
scanf("%lld", &a[i]); //以下四行相当于*a[i], sign表示正负, num表示绝对值=2的个数
if(a[i] == -2) num ++, sign = -1*sign;
else if(a[i]==2) num++;
else if(a[i]==-1) sign = -1*sign;
else if(a[i]==0) num=0;

//从头到尾相乘: (数字为0, 下标为res0 - 1) ~ i
if(sign>0 && num*sign > res)
{
res = num*sign; //res 正负表示正负, 大小表示多少个2相乘
resl = res0; //resl:结果左边界
resr = i;//resr:结果右边界
}
//中间一段相乘
if(sign<0 && minn!=-99 && minn-num*sign > res)
{
res = minn-num*sign;
resl = resmin;
resr = i;
}

      //以下是后面特例的初始化
if(a[i]==0)
{
res0 = i+1;//前面最近所在0的下标+1
sign=1;
minn = -99;//绝对值最小的负数的大小
resmin = i+1;//绝对值最小的负数的下标后一位
}
if(sign<0 && num*sign > minn)
minn = num*sign, resmin = i+1;
}
cout << resl-1 << ' '<< n-resr<<'\n';
}
return 0;
}

CF problem: (D) Maximum Product Strikes Back的更多相关文章

  1. [LeetCode&Python] Problem 628. Maximum Product of Three Numbers

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  2. 最大乘积(Maximum Product,UVA 11059)

    Problem D - Maximum Product Time Limit: 1 second Given a sequence of integers S = {S1, S2, ..., Sn}, ...

  3. 【Leetcode_easy】628. Maximum Product of Three Numbers

    problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...

  4. 暴力求解——最大乘积 Maximum Product,UVa 11059

    最大乘积 Maximum Product 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/B 解题思路 ...

  5. UVa 11059 - Maximum Product 最大乘积【暴力】

    题目链接:https://vjudge.net/contest/210334#problem/B 题目大意:Given a sequence of integers S = {S1, S2, . . ...

  6. UVA11059 - Maximum Product

    1.题目名称 Maximum Product 2.题目地址 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...

  7. leetcode 318. Maximum Product of Word Lengths

    传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...

  8. uva 11059 maximum product(水题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK

  9. [LeetCode] Maximum Product Subarray 求最大子数组乘积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

随机推荐

  1. instanceOf与父子类型转换

    instanceOf 只要有父子类关系,sout(A instanceOf B)结果就位ture,反之false 先前定义 class Person ; method calss Father ; m ...

  2. MySQL二进制binlog日志说明以及利用binlog日志恢复数据

    MySQL的binlog日志对于mysql数据库来说是十分重要的.在数据丢失的紧急情况下,我们往往会想到用binlog日志功能进行数据恢复(定时全量备份+binlog日志恢复增量数据部分). 一.关于 ...

  3. Windows 如何打开 .md 文件

    •问题 最近在学习[C++|黑马程序员],对于课堂笔记中的 .md 文档无从下手,要是直接通过 Chrome 打开排版又很乱,且不能正常显示URL中的内容: 这可如何是好??? •解决方案 1. 打开 ...

  4. 22.1.22 并查集和KMP算法

    22.1.22 并查集和KMP算法 1.并查集结构 1)实现: 并查集有多种实现方式,例如向上指的图的方式,数组的方式等等.其根本思想就在于准确记录某个节点的根节点,这个这种记录就能够很快的实现并查集 ...

  5. FiddlerEverywhere注册账号进行激活失效问题

    有关FiddlerEverywhere通过邮件激活账号时,激活链接提示已失效问题:这个链接有进行一个讨论(https://www.telerik.com/forums/unable-to-activa ...

  6. Switch语句的条件只能接受什么类型的值

    switch语句只能针对基本数据类型使用,这些类型包括int.char.枚举.bool等.对于其他类型,则必须使用if语句. 在一个 switch 中可以有任意数量的 case 语句.每个 case ...

  7. 什么是IOC?

    IoC是什么 Ioc-Inversion of Control,即"控制反转",不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传 ...

  8. 攻防世界upload1

    upload1 进入环境就一个上传,我们先上传一个普通的木马文件看看 木马内容 <?php @eval($_POST["cmd"]); ?> 估计是前端校验我们查看源码 ...

  9. (stm32学习总结)—SPI-FLASH 实验

    SPI总线 SPI 简介 SPI 的全称是"Serial Peripheral Interface",意为串行外围接口,是Motorola 首先在其 MC68HCXX 系列处理器上 ...

  10. web app遇到的一些坑及小技能(持续更新...)

    遇到的一些坑 问题:手机端 click 事件会有大约 300ms 的延迟 原因:手机端事件 touchstart --\> touchmove --> touchend or touchc ...