首页 > 其他分享 >Codeforces Round 882 div.2 B

Codeforces Round 882 div.2 B

时间:2023-07-20 21:14:35浏览次数:41  
标签:882 vampires sum Codeforces number test groups each div.2

Smiling & Weeping

              ----玫瑰花你拿才好看,风景要和你看才浪漫--<-<-<@ B. Hamon Odyssey time limit per test 1 second memory limit per test 256 megabytes input standard input output 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

f(l,r)=al&al+1&al+2&…&ar.�(�,�)=��&��+1&��+2&…&��. 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.

Input

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.

Output

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.

题目链接:Problem - B - Codeforces

思路:咱们找一下规律,主要分为两个情况:

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~

 

标签:882,vampires,sum,Codeforces,number,test,groups,each,div.2
From: https://www.cnblogs.com/smiling-weeping-zhr/p/17569655.html

相关文章

  • Codeforces 1696G - Fishingprince Plays With Array Again
    初读题目可以发现一些性质:每次操作会使整个序列的和减少至多\(X+Y\),因此\(ans\ge\dfrac{\suma_i}{X+Y}\)。对于两个不相邻位置\(a_i,a_j(|i-j|>1)\),每次操作最多使它们的和减少\(\max(X,Y)\)。然后你发现两个限制可以结合在一起使用,稍加思考可以得到一个比较普适的结论:......
  • Codeforces 1446F - Line Distance
    感觉这种类似于让你找第\(k\)大距离的计算几何题其实都挺套路的。二分一个答案\(t\),然后思考一下什么样的点对满足原点到它们的连线的距离\(\let\)。以原点为圆心\(t\)为半径画个圆,然后分情况讨论:如果\((x_i,y_i)\)或\((x_j,y_j)\)在圆内,那么必然符合条件。如果\(......
  • Codeforces 1621H - Trains and Airplanes
    这能3500?对于一组在\(u\)上的询问,考虑每种线路\(x\),假设\(1\tou\)路径上线路\(x\)的长度为\(len\),那么不难发现收罚款的次数只有两种可能:\(\lfloor\dfrac{len}{T}\rfloor\)或者\(\lfloor\dfrac{len}{T}\rfloor+1\),且对于一个\(v\)满足\(z_u=z_v\)且\(v\)在\(u......
  • Educational Codeforces Round 151
    AB略C(简)将密码\(P\)与\(S\)进行匹配,按顺序决定\(P_i\),为了避免\(P\)成为\(S\)的子串,每次贪心地选择当前匹配位置最靠后的。若出现匹配不上则“YES”。D有点意思。从基础的情况入手:设\(\{s_i\}\)为\(\{a_i\}\)的前缀和,弄出\(\{s_i\}\)的图像,让我们考虑第一个......
  • Codeforces Round 885 (Div. 2)
    Preface打的就是依托答辩居然也能上分,看来手稳还是重要的说D题半场开香槟以为随便写,然后没想到怎么处理这个局部没有三分性的函数,直接GG后面听学长一说其实分成四种函数分别求最值即可直接恍然大悟,只能说还是太菜太菜而且F好像是个蓝桥杯的某个题的弱化版,我说比赛的时候怎么那......
  • Codeforces Round 882 div.2 A
    Smiling&Weeping----总有人间一两风,填我十万八千梦A.TheManwhobecameaGodtimelimitpertest1secondmemorylimitpertest256megabytesinputstandardinputoutputstandardoutput Kars istiredand......
  • Codeforces Round 885
    目录写在前面ABCDEF写在最后写在前面比赛地址:https://codeforces.com/contest/1848。我现在手里有三套题要补呃呃这套是两天前补的了,所以简单写写。太好玩辣,多校!A考虑一个2x2的矩阵,若小波奇和她的辣妹朋友位于对角线上就会被辣妹朋友堵在墙角,若相邻则永远抓不到。发现移......
  • Codeforces Round #885 (Div. 2) A-D
    比赛链接A代码#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;boolsolve(){intn,m,k;cin>>n>>m>>k;intx,y;cin>>x>>y;boolok=1;for(inti=1;i<=k;i++)......
  • Codeforces Round 885 (Div. 2)
    A.VikaandHerFriends枚举所有的点,判断是否存在点与Vika的距离和其他k个人的距离的奇偶性不同。#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintmod=998244353;voidsolve(){intn,m,k,sx,sy;cin>>n>>m>>k>......
  • CodeForces 1848E Vika and Stone Skipping
    洛谷传送门CF传送门感觉比这场的F简单。发现我们要进行\(x\)不断乘一些数然后求答案的操作,猜测答案与\(x\)的因数有关。如果你对数字比较敏感,不难发现答案就是\(x\)的奇因子个数。官方题解的证明:设\(x=f+(f-1)+\cdots+(f-(c-1))\),由等差数列求和公......