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为还需要的蜡烛数, ...
随机推荐
- ERROR: Failed to install the following Android SDK packages as some licences have not been accepted.
android studio 配置sdk时提示如下错误 麻麻蛋~ 根据accepted 了解到是安装android-26时未被允许:于是执行如下步骤 1.cd 到sdk目录 D:\develop\An ...
- Factory Method Pattern 工厂方法模式简介与 C# 示例【创建型】【设计模式来了】
〇.简介 1.什么是工厂方法模式? 一句话解释: 实体类和工厂类均为单独实现,不影响已实现的类,方便扩展. 工厂方法模式(Factory Method Pattern)是一种创建型模式,它允许客户 ...
- JS逆向实战15——猿人学第五题 动态cookie乱码增强
声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除! 网站 https://ma ...
- RStuido Server 选择不同的 R 版本(conda 中的不同 R 版本)
自从上一次服务器重装系统之后,总感觉缺少了一些东西,安装R包很多依赖库报错,也可以解决,但总是存在,烦. 一天,一个同事问我说ggpubr包安装不成功,我就自己试了一下,真的是--安装不成功. 当你到 ...
- 使用RSS打造你的科研资讯头条
本文章为 "生信草堂" 首发,经生信草堂授权.原作者(Steven Shen)同意转载.由于微信不允许外部链接,你需要点击文章尾部左下角的 "阅读原文",才能访 ...
- [转载]C++ 入门教程(41课时) - 阿里云大学
C++ 教程 C++ 是一种中级语言,它是由 Bjarne Stroustrup 于 1979 年在贝尔实验室开始设计开发的.C++ 进一步扩充和完善了 C 语言,是一种面向对象的程序设计语言.C++ ...
- Kali Linux的目录结构
Linux目录和Windows目录有着很大的不同,Linux目录类似一个树,最顶层是其根目录,如下图: kali Linux 目录结构 bin目录 存放二进制可执行文件(如我们常用的 ls ping ...
- 2023-06-17:说一说redis中渐进式rehash?
2023-06-17:说一说redis中渐进式rehash? 答案2023-06-17: 在Redis中,如果哈希表的数组一直保持不变,就会增加哈希冲突的可能性,从而降低检索效率.为了解决这个问题,R ...
- 10. docker方式下的mysql设置主从复制(一主两从)
上一篇 [centos 使用 docker 方式安装 mysql] 笔记中,我们在三个虚拟机中使用 docker 方式新建了三个 mysql 容器服务,那么我们这篇文章来记录下,如何在这三台机器中设置 ...
- Git 多账号配置
本地登录多账号并连接对应的远程仓库,主要就是 密钥配对,我这里刚开始配了密钥也将密钥复制到ssh但是还是连接不到第二个远程仓库,后来发现是需要 密钥代理 1.在当前项目下更改git账号信息: git ...