[luogu4005]小Y和地铁【搜索+树状数组】
传送门:https://www.luogu.org/problemnew/show/P4005
最简单的暴力拿最高的分,二进制爆搜。
#include <bits/stdc++.h>
#define ll long long
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define N 55
using namespace std;
template <typename T>
inline void read(T &x) {
x = 0; T fl = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') fl = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x *= fl;
}
struct BIT {
#define lowbit(x) (x&-x)
int tr[N], n;
void init(int M) {
n = M;
memset(tr, 0, sizeof(tr));
}
void add(int x, int val) {
for (; x <= n; x += lowbit(x)) tr[x] += val;
}
int sum(int x) {
int res = 0;
for (; x; x -= lowbit(x)) res += tr[x];
return res;
}
int query(int l, int r) {
return sum(r) - sum(l - 1);
}
}up, down;
int tot, ans, n;
int l[N], r[N], a[N];
void dfs(int st, int sum) {
if (st > tot) {
ans = min(ans, sum);
return;
}
if (sum > ans) return;
int a1 = min(up.query(l[st], r[st]), down.query(l[st], n) + up.query(r[st], n));
up.add(r[st], 1);
dfs(st + 1, sum + a1);
up.add(r[st], -1);
int a2 = min(down.query(l[st], r[st]), up.query(l[st], n) + down.query(r[st], n));
down.add(r[st], 1);
dfs(st + 1, sum + a2);
down.add(r[st], -1);
}
int main() {
int cas; read(cas);
while (cas --) {
read(n);
ans = inf;
for (int i = 1; i <= n; i ++) read(a[i]);
tot = 0;
for (int i = 1; i <= n; i ++) {
for (int j = i + 2; j <= n; j ++) {
if (a[i] == a[j]) {
l[++ tot] = i;
r[tot] = j;
break;
}
}
}
up.init(n); down.init(n);
dfs(1, 0);
printf("%d\n", ans);
}
return 0;
}
[luogu4005]小Y和地铁【搜索+树状数组】的更多相关文章
- 【bzoj4548】小奇的糖果 STL-set+树状数组
题目描述 平面上有n个点,每个点有一种颜色.对于某一条线段,选择所有其上方或下方的点.求:在不包含所有颜色的点的前提下,选择的点数最多是多少.(本题中如果存在某颜色没有相应的点,那么选择任何线段都不算 ...
- 算法笔记求序列A每个元素左边比它小的数的个数(树状数组和离散化)
#include <iostream> #include <algorithm> #include <cstring> using namespace std ; ...
- 2019.01.21 bzoj2441: [中山市选2011]小W的问题(树状数组+权值线段树)
传送门 数据结构优化计数菜题. 题意简述:给nnn个点问有多少个www型. www型的定义: 由5个不同的点组成,满足x1<x2<x3<x4<x5,x3>x1>x2 ...
- CodeForces12D 树状数组降维
http://codeforces.com/problemset/problem/12/D 题意 给N (N<=500000)个点,每个点有x,y,z ( 0<= x,y,z <=1 ...
- BZOJ4237 稻草人(分治+树状数组+单调栈)
如果要询问的某个纵坐标为inf的点左边是否有点能与其构成所要求的矩形,只要用个单调栈就可以了.可以想到用分治来制造单调性. 按横坐标排序,每次考虑跨过分治中心的矩形.考虑右边的每个点能与左边的哪些点构 ...
- 【BZOJ4384】[POI2015]Trzy wieże 树状数组
[BZOJ4384][POI2015]Trzy wieże Description 给定一个长度为n的仅包含'B'.'C'.'S'三种字符的字符串,请找到最长的一段连续子串,使得这一段要么只有一种字符 ...
- codeforces 589G G. Hiring(树状数组+二分)
题目链接: G. Hiring time limit per test 4 seconds memory limit per test 512 megabytes input standard inp ...
- Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组
E. George and Cards George is a cat, so he loves playing very much. Vitaly put n cards in a row in ...
- 2016 Multi-University Training Contest 4 Bubble Sort(树状数组模板)
Bubble Sort 题意: 给你一个1~n的排列,问冒泡排序过程中,数字i(1<=i<=n)所到达的最左位置与最右位置的差值的绝对值是多少 题解: 数字i多能到达的最左位置为min(s ...
随机推荐
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.bw.mapper.BillMapper.getBillList at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:225
这个错误是没有找到映射文件 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.b ...
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-25: ordinal not in range(128)
python报错:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-25: ordinal not in ...
- K8S、云计算、大数据、编程语言
云计算.大数据.编程语言学习指南下载,100+技术课程免费学!这份诚意满满的新年技术大礼包,你Get了吗?-云栖社区-阿里云https://yq.aliyun.com/articles/691028 ...
- 中国科学技术大学统一身份认证系统CAS
CAS | Apereohttps://www.apereo.org/projects/cas 中国科学技术大学统一身份认证系统https://passport.ustc.edu.cn/login?s ...
- .net 报错汇总——持续更新
1.未能找到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPla PM> Install-Package Microsoft ...
- [转帖]CS、IP和PC寄存器
https://www.cnblogs.com/zhuge2018/p/8466288.html 之前的理解不对 当然了 现在的理解也不太对.. CS.IP和PC寄存器 CS寄存器和IP寄存器: 首先 ...
- Chrome 使用绿色版实现同一个机器 打开多个不同的chrome版本
1. 之前找了一个方案能够实现 多个chrome版本的 同时安装 但是发现不是很好. 2. 最近的一个办法 就是使用chrome的绿色版来实现 3. 下载地址: https://www.chrome6 ...
- syncthing 多主机同步文件工具
周五看了下阮一峰的blog 看到有一个 syncthing的小工具挺好用的 进行了简单的尝试: 1. 下载文件位置: https://syncthing.net 2. 下载文件后的简单安装 绿色版直接 ...
- laravel 项目表单中有csrf_token,但一直报错419错误 解决redis连接错误:MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persi
laravel 项目表单中有csrf_token,但一直报错419错误,因为项目中使用到Redis缓存,在强制关闭Redis后出现的问题,查询laravel.log文件查找相关问题 安装redis后在 ...
- Netcat实用操作
写久了web倦了,第n次开始尝试网络开发,于是熟悉一下常用工具. 尝试了一下netcat来测试服务器,或者充当客户端都异常好用.于是记录一下常用的一下命令 1. 充当服务器,或者客户端进行访问 通过n ...