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为还需要的蜡烛数, ...
随机推荐
- 20230611 再次升级SSD
家里常用电脑的硬盘又显得捉襟见肘,老规矩,升级SSD.幸亏几年前摸索的方法记录下来了,翻出以前的博客复习一下.为了保险起见,也重新在网上搜了一下,看是不是有新的更方便的方法,答案是没有,只是搜出很多推 ...
- LeetCode 双周赛 107(2023/06/24)滑动窗口与离散化
本文已收录到 AndroidFamily,技术和职场问题,请关注公众号 [彭旭锐] 和 [BaguTree Pro] 知识星球提问. 往期回顾:LeetCode 单周赛第 348 场 · 数位 DP ...
- windows安全中心打不开
解决win11打不开安全中心的问题!!! 许多用户在最近都升级了Windows11系统,而且不少用户最近在使用Win11的时候发现自己打不开Windows安全中心 操作方法: 管理员权限打开Power ...
- IoTOS-v1.2.1接入J-IM(t-io)后台通知App
IoTOS v1.2.1 一.登录页增加可修改轮播 登录页增加可修改数据轮播: 首页轮播图由背景图片.标题.介绍.按钮一.按钮二(可配置跳转地址打开方式)组合而成 二.登录页增加常用运营商平台& ...
- 相较于Scrum, 我更推崇精益Kanban,帮助团队建立价值交付流,识别瓶颈问题
最近在学习实践精益Kanban方法,结合自己团队实践Srum的经历,整理些资料二者的差异.相较于Scrum, 我更推崇精益Kaban. Agile是一套理论和原则,就像天边的北极星.Devops是一种 ...
- 记一次 .NET 某游戏服务后端 内存暴涨分析
一:背景 1. 讲故事 前几天有位朋友找到我,说他们公司的后端服务内存暴涨,而且CPU的一个核也被打满,让我帮忙看下怎么回事,一般来说内存暴涨的问题都比较好解决,就让朋友抓一个 dump 丢过来,接下 ...
- C语言链表实现(郝斌数链表学习笔记)
#include "stdafx.h" #include<stdio.h> #include<stdlib.h> typedef struct Node { ...
- Blazor前后端框架Known-V1.2.6
V1.2.6 Known是基于C#和Blazor开发的前后端分离快速开发框架,开箱即用,跨平台,一处代码,多处运行. Gitee: https://gitee.com/known/Known Gith ...
- YUM histoy 与 RPM -qa --last
查看Linux yum安装包的安装时间,可以使用以下命令: rpm -qa --last 该命令将显示已安装的所有rpm包及其安装日期和时间. 可以使用管道符 '|' 和 grep 命令来查找特定的包 ...
- TypeScript: 类型别名
类型别名 在 TYPESCRIPT 中,类型别名可以使用 TYPE 关键字来定义.类型别名可以方便地定义一个类型,并为其起一个易于理解的名称,以便在其他地方引用该类型时使用. 示例 type MySt ...