Codeforces Round 882 div.2 B
Smiling & Weeping
1 second
256 megabytes
standard input
standard output
Jonathan is fighting against DIO's Vampire minions. There are n� of them with strengths a1,a2,…,an�1,�2,…,��.
Denote (l,r)(�,�) as the group consisting of the vampires with indices from l� to r�. Jonathan realizes that the strength of any such group is in its weakest link, that is, the bitwise AND. More formally, the strength level of the group (l,r)(�,�) is defined as
Here, && denotes the bitwise AND operation.
Because Jonathan would like to defeat the vampire minions fast, he will divide the vampires into contiguous groups, such that each vampire is in exactly one group, and the sum of strengths of the groups is minimized. Among all ways to divide the vampires, he would like to find the way with the maximum number of groups.
Given the strengths of each of the n� vampires, find the maximum number of groups among all possible ways to divide the vampires with the smallest sum of strengths.
The first line contains a single integer t� (1≤t≤104)(1≤�≤104) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n� (1≤n≤2⋅1051≤�≤2⋅105) — the number of vampires.
The second line of each test case contains n� integers a1,a2,…,an�1,�2,…,�� (0≤ai≤1090≤��≤109) — the individual strength of each vampire.
The sum of n� over all test cases does not exceed 2⋅1052⋅105.
For each test case, output a single integer — the maximum number of groups among all possible ways to divide the vampires with the smallest sum of strengths.
思路:咱们找一下规律,主要分为两个情况:
1.所有数的&值非零:
&到最后一个数仍旧非零,虽然分组为一但能保证最小,多&一个 (新值) <= (旧值) , 即使等于,值+旧值 > 新值 ,并不能保证最小
所以分组只能为1,才能保证所有值的&最小
2.所有数的&值为零:
贪心算法:向前运算 if(an == 0) ans++ , an = num[i]; 最后判断一下an的值是否为0
现在是代码时间欢迎━(*`∀´*)ノ亻!
1 #include<bits/stdc++.h>
2 using namespace std;
3 int t , n;
4 int main()
5 {
6 scanf("%d",&t);
7 while(t--)
8 {
9 scanf("%d",&n);
10 vector<int> num(n+10);
11 int sum = 0;
12 for(int i = 1; i <= n; i++)
13 scanf("%d",&num[i]);
14 sum = num[1];
15 for(int i = 1; i <= n; i++)
16 sum &= num[i];
17 if(sum != 0) { printf("1\n"); continue; }
18 int an = num[1] , ans = 0;
19 for(int i = 2; i <= n; i++)
20 {
21 if(an == 0)
22 an = num[i] , ans++;
23 else
24 an &= num[i];
25 }
26 if(an == 0 || ans == 0) ans++;
27 printf("%d\n",ans);
28 }
29 return 0;
30 }
没有长亭古道,没有劝君更尽一杯酒,和所有文章的结尾一样,我们下次再见
ヾ( ̄▽ ̄)Bye~Bye~
Codeforces Round 882 div.2 B的更多相关文章
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
随机推荐
- django 如何提升性能(高并发)
django 如何提升性能(高并发) 对一个后端开发程序员来说,提升性能指标主要有两个一个是并发数,另一个是响应时间网站性能的优化一般包括 web 前端性能优化,应用服务器性能优化,存储服务器优化. ...
- CentOS7环境编译python3.9版本pjsua
环境:CentOS 7.6_x64 Python版本 :3.9.12 pjsip版本:2.13 一.背景描述 pjsip地址:https://www.pjsip.org/ GitHub地址:https ...
- Elastaticsearch 集群部署
系统Ubuntu 16.04 Elastaticsearch 5.6.9 Kibana 5.6.9 官网地址 https://www.elastic.co/products/elasticsearch ...
- JVM监控工具jstat使用介绍
jstat 是 Java 自带的一个命令行工具,用于监控 JVM 运行时的状态信息.它可以通过以下格式的命令来调用: jstat [option] <vmid> [<interval ...
- Python 标准类库-并发执行之multiprocessing-基于进程的并行
实践环境 Python3.6 介绍 multiprocessing是一个支持使用类似于线程模块的API派生进程的包.该包同时提供本地和远程并发,通过使用子进程而不是线程,有效地避开了全局解释器锁.因此 ...
- Spark架构与运行流程
1. 阐述Hadoop生态系统中,HDFS, MapReduce, Yarn, Hbase及Spark的相互关系. 2. Spark已打造出结构一体化.功能多样化的大数据生态系统,请简述Spark生态 ...
- LLE算法的应用场景和案例:详解LLE算法在实际问题中的效果和表现
目录 引言 随着深度学习技术的不断发展,神经网络模型在人工智能领域的应用越来越广泛.其中,LLE(Largely Element-wise Linear) 神经网络是一种常用的神经网络模型,其基本思想 ...
- mysql索引优化-01
1.1索引是什么? mysql官方对于索引的定义:可以帮助mysql高效的获取数据的数据结构. mysql在存储数据之外,数据库系统中还维护着满足特定查找算法的数据结构,这些数据结构给以某种引 ...
- Web网页音视频通话之Webrtc相关操作(一)
目录 打开摄像头/关闭摄像头 静音/解除静音 打开视频/关闭视频 截图且下载 打开摄像头/关闭摄像头 效果图 HTML <!DOCTYPE html> <html lang=&quo ...
- 2023年icpc大学生程序设计竞赛-crf
第一次在除郑轻以外的校外的地方比赛,也是第一次出市比赛,赛程也比较长.20号出发的时候遇到一些意外,不过无伤大雅,第一天热身赛平平无奇,晚上的时候补了一下前年icpc的题,一个多小时做了五题,很是自信 ...