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. Linux中ftp服务器的安装与部署

    一.ftp简介FTP(File Transfer Protocol,文件传输协议) 是 TCP/IP 协议组中的协议之一.FTP协议包括两个组成部分,其一为FTP服务器,其二为FTP客户端.其中FTP ...

  2. Redis系统学习

    准备写一些关于Redis学习的文章的,发现网上有N多资料有人已经做了总结.查看这些Redis资料,按次序浏览这些Redis资料,相信想学习Redis的同学会很快熟悉: 1.Redis学习手册(目录) ...

  3. 接口(interface)与抽象类(abstract class)两者的异同

    首先说明一下,JDK1.8以后接口可以有默认方法和静态方法以及私有方法. 一.概念: 接口(interface):是抽象类的变体,其中所有的方法都是抽象的且不能有方法体,而且只能定义 static f ...

  4. 解释JDBC抽象和DAO模块?

    通过使用JDBC抽象和DAO模块,保证数据库代码的简洁,并能避免数据库资源错误关闭导致的问题,它在各种不同的数据库的错误信息之上,提供了一个统一的异常访问层.它还利用Spring的AOP 模块给Spr ...

  5. web.xml---配置文件概要

    web.xml分发器: case1: springMvc的分发器: 作用:将匹配上的请求交由springMvc处理,路径会继续到达springMvc的处理器映射器 <servlet> &l ...

  6. springboot服务引入外部jar包在windows运行正常,在linux环境上无法加载到引入jar包的类

    一.问题描述 最近开发了一个springboot程序,需要依赖第三方jar包,这个jar包无法直接通过pom远程仓库下载,需要从自己本地引入,于是配置pom文件如下:将本地jar包引入工程,syste ...

  7. SpringDataRedis的Keyspaces设置

    前言 原文:https://docs.spring.io/spring-data/redis/docs/2.3.2.RELEASE/reference/html/#redis.repositories ...

  8. Serlvet 输出中文

    1 response.setHeader("Content-type", "text/html;charset=UTF-8"); 2 response.setC ...

  9. “a==b”和”a.equals(b)”有什么区别?

    如果 a 和 b 都是对象,则 a==b 是比较两个对象的引用,只有当 a 和 b 指 向的是堆中的同一个对象才会返回 true,而 a.equals(b) 是进行逻辑比较,所以 通常需要重写该方法来 ...

  10. char的越界赋值即其原理剖析

    思考: int ch = 'A'; int ch1 = 65; int ch2 = 321; printf("%c %c %c\n", ch, ch1, ch2);的输出结果是什么 ...