K - The Number of Products
K - The Number of Products
题目入口:K - The Number of Products
Codeforces:B. The Number of Products
参考网站:
https://www.cnblogs.com/Tianwell/p/11530761.html
https://blog.csdn.net/weixin_43870697/article/details/100896170
解题思路
题意:给定一串数字 问从l到r形成的区间数字乘积为负、正的区间个数各是多少
思路:首先粗略谈一下第二个TLE的代码 想法是好的 数组的每一位异或实现存放当前为止所有数乘积结果的正负(0正 1负) 此外对于每一个l~r区间 想要知道其正负 只需要将到r为止乘积正负值与到l-1为止乘积正负值异或即可
但遗憾的是…TLE
我们开始说第一个代码
a[]
存放每一位数字
neg[i]
存放到第i
个数为止以i
结尾的区间数字乘积为负的区间个数
pos[i]
存放到第i
个数为止以i
结尾的区间数字乘积为正的区间个数
那么显然最终的负区间个数便是neg[i]
所有值之和 最终的正区间个数便是pos[i]
所有值之和
所以在这里我们分别用ncnt
(negative counting)和pcnt
(positive counting)来得到最终答案
首先利用第一个数字初始化neg[1]
和pos[1]
接下来从第2
到第n
个数遍历
如果第i
个数为负数 那么之前以第i-1
个数结尾的所有区间数字乘积为正的区间涵盖第i
个数(负数)后都转变为区间数字乘积为负的区间
因此此时的neg[i]
存放的值为pos[i-1]
加上1
(这个1代表这第i个数自己形成的负区间)
同理我们不难得到第i
个数位正数的情况的处理
到这里讲解结束
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 2e5+10;
LL a[N], neg[N], pos[N];
int main() {LL n;scanf("%lld", &n);for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);neg[1] = a[1]<0?1:0;pos[1] = 1-neg[1];LL ncnt = neg[1], pcnt = pos[1];for (int i = 2; i <= n; i++){if (a[i] < 0){neg[i] = pos[i-1]+1;pos[i] = neg[i-1];}else {pos[i] = pos[i-1]+1;neg[i] = neg[i-1];}ncnt += neg[i];pcnt += pos[i];}printf("%lld %lld\n", ncnt, pcnt);return 0;
}
TLE代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 2e5+10;
LL a[N];
int main() {LL n, ncnt = 0, pcnt = 0;scanf("%lld", &n);a[0] = 0;for (int i = 1; i <= n; i++){scanf("%lld", &a[i]);if (a[i] > 0) a[i] = 0;else a[i] = 1;a[i] ^= a[i-1];}for (int i = 1; i <= n; i++){for (int j = i; j <= n; j++){if (a[i-1] ^ a[j]) ncnt++;else pcnt++;}}printf("%lld %lld\n", ncnt, pcnt);return 0;
}
K - The Number of Products相关推荐
- 1215B. The Number of Products
B. The Number of Products:题目 #include <bits/stdc++.h> using namespace std; #define int long lo ...
- The Number of Products
The Number of Products You are given a sequence a1,a2,-,ana1,a2,-,an consisting of nn non-zero integ ...
- leetcode 476. 数字的补数(Java版)| How to extract ‘k’ bits from a given position in a number
题目 https://leetcode-cn.com/problems/number-complement/ 思路 我们想要返回已知数字的补数(num>=1). 思路: 获取 num 的二进制数 ...
- 和至少为k的最短子数组 python_LeetCode 862. 和至少为 K 的最短子数组
最近刷LeetCode题目的一些思路,题目信息 返回A 的最短的非空连续子数组的长度,该子数组的和至少为 K .如果没有和至少为 K 的非空子数组,返回 -1 . 示例 1:输入:A = [1], K ...
- POJ3111 K Best —— 01分数规划 二分法
题目链接:http://poj.org/problem?id=3111 K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissio ...
- 斯坦福CS231n项目实战(一):k最近邻(kNN)分类算法
我的网站:红色石头的机器学习之路 我的CSDN:红色石头的专栏 我的知乎:红色石头 我的微博:RedstoneWill的微博 我的GitHub:RedstoneWill的GitHub 我的微信公众号: ...
- K for the Price of One(EASY HARD)
This is the easy version of this problem. The only difference is the constraint on k - the number of ...
- Magento 产品推荐 extension Featured products 2.0 – revamped!
Platform: Community Platform: Community Platform: Enterprise Compatible with: 1.6, 1.6.1, 1.6.2.0, 1 ...
- magento创建自定义页面 (Bestseller实例) Bestseller products in Magento
Bestseller or best selling product is one of the features people tend to ask for when it comes to Ma ...
最新文章
- 带AM或者PM的时间字符串转data类型
- cad怎么卸载干净_百度软件中心助手怎么样干净卸载
- php常量定义表达式,从表达式创建PHP类常量的最佳解决方法?
- 总结 | 深度学习之Pytorch入门教程
- android程序数据迁移sd,如何迁移安卓手机及SD卡的数据?方法很简单!
- 笔记本cpu排名_2020年双十一哪一款笔记本电脑值得买?高性价比笔记本电脑推荐(10月更新)...
- 发电厂及电力系统类毕业论文文献都有哪些?
- 从原子结构--PN结原理--光伏发电
- COMSOL Multiphysics 多物理场仿真学习小记
- [ 加拿大访问学者 ] 用于签证的访问学者研究计划 [ 提交大使馆 签证用 ]
- openstack常用命令
- 回顾使用云桌面的那些经验
- 计算机三级网络技术最全知识点总结九
- 用python实现小猪佩奇
- C语言手机通讯录系统
- 【系统分析师之路】第十六章 复盘计算机网络(新技术领域)
- 学计算机语言步骤,如何学编程语言?好菜鸟学习编程语言的步骤
- mysql数据库有string_mysql数据库内STRING系列数据类型介绍
- LCP 2.分式简化
- 如何设置app字体跟随系统_Android 应用全局字体调节或禁止随系统字体大小更改...
热门文章
- Python 元祖的用法
- 机器学习中集成学习的一些知识
- Bochs源码分析 - 14:bochs关于实地址模式下IVT表的管理
- 树莓派3b+简单入门
- 夏天味道的一款改色膜,必选它
- 图像分割算法——Normalized Cuts(Ncuts) 算法的简单理解
- 使用qrcodejs和jimpjs两个库实现给图片加二维码水印
- “ERROR“ dispatch for GET “/error“ 错误解决
- HTML零基础入门详细教程
- 【数学竞赛】极限—等价无穷小