1269 - Consecutive Sum
| Time Limit: 3 second(s) | Memory Limit: 64 MB |
Little Jimmy is learning how to add integers. As in decimal the digits are 0 to 9, it makes a bit hard for him to understand the summation of all pair of digits. Since addition of numbers requires the knowledge of adding digits. So, his mother gave him a software that can convert a decimal integer to its binary and a binary to its corresponding decimal. So, Jimmy's idea is to convert the numbers into binaries, and then he adds them and turns the result back to decimal using the software. It's easy to add in binary, since you only need to know how to add (0, 0), (0, 1), (1, 0), (1, 1). Jimmy doesn't have the idea of carry operation, so he thinks that
1 + 1 = 0
1 + 0 = 1
0 + 1 = 1
0 + 0 = 0
Using these operations, he adds the numbers in binary. So, according to his calculations,
3 (011) + 7 (111) = 4 (100)
Now you are given an array of n integers, indexed from 0 to n-1, you have to find two indices i j in the array (0 ≤ i ≤ j < n), such that the summation (according to Jimmy) of all integers between indices i and jin the array, is maximum. And you also have to find two indices, p q in the array (0 ≤ p ≤ q < n), such that the summation (according to Jimmy) of all integers between indices p and q in the array, is minimum. You only have to report the maximum and minimum integers.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 50000). The next line contains n space separated non-negative integers, denoting the integers of the given array. Each integer fits into a 32 bit signed integer.
Output
For each case, print the case number, the maximum and minimum summation that can be made using Jimmy's addition.
Sample Input |
Output for Sample Input |
|
2 5 6 8 2 4 2 5 3 8 2 6 5 |
Case 1: 14 2 Case 2: 15 1 |
Note
Dataset is huge, use faster I/O methods.
题意:求连续区间的异或的最大和最小值;
思路:trie树,贪心;
先求一遍前缀异或和;然后先将0加入tire树,0异或任何值为那个值本身,然后我们知道pre[i]^pre[j];j<i;
ans[j+1]^ans[j+2]...^ans[i];也就是区间[i,j],那么我们把前面的前缀加入tire,然后我们在树中贪心选取,如果是找最大的,就贪心选与当前位不同的,因为是按最高位
依次往下,所以我们保证高位数要尽量大,如果不存在与当前位相同,那么只能走相同的。知道咋贪心最大的,最小的和这差不多。
复杂度为(n*log(1<<31-1));
1 #include<stdio.h>
2 #include<algorithm>
3 #include<string.h>
4 #include<stdlib.h>
5 #include<iostream>
6 using namespace std;
7 typedef long long LL;
8 LL ans[500005];
9 int id[32];
10 struct node
11 {
12 node *p[2];
13 node()
14 {
15 memset(p,0,sizeof(p));
16 }
17 };
18 node *root;
19 void in(int *v)
20 {
21 int i,j,k;
22 node *c=root;
23 for(i=30; i>=0; i--)
24 {
25 int pre=v[i];
26 if(c->p[v[i]]==NULL)
27 {
28 c->p[v[i]]=new node();
29 }
30 c=c->p[v[i]];
31 }
32 }
33 LL ma(int *v)
34 {
35 LL sum=0;
36 LL l=1;
37 node*c=root;
38 int i;
39 for(i=30; i>=0; i--)
40 {
41 int pre=v[i];
42 if(c->p[(pre+1)%2]==NULL)
43 {
44 c=c->p[pre];
45 sum*=2;
46 sum+=0;
47 }
48 else
49 {
50 c=c->p[(pre+1)%2];
51 sum*=2;
52 sum+=1;
53 }
54 }
55 return sum;
56 }
57 LL mi(int *v)
58 {
59 LL sum=0;
60 LL l=2;
61 node*c=root;
62 int i;
63 for(i=30; i>=0; i--)
64 {
65 int pre=v[i];
66 if(c->p[pre]==NULL)
67 {
68 c=c->p[(pre+1)%2];
69 sum*=2;
70 sum+=1;
71 }
72 else
73 {
74 c=c->p[pre];
75 sum*=2;
76 sum+=0;
77 }
78 }
79 return sum;
80 }
81 void del(node *c)
82 {
83 for(int i=0; i<2; i++)
84 {
85 if(c->p[i]!=NULL)
86 del(c->p[i]);
87 }
88 free(c);
89 }
90 int main(void)
91 {
92 int i,j,k;
93 scanf("%d",&k);
94 int s;
95 int n,m;
96 for(s=1; s<=k; s++)
97 {
98 scanf("%d",&n);
99 for(i=1; i<=n; i++)
100 {
101 scanf("%lld",&ans[i]);
102 ans[i]^=ans[i-1];
103 }
104 root=new node();
105 LL maxx=0;
106 LL minn=1e12;
107 memset(id,0,sizeof(id));
108 in(id);
109 for(i=1; i<=n; i++)
110 {
111 memset(id,0,sizeof(id));
112 LL kk=ans[i];
113 int cnt=0;
114 while(kk)
115 {
116 id[cnt++]=kk%2;
117 kk/=2;
118 }
119 maxx=max(maxx,ma(id));
120 minn=min(minn,mi(id));
121 in(id);
122 }del(root);
123 printf("Case %d: ",s);
124 printf("%lld %lld\n",maxx,minn);
125 }return 0;
126 }
1269 - Consecutive Sum的更多相关文章
- LightOJ 1269 Consecutive Sum (Trie树)
Jan's LightOJ :: Problem 1269 - Consecutive Sum 题意是,求给定序列的中,子序列最大最小的抑或和. 做法就是用一棵Trie树,记录数的每一位是0还是1.查 ...
- LightOJ 1269 - Consecutive Sum Trie树
题意:给出一串序列,求区间连续异或值的最大和最小. 思路:如果不是出在专题里,想不到可以用字典树做.先求前缀异或值,转为二进制,加入Trie树中,如果要求最大,就是尽可能走和当前位数字相反的,这样异或 ...
- Consecutive Sum LightOJ - 1269(区间异或和)
Consecutive Sum 又来水一发blog... 本来是昨天补codechef的题,最后一道题是可持久化字典树,然后去黄学长博客看了看 觉得字典树写法有点不太一样,就想着用黄学长的板子写码几道 ...
- hdoj 1977 Consecutive sum II
Consecutive sum II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDOJ(HDU) 1977 Consecutive sum II(推导、、)
Problem Description Consecutive sum come again. Are you ready? Go ~~ 1 = 0 + 1 2+3+4 = 1 + 8 5+6+7+8 ...
- hdu 1231, dp ,maximum consecutive sum of integers, find the boundaries, possibly all negative, C++ 分类: hdoj 2015-07-12 03:24 87人阅读 评论(0) 收藏
the algorithm of three version below is essentially the same, namely, Kadane's algorithm, which is o ...
- BZOJ 2157: 旅游( 树链剖分 )
树链剖分.. 样例太大了根本没法调...顺便把数据生成器放上来 -------------------------------------------------------------------- ...
- Codeforces Round #747 (Div. 2) Editorial
Codeforces Round #747 (Div. 2) A. Consecutive Sum Riddle 思路分析: 一开始想起了那个公式\(l + (l + 1) + - + (r − 1) ...
- Codeforces Round #821(Div.2) (A-C) 题解
Codeforces Round #821(Div.2) (A-C) A.Consecutive Sum 大致题意 给定一组共 n 个数据 ,如果俩个数的下标在 mod k 意义下同余,则可以交换a[ ...
随机推荐
- 【Go语言学习笔记】包
包其实是每个大型工程都会使用的模块化工具. 将相关的代码封装成一个包,给其他项目调用,提供不同的功能. GO的设计是将一个文件夹看成一个包,虽然不一定非要用文件夹的名字,但是比较建议. 同一个文件夹下 ...
- 实时数仓(二):DWD层-数据处理
目录 实时数仓(二):DWD层-数据处理 1.数据源 2.用户行为日志 2.1开发环境搭建 1)包结构 2)pom.xml 3)MykafkaUtil.java 4)log4j.properties ...
- vmware使用nat连接配置
一.首先查看自己的虚拟机服务有没有开启,选择电脑里面的服务查看: 1.计算机点击右键选择管理 2.进入管理选择VM开头的服务如果没有开启的话就右键开启 二.虚拟机服务开启后就查看本地网络虚拟机的网 ...
- jenkins之代码回滚
#:通过传参数方式 #:保存后就会看到这样 #;:我们在jenkins服务器写一个脚本 root@ubuntu:~# mkdir /root/script/web1 -pv mkdir: create ...
- Mysql 常见报错和疑问汇总
1.初始化数据库的时候报错 error while loading shared libraries: libstdc++.so.5: cannot open shared object file: ...
- 错误: 找不到或无法加载主类(IDEA中启动spring boot项目)
版权声明:本文为博主原创文章,如果转载请给出原文链接:http://www.jufanshare.com/content/142.html 提示:需要对IDEA编辑工具使用熟悉 出现一个问题,就是sp ...
- 【C/C++】算法入门:排序/算法笔记
(设排序从小到大) 冒泡排序 这个大家都会,从第一个开始往后俩俩交换,然后第二个,最后到最后一个,复杂度n^2 选择排序 思路和冒泡差不多,比如要得到从小到大的排序,就是从第一个开始,i取1~n,每次 ...
- centos7部署mysql-5.7
目录 一.环境声明 二.程序部署 三.更改初始密码 一.环境声明 [mysql-Server] 主机名 = host-1 系统 = centos-7.3 地址 = 1.1.1.1 软件 = mysql ...
- pycharm的破解和基本使用
pycharm的破解 pycharm的账号注册 在完成安装后打开pycharm软件,需要选择购买或是使用.点击试用,选择进入官网注册账号. 进入官网后选择邮箱登录,输入自己的邮箱,点击sign up ...
- CTFHub[PWN技能树]——栈溢出
文章目录 一.ret2text 二.ret2shellcode 一.ret2text 例行检查,64位程序,没有开启任何保护 本地运行一下情况,看看大概的情况 64位ida载入,检索程序里的字符串发现 ...