CF problem: (D) Maximum Product Strikes Back

5
4
1 2 -1 2
3
1 1 -2
5
2 0 -2 2 -1
3
-2 -1 -1
3
-1 -2 -2
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的更多相关文章
- [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. ...
- 最大乘积(Maximum Product,UVA 11059)
Problem D - Maximum Product Time Limit: 1 second Given a sequence of integers S = {S1, S2, ..., Sn}, ...
- 【Leetcode_easy】628. Maximum Product of Three Numbers
problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...
- 暴力求解——最大乘积 Maximum Product,UVa 11059
最大乘积 Maximum Product 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/B 解题思路 ...
- UVa 11059 - Maximum Product 最大乘积【暴力】
题目链接:https://vjudge.net/contest/210334#problem/B 题目大意:Given a sequence of integers S = {S1, S2, . . ...
- UVA11059 - Maximum Product
1.题目名称 Maximum Product 2.题目地址 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...
- leetcode 318. Maximum Product of Word Lengths
传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...
- uva 11059 maximum product(水题)——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- Git 修改历史提交信息 commit message
修改最近一条提交的消息 git commit --amend 进入vim模式 按字母 o 或者 insert键 开始修改内容 按 esc 推出编辑,最常用的是输入":q"直接退出, ...
- ZYNQ 驱动初始化套路--UART串口
@ 目录 UART 串口 GPIO之 MIO控制 LED 套路如下: 1.定义硬件信息结构体 namePs_Config *Config; 2.定义驱动信息结构体 namePs name_Ps; 3. ...
- 内网渗透----Linux信息收集整理
一.基础信息收集 1.查看系统类型 cat /etc/issue cat /etc/*-release cat /etc/lsb-release cat /etc/redhat-release 2.内 ...
- MySQL — 数据查询语言
目录 1.基础查询 2.条件查询 3.分组查询 4.排序查询 5.分页查询 6.多表查询 6.1.连接查询 6.1.1.内连接 6.1.2.外连接 6.1.3.自连接 6.1.4.联合查询 6.2.子 ...
- 一个简单的模拟实例说明Task及其调度问题
Task对于.NET的重要性毋庸置疑.通过最近的一些面试经历,发现很多人对与Task及其调度机制,以及线程和线程池之间的关系并没有清晰的认识.本文采用最简单的方式模拟了Task的实现,旨在说明Task ...
- HC-05蓝牙模块使用教程
1.接线 蓝牙模块 与 USB转TTL模块的接线: 5V->5V GND->GND RX->TXD TX->RXD 2.调试 给蓝牙模块上电,手机端下载蓝牙串口助手SPP,手机 ...
- bzoj3926/luoguP3346 [Zjoi2015]诸神眷顾的幻想乡(trie上构建广义后缀自动机)
bzoj3926/luoguP3346 [Zjoi2015]诸神眷顾的幻想乡(trie上构建广义后缀自动机) bzoj Luogu 题解时间 给你个无根trie树(你管这叫trie树?),问你选取一条 ...
- AFO以后的机房游记
2019.8.6~8.8 周老师让我讲插头DP,理所当然地到机房备课(tuifei) dl24来了足足19个人.只可惜lsy没来,我们的phy,ljx去了首师附.看不到神仙打架了[哭] 插头DP这玩意 ...
- Spring cloud config 客户端连接RabbitMQ 报 socket closed
java.net.SocketException: socket closed at java.net.SocketInputStream.socketRead0(Native Method) ...
- spring-boot中的AOP
public class User { private Integer id; private String username; private String note; public User(In ...