首页 > 其他分享 >Codeforces 1660 D

Codeforces 1660 D

时间:2022-10-05 18:34:47浏览次数:74  
标签:nl int Codeforces 传送门 ans nr 1660 pre2

我是蒟蒻!
我是蒟蒻!
我是蒟蒻!
重要的事情说三遍

传送门

传送门点这儿
$ \color{white}{哈哈哈!你被骗了!} $

$ \color{white}{真传送门在上面的感叹号!} $

思路

嗯?一片空白?


最重要的地方:$ a_i \in \{ -2, -1, 0, 1, 2 \} $ !!
首先为了尽可能大我们肯定不可能有 0 。
所以用双指针的方法处理出 $ [l, r] $ ,$ a_{l}a_{l + 1}a_{l + 2} \cdots a_{r - 1}a_{r} $ 里面不能有 $ 0 $ 。
其次大小从哪里来?$ -2, 2 $ !
所以我们的乘积就可以变为 $ 2^n $ 或 $ -(2^n) $ ,后面处理 max product 就不会爆了。
最后,如果乘积是负数怎么办?
截取第一个和最后一个范围内的负数,把其中一个弄没就行了。


细节看代码。

代码

#include <bits/stdc++.h>
using namespace std;

int a[200005], pre1[200005], pre2[200005];;

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		memset(pre1, 0, sizeof pre1);
		memset(pre2, 0, sizeof pre2);
		int n;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
		}
		for (int i = 1; i <= n; i++) {
			pre1[i] = pre1[i - 1] + (a[i] < 0);
			pre2[i] = pre2[i - 1] + (abs(a[i]) == 2);
		}
		int ans = 0, ans_l = 1, ans_r = 0;
		int l = 1, r = 0;
		while (l <= n) {
			while (l <= n && a[l] == 0) {
				l++;
			}
			if (l > n) {
				break;
			}
			r = l;
			while (r <= n && a[r] != 0) {
				r++;
			}
			r--;
			if ((pre1[r] - pre1[l - 1]) & 1) {
				int nl = l, nr = r;
				while (nl <= n && a[nl] > 0) {
					nl++;
				}
				nl++;
				while (nr >= 1 && a[nr] > 0) {
					nr--;
				}
				nr--;
				if (pre2[r] - pre2[nl - 1] > ans) {
					ans = pre2[r] - pre2[nl - 1];
					ans_l = nl;
					ans_r = r;
				}
				if (pre2[nr] - pre2[l - 1] > ans) {
					ans = pre2[nr] - pre2[l - 1];
					ans_l = l;
					ans_r = nr;
				}
			} else {
				if (pre2[r] - pre2[l - 1] > ans) {
					ans = pre2[r] - pre2[l - 1];
					ans_l = l;
					ans_r = r;
				}
			}
			l = r + 1;
		}
		printf("%d %d\n", ans_l - 1, n - ans_r);
	}
}

标签:nl,int,Codeforces,传送门,ans,nr,1660,pre2
From: https://www.cnblogs.com/AProblemSolver/p/16756081.html

相关文章

  • Codeforces Round #804 (Div. 2) C(组合 + mex)
    CodeforcesRound#804(Div.2)C(组合+mex)本萌新的第一篇题解qwq题目链接:传送门QAQ题意:给定一个\(\left[0,n-1\right]\)的排列,问有多少个排列,所有的子区间的......
  • Codeforces Round #824 (Div. 2)
    题目链接A~D懒得写。不是因为题不好,其实我觉得做起来非常舒适。但就是懒得写了(E-HousePlanning\(d_1,d_2\)全打乱感觉很难。先看看不打乱怎么做。对于每个\(i......
  • Codeforces Round #774 (Div. 2) - E. Power Board
    枚举+数论Problem-E-Codeforces题意有一个\(n*m\;(1<=n,m<=10^6)\)的矩阵,第i行第j列是\(i^j\),求这个矩阵中的\(n*m\)的数中有多少种不同的数思路......
  • Codeforces Round #785 (Div. 2) - D. Lost Arithmetic Progression
    GCDProblem-D-Codeforces题意有2个等差数列A,B,它们公有的项组成新的等差数列C;现在给出B的(首项,公差,项数)=(b,q,y),C的(首项,公差,项数)=(c,r,z)求满足条件的A的数量,如......
  • CodeForces 1527E Partition Game
    洛谷传送门CF传送门考虑朴素dp:设\(f_{i,j}\)表示分了\(j\)段且第\(j\)段的末尾是\(i\)的最小花费。有转移:\(f_{i,j}\gets\min\limits_{k=0}^{i-1}f_{k,j-1......
  • Codeforces Educational Codeforces Round 136 C D
    C一开始没有读懂题意,以为是轮流游戏,看别人翻译才发现先手在下一轮会变为反手,导致搞了半天过不了样例。可以知道如果\(n\)这张牌在Alice手中,则Alice先手打出这张牌必胜。......
  • Codeforces Round #824 (Div. 2)
    CodeforcesRound#824(Div.2)A#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#defineendl'\n'usingnamespacestd;intt,n;inlin......
  • Codeforces Round #824赛时情况&赛后总结
    前两天的CF到今天才总结,还是太鸽了呢赛时首先看了题目,由于英语障碍,我还在看A题时,YSC就已经A了(我还是太逊了)。看懂后,发现A是道水题(正常),快速切掉。随后看B,阅读倒没什么障......
  • Codeforces Global Round 22
    题目链接这场彻底打崩了,只做了A,B,C,可以看出我离退役已经不远力!A.GloryAddictstrash题不写。感觉出得很没意思。B.PrefixSumAddicts用\(s_{n-k+1}\sims_n\)......
  • CodeForces 1455G Forbidden Value
    洛谷传送门CF传送门小清新动态开点线段树优化dp题。首先题目中的if嵌套看起来就很烦,可以考虑建树,外面再套一层大的if0...end,这样就将本题转化成一个树上问题。......