bzoj 1702 贪心,前缀和
[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 807 Solved: 317
[Submit][Status][Discuss]
Description
Farmer John's N cows (1 <= N <= 100,000) share many similarities. In fact, FJ has been able to narrow down the list of features shared by his cows to a list of only K different features (1 <= K <= 30). For example, cows exhibiting feature #1 might have spots, cows exhibiting feature #2 might prefer C to Pascal, and so on. FJ has even devised a concise way to describe each cow in terms of its "feature ID", a single K-bit integer whose binary representation tells us the set of features exhibited by the cow. As an example, suppose a cow has feature ID = 13. Since 13 written in binary is 1101, this means our cow exhibits features 1, 3, and 4 (reading right to left), but not feature 2. More generally, we find a 1 in the 2^(i-1) place if a cow exhibits feature i. Always the sensitive fellow, FJ lined up cows 1..N in a long row and noticed that certain ranges of cows are somewhat "balanced" in terms of the features the exhibit. A contiguous range of cows i..j is balanced if each of the K possible features is exhibited by the same number of cows in the range. FJ is curious as to the size of the largest balanced range of cows. See if you can determine it.
N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色,
每头牛有多种特色,用二进制01表示它的特色ID。比如特色ID为13(1101),
则它有第1、3、4种特色。[i,j]段被称为balanced当且仅当K种特色在[i,j]内
拥有次数相同。求最大的[i,j]段长度。
Input
* Line 1: Two space-separated integers, N and K.
* Lines 2..N+1: Line i+1 contains a single K-bit integer specifying the features present in cow i. The least-significant bit of this integer is 1 if the cow exhibits feature #1, and the most-significant bit is 1 if the cow exhibits feature #K.
Output
* Line 1: A single integer giving the size of the largest contiguous balanced group of cows.
Sample Input
7
6
7
2
1
4
2
INPUT DETAILS:
The line has 7 cows with 3 features; the table below summarizes the
correspondence:
Feature 3: 1 1 1 0 0 1 0
Feature 2: 1 1 1 1 0 0 1
Feature 1: 1 0 1 0 1 0 0
Key: 7 6 7 2 1 4 2
Cow #: 1 2 3 4 5 6 7
Sample Output
OUTPUT DETAILS:
In the range from cow #3 to cow #6 (of size 4), each feature appears
in exactly 2 cows in this range:
Feature 3: 1 0 0 1 -> two total
Feature 2: 1 1 0 0 -> two total
Feature 1: 1 0 1 0 -> two total
Key: 7 2 1 4
Cow #: 3 4 5 6
HINT
鸣谢fjxmyzwd
Source
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std; int k;
int hash[][],mod=,a[][],s[][]; inline bool check(int t,int xt)
{
int i;
bool flag=true;
for(i=;i<=k-;i++)
if(s[xt][i]!=hash[t][i])
return false;
return true;
}
inline int find(int x,int xt,int xp)
{
int t=x;
while(hash[t][]!=-)
{
if(!check(t,xt)) t=(t+)%mod;
else break;
}
if(hash[t][]==-)
{
int i;
for(i=;i<=k-;i++)
hash[t][i]=s[xt][i];
hash[t][]=xp;
hash[t][]=;
return xp;
}
return hash[t][];
}
int main()
{
int n;
scanf("%d%d",&n,&k);
int i,j;
int x;
for(i=;i<=n;i++)
{
scanf("%d",&x);
int p=;
while(x!=)
{
a[i][p]=x%;
x=x/;
p++;
}
}
for(i=;i<=n;i++)
for(j=;j<=k-;j++)
s[i][j]=s[i-][j]+a[i][j];
for(i=;i<=n;i++)
for(j=k-;j>=;j--)
s[i][j]-=s[i][];
memset(hash,-,sizeof(hash));
int ans=;
for(i=;i<=n;i++)
{
int p=;
for(j=k-;j>=;j--)
{
p=(p*+s[i][j])%mod;
while(p<)
p=-p;
}
int loc=find(p,i,i);
ans=max(ans,i-loc);
}
printf("%d\n",ans);
}
bzoj 1702 贪心,前缀和的更多相关文章
- 【贪心+前缀】C. Fountains
http://codeforces.com/contest/799/problem/C [题意] 有n做花园,有人有c个硬币,d个钻石 (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 ...
- hdu3613 Best Reward manacher+贪心+前缀和
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...
- bzoj 1044 贪心二分+DP
原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1044 首先对于第一问,我们可以轻易的用二分答案来搞定,对于每一个二分到的mid值 我们从l ...
- bzoj 1193 贪心
如果两点的曼哈顿距离在一定范围内时我们直接暴力搜索就可以得到答案,那么开始贪心的跳,判断两点横纵坐标的差值,差值大的方向条2,小的条1,不断做,直到曼哈顿距离较小时可以暴力求解. 备注:开始想的是确定 ...
- bzoj 2697 贪心
就贪心就行了,首先可以看成n个格子,放物品,那么 一个物品假设放3个,放在1,k,n处,那么价值和放在1,n 是一样的,所以一个物品只放两个就行了,价值大的应该尽量放 在两边,那么排序之后模拟就行了 ...
- bzoj 3037 贪心
我们可以贪心的分析,每个点的入度如果是0,那么这个点不可能 被用来更新答案,那么我们每次找入度为0的点,将他去掉,如果他连的 点没有被更新过答案,那么更新答案,去掉该点,环的时候最后处理就行了 /** ...
- suoi38 卖XY序列 (贪心+前缀和)
因为只能带一个,买卖价格又一样,所以只要右边的比左边的大,就从这买下来然后带到下一个卖掉就行了(我想到别处再卖的话大不了再重新买回来嘛) 所以给max(w[i]-w[i-1],0)维护一个前缀和就行了 ...
- bzoj 1193 贪心+bfs
1193: [HNOI2006]马步距离 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2015 Solved: 914[Submit][Statu ...
- bzoj 1899 贪心+dp
思路:这个贪心排顺序我居然没看出来. 吃饭时间长的在前面, 用反证法很容易得出. 剩下的就是瞎dp啦. #include<bits/stdc++.h> #define LL long lo ...
随机推荐
- 【前端】jq弹出一个透明小提示窗,然后逐渐消失
function show_main(content) { var showWindow = '<div id="show_main" style="borde ...
- ssm框架搭建(下) 简单案例
前言 这段时间没有更新博客,一直想做一个基于ssm的简单的项目.经过多次的尝试,终于实现了简单的增删查改功能了. 正文 由于前端的技术不是很熟悉,经过多方的查阅,使用了bootstrap的样式,来使界 ...
- docker新手入门(基本命令以及介绍)
Docker 的核心内容 镜像 (Image) 容器 (Container) 仓库 (Repository) Registry 用来保存用户构建的镜像 docker的开始使用: 1. docker ...
- 【前端】pid2children iview tree json
<script> import inBody from '../inBody' export default { components:{ inBody } ,data () { retu ...
- OpenCV2:第二章 创建图像并显示
一.简介 相当于在PS中,新建一个画布 二.CvMat类/LPLImage和CvMat结构体 参考: OpenCV2:第一章 图像表示 三.create() Mat m(2,2,CV_8UC3); m ...
- ionic小白的学习路之安装运行篇
1.什麽是ionic? Ionic 是一款基于Angular.Cordova 的强大的HTML5 移动应用开发框架, 可以快速创建一个跨平台的移动应用.可以快速开发移动App.移动端WEB 页面.微信 ...
- TWaver3D直线、曲线、曲面的绘制
插播一则广告(长期有效) TWaver需要在武汉招JavaScript工程师若干 要求:对前端技术(JavasScript.HTML.CSS),对可视化技术(Canvas.WebGL)有浓厚的兴趣 基 ...
- WEB 前端模块化,读文笔记
文章链接 WEB 前端模块化都有什么? 知识点 根据平台划分 浏览器 AMD.CMD 存在网络瓶颈,使用异步加载 非浏览器 CommonJS 直接操作 IO,同步加载 浏览器 AMD 依赖前置 req ...
- VW结合rem进行移动端布局
---恢复内容开始--- html { font-size:10vw: } div { width: 1rem; height: 1rem; } VW这个单位适合用来适应不同设备的 一个设备的宽度就为 ...
- TCP socket编程记录(C语言)
最近使用到TCP的socket编程,记录一下思路. TCP Socket 服务器编程思路 0.初始化套接字库 1.创建套接字----------2个(服务器本身需要一个套接字,客户端要返回一个套接字) ...