题目链接:https://www.luogu.org/problemnew/show/P3608 乍一看很容易想到O(N^2)的暴力. 对于每个H[i]从i~i-1找L[i]再从i+1~n找R[i],然后比较. 60分(数据够水) 但是这个思路就是很直白的模拟,让人不容易想到如何去优化. 然后我们换一个也是差不多O(N^2)的思路: 我们设法把H[i]所对应的第k大的k求出来. for example: H 34 6 23 0 5 99 2 Kth 2 4 3 7 5 1 6 那么我们就能发现,…
P3608 [USACO17JAN]Balanced Photo平衡的照片 题目描述 Farmer John is arranging his NN cows in a line to take a photo (1 \leq N \leq 100,0001≤N≤100,000). The height of the iith cow in sequence is h_ih​i​​, and the heights of all cows are distinct. As with all ph…
传送门 树状数组裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 100001 using namespace std; int n, m, ans; int a[N], b[N], R[N], L[N], c[N]; inline int read() { int x = 0, f = 1; char ch = getchar…
题目链接 Solution 先离散化,然后开一个大小为 \(100000\) 的树状数组记录前面出现过的数. 然后查询 \((h[i],n]\) 即可. 还要前后各做一遍. Code #include<bits/stdc++.h> #define N 200008 #define ll long long using namespace std; void in(ll &x) { char ch=getchar();ll f=1,w=0; while(ch<'0'||ch>…
题目链接:https://www.luogu.com.cn/problem/P3608 方法一 用树状数组求逆序对先后扫两遍,一次从前往后,一次从后往前,算出每头奶牛左右两边比她高的数量. 最后统计一下. #include <bits/stdc++.h> using namespace std; int sum[500010], l[100010], r[100010]; int n, m, u, v, a[500010], t[500010]; int ans; inline int rea…
1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 510  Solved: 196[Submit][Status][Discuss] Description Farmer John's N cows (1 <= N <= 100,000) share many similarities. In fact, FJ has been able to narrow…
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记得离散化 线段树合并版: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; ; ,edge_head[maxn],W[ma…
传送门 Description FJ正在安排他的N头奶牛站成一排来拍照.(1<=N<=100,000)序列中的第i头奶牛的高度是h[i],且序列中所有的奶牛的身高都不同. 就像他的所有牛的照片一样,FJ希望这张照片看上去尽可能好.他认为,如果L[i]和R[i]的数目相差2倍以上的话,第i头奶牛就是不平衡的.(L[i]和R[i]分别代表第i头奶牛左右两边比她高的数量).如果L[i]和R[i]中较大者比较小者的数量严格多两倍的话,这头奶牛也是不平衡的.FJ不希望他有太多的奶牛不平衡. 请帮助FJ计…
Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 9036    Accepted Submission(s): 4294 Problem Description A balanced number is a non-negative integer that can be balanced if a pi…
题目链接 luogu 思路 可以说是线段树合并的练手题目吧 也没啥说的,就是dfs,然后合并... 看代码吧 错误 和写主席树错的差不多 都是变量写错.... 代码 #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<=b;++i) using namespace std; const int maxn=1e5+7; inline int read() { int x=0,f=1;char s=getchar(); for(…
Description 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…
[题意]给定n头牛,k个特色,给出每头牛拥有哪些特色的二进制对应数字,[i,j]平衡当且仅当第i~j头牛的所有特色数量都相等,求最长区间长度. [算法]平衡树+数学转化 [题解]统计前缀和sum[i][j]表示前i头牛特色为j的数量,则区间i~j平衡需要满足: sum[j][1]-sum[i-1][1]=sum[j][2]-sum[i-1][2]=sum[j][3]-sum[i-1][3]=... 移项可得,只须 sum[j][1]-sum[j][2]=sum[i-1][1]-sum[i-1][…
题目链接:https://www.luogu.org/problemnew/show/P3609 ### 看着标签什么记搜什么暴力点进来,读完题第一直觉DP? 还真是个\(DP\). 题目所描述的状态十分明显,第 \(i\) 轮,变换 \(j\) 次,当前FJ手势 (\(x,y,z\)) 那我们不妨直接令\(f[i][j][k]\)表示当前第 \(i\) 轮,变换 \(j\) 次,在FJ手势为 \(k\) 时的胜场为多少.(这里懒得考虑啥手势能胜FJ,故直接用FJ的手势为参考.) 那么在转移时,…
题意: 平衡树定义为“一个整数的某个数位若是奇数,则该奇数必定出现偶数次:偶数位则必须出现奇数次”,比如 222,数位为偶数2,共出现3次,是奇数次,所以合法.给一个区间[L,R],问有多少个平衡数? 思路: 这题比较好解决,只有前导零问题需要解决.如果枚举到011,那么其前导零(偶数)出现了1次而已,而此数11却是平衡数,所以不允许前导零的出现! 由于dfs时必定会枚举到前导零,否则位数较少的那些统计不到.状态需要3维or2维也行,3维的比较容易处理,用一维表示数位出现次数,另一维表示数位是否…
https://www.luogu.org/problemnew/show/P3611 二分答案+优先队列 二分O(logn) 判一次正确性O(nlogn) 总体O(nlognlogn) 为了让priority_queue变成小根堆,就把元素全部取相反数了. #include<iostream> #include<cstdio> #include<queue> using namespace std; inline int rd(){ ,f=;char c; :; +c…
Description 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 第一行给出数字N,K 下面N行每行给出一个数字,代表这头牛的特征值 Output 求出一个区间值,在这个区间中,所有牛的这K种特征值的总和是相等的. Sample In…
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4759 [题解] 排序,从大到小插入,树状数组统计. # include <vector> # include <stdio.h> # include <string.h> # include <iostream> # include <algorithm> // # include <bits/stdc++.h> using n…
题目大意: 我们有一个数列,数列中有n个数,对于一个数ai,在它左边的比他大的数的个数为li,右边比他大的数的个数为ri,若li,ri中的较大者比较小者的两倍还大,那么他就是一个不平衡数,求不平衡数的数量. ————————————————我是分割线———————————————— 好吧,典型逆序对. 因为我们要求左边比他大的数的个数,所以我们倒着排序,然后再离散. 然后树状数组解决问题. 那么右边怎么办? 很显然我们得到的离散数组的值就是比他大的数的个数+1(它本身),所以右边的答案为:离散数…
[题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; int i,N,ans,l1,l2; ],val[],id[]; template <typename T> void read(T &x) { ; ; ; } +c-'; x*=f; } bool cmp(int a,int b) { return val[a] > val[b]; } int lowbit(int x)…
n<=100000个数表示每头牛在K<=30种物品的选取情况,该数在二进制下某位为0表示不选1表示选,求一个最大的区间使区间内选择每种物品的牛一样多. 数学转化,把不同状态间单变量的关系通过不等式移项转变为单状态的多变量关系. sum[i,j]表示前i头牛有多少选了物品j,那么问题要求即对任意j∈[1,K],sum[p,j]-sum[q,j]相等,使p-q最大.(多状态,单变量) 列出来,sum[p,1]-sum[q,1]=sum[p,2]-sum[q,2]=……,移项,sum[p,2]-su…
我%&&--&()&%????? 双模hashWA,unsigned long longAC,而且必须判断hash出来的数不能为0???? 我可能学了假的hash 这个题求个前缀和,然后目标是找到距离当前位置最远,且能使这两个数组差分后2-k位相同 hash把差分后数组的2到k位压起来即可,用map存这个hash值最早出现的位置 但是我还是不明白为啥hash值不能为0啊?? #include<iostream> #include<cstdio> #i…
题目链接:https://www.luogu.org/problemnew/show/P1879 状压DP. 设dp[i][j]表示第i行,状态为j的方案数 初始dp[0][0] = 1 这样一共12行12列,最多1<<12. 这样转移时,只要满足上下没有两个1,这两行分别满足没有相邻1. 加法原理转移. \(j&k==0\) $ dp[i][j] += dp[i-1][k] $ #include <cstdio> #include <cstring> #inc…
题目链接:https://www.luogu.org/problemnew/show/P3258 谁说树剖过不去会RE呢? 我今天就是要强行树剖了 树剖强艹 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define ll long long using namespace std; const int maxn = 500010; ll ans[…
题目链接:https://www.luogu.org/problemnew/show/P1606 这个题..第一问很好想,但是第二问,如果要跑最短路计数的话,零边权的花怎么办? 不如这样想,如果这个点能到花的话,那把他和从花能到的一个点边权连成一,好比两条路径共为1:一条为1一条为0的路径 但在实际操作的时候,一朵花是可以到另一朵花的! 电风扇好啊 #include <queue> #include <cstdio> #include <cstring> #includ…
题目链接:https://www.luogu.org/problemnew/show/P2936 菜 #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 10000; const int inf = 1e9; int n, m, s,…
题目链接:https://www.luogu.org/problemnew/show/P3128 菜 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define ll long long #define lson left, mid, rt<<1 #define rson mid+1, right, rt<<1|1 usin…
题目链接:https://www.luogu.org/problemnew/show/P2860 考虑在无向图上缩点. 运用到边双.桥的知识. 缩点后统计度为1的点. 度为1是有一条路径,度为2是有两条路径. #include <stack> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; cons…
题目链接:https://www.luogu.org/problemnew/show/P2397 卡空间. 对于众数出现次数 > n/2 我们考虑rand. 每次正确的概率为1/2,五个测试点,全对的概率为1/32. 所以: #include <ctime> #include <cstdio> #include <cstdlib> using namespace std; int n, m; int main() { scanf("%d",&a…
题解报告:https://www.luogu.org/problemnew/show/P2341 我们把图中的强连通分量缩点,然后只有出度为0的牛是受欢迎的,这样如果出度为0的牛只有一个,说明受所有牛欢迎.否则出度为0只是受一些牛欢迎. #include <stack> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using names…
题目链接:https://www.luogu.org/problemnew/show/P2939 本来说是双倍经验题,跟飞行路线一样的,结果我飞行路线拿deque优化SPFA过了这里过不了了. 所以多学一种优先队列优化. #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define ll long lon…