P2866 [USACO06NOV]糟糕的一天Bad Hair Day 75通过 153提交 题目提供者洛谷OnlineJudge 标签USACO2006云端 难度普及/提高- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 题目标题 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her…
题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads. Each cow i has a specified…
题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads. Each cow i has a specified…
https://www.luogu.org/problem/show?pid=2866 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of o…
链接:https://ac.nowcoder.com/acm/contest/984/A 来源:牛客网 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the…
传送门 这是一道典型的单调栈. 题意理解 先来理解一下题意(原文翻译得有点问题). 其实就是求对于序列中的每一个数i,求出i到它右边第一个大于i的数之间的数字个数c[i].最后求出和. 首先可以暴力求解,时间复杂度o(n^2)显然TLE. 然后就是用单调栈来做. 单调栈 单调栈就是维护一个栈,使得栈中的元素是单调的(递增/递减). 假设是递减——对于每一个新来的元素,把栈顶大于这个元素的每一个数字全部弹出,最后把这个元素加进去. (如果栈为空,直接加入) 单调栈有什么用呢? 单调递增栈能以o(n…
[题意概述] 给出一个长度为n的序列a,求有多少对[i,j]满足i<j且a[i]>max(a[i+1],a[i+2],...,a[j]). [题解] 单调栈. 倒着处理序列的元素,维护一个单调递减的栈,同时记录栈中的每个元素进栈的时间.如果当前元素x大于栈顶元素,那么栈顶到第二个元素在原素列之间的这一段就都是可以被x看见的,答案加上time[top]-time[top-1]. #include<cstdio> #include<algorithm> #include&l…
看到这道题很容易想到单调栈,但我一开始想的是从后往前扫,但发现会有问题(因为这样会对后面牛的答案造成影响),所以这时我们要及时换一个思路,从前往后扫. 维护一个单调递减的栈,插入h[i]时,小等于它的数都要出栈,累加栈中元素数量,表示的意义就是:当前栈中的牛都是可以看到i这头牛的,即他们不会被i这头牛挡住. 1 #include<bits/stdc++.h> 2 using namespace std; 3 long long ans,h[80010]; 4 int s[80010],top,…
P2866 [USACO06NOV]糟糕的一天Bad Hair Day 题意翻译 农夫约翰有N (N \leq 80000)N(N≤80000)头奶牛正在过乱头发节.每一头牛都站在同一排面朝东方,而且每一头牛的身高为h_ihi​.第NN头牛在最前面,而第11头牛在最后面. 对于第ii头牛前面的第jj头牛,如果h_i>h_{i+1}hi​>hi+1​并且h_i>h_{i+2}hi​>hi+2​ \cdots⋯ h_i>h_jhi​>hj​,那么认为第ii头牛可以看到第i+…
P2866 [USACO06NOV]糟糕的一天Bad Hair Day 奶牛题里好多单调栈..... 维护一个单调递减栈,存每只牛的高度和位置,顺便统计一下答案. #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define N 80010 int n,tp,q[N],h[N]; long long ans; int main(){ scanf("%d&qu…
P2866 [USACO06NOV]糟糕的一天Bad Hair Day 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cow…
洛谷P1879 [USACO06NOV]玉米田Corn Fields \(f[i][j]\) 表示前 \(i\) 行且第 \(i\) 行状态为 \(j\) 的方案总数.\(j\) 的大小为 \(0 \to (1 >> n - 1)\) . 第 \(i\) 行,种植状态为 \(j\) 的方案总数等于所有合法的 \(f[i-1][k]\) 之和. 状态 \(j\) 满足同一行内没有相邻的两块草地(没有共同边). 状态 \(j\) 和 \(k\) 满足相邻两行的种植情况没有两块草地有共同边. \[…
洛谷 P6218 [USACO06NOV] Round Numbers S 题目描述 如果一个正整数的二进制表示中,\(0\) 的数目不小于 \(1\) 的数目,那么它就被称为「圆数」. 例如,\(9\) 的二进制表示为 \(10011001\),其中有 \(2\) 个 \(0\) 与 \(2\) 个 \(1\).因此,\(9\) 是一个「圆数」. 请你计算,区间 \([l,r]\) 中有多少个「圆数」. 输入格式 一行,两个整数 \(l,r\). 输出格式 一行,一个整数,表示区间 \([l,…
洛谷P1879:https://www.luogu.org/problemnew/show/P1879 思路 把题目翻译成人话 在n*m的棋盘 每个格子不是0就是1 1表示可以种 0表示不能种 相邻的格子不能同时种 求总方案数 把每行看成一个n位的2进制数 预处理出每行的状态后 进行DP即可 PS:在处理每行的初始状态时 将其取反后更好操作 代码 #include<iostream> #include<cstdio> using namespace std; #define mod…
P1879 [USACO06NOV]玉米田Corn Fields 题目描述 农场主\(John\)新买了一块长方形的新牧场,这块牧场被划分成\(M\)行\(N\)列\((1 ≤ M ≤ 12; 1 ≤ N ≤ 12)\),每一格都是一块正方形的土地.\(John\)打算在牧场上的某几格里种上美味的草,供他的奶牛们享用. 遗憾的是,有些土地相当贫瘠,不能用来种草.并且,奶牛们喜欢独占一块草地的感觉,于是\(John\)不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边. \(John\)想知…
P2867 [USACO06NOV]大广场Big Square 题目描述 Farmer John's cows have entered into a competition with Farmer Bob's cows. They have drawn lines on the field so it is a square grid with N × N points (2 ≤ N ≤ 100), and each cow of the two herds has positioned he…
P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided t…
P2867 [USACO06NOV]大广场Big Square 题目描述 Farmer John's cows have entered into a competition with Farmer Bob's cows. They have drawn lines on the field so it is a square grid with N × N points (2 ≤ N ≤ 100), and each cow of the two herds has positioned he…
没学状压DP的看一下 合法布阵问题  P1879 [USACO06NOV]玉米田Corn Fields 题意:给出一个n行m列的草地(n,m<=12),1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多少种放法. 分析:假如我们知道每行都有x种合法放法(也就是x种状态),所以对于第i行就有x种放法,那么对于第i+1行的每种放法就有对应的x种放法. 所以定义dp[i][j]表示第i行状态为j时的方法数(j=0,j<=x;j++),有转移方程:dp[i][j]…
P1879 [USACO06NOV]玉米田Corn Fields 题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the…
单调栈版子 #include<cstdio> #include<cstring> using namespace std; ; ,zh[N]; int read(){ ; char ch=getchar(); ') ch=getchar(); '){ sum=sum*+ch-'; ch=getchar(); } return sum; } int main(){ ; scanf("%d",&n); ;i<=n;i++){ a=read(); whi…
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't b…
题目大意:有一个$n\times m$的矩阵,$(1 \leq m \leq 12; 1 \leq n \leq 12)$,想在其中的一些格子中种草,一些格子不能种草,且两块草地不相邻.问有多少种种植方案. 题解:状压$DP$,$f_{i,j}$表示处理到了第$i$行,当前状态为$j$的方案数 卡点:无 C++ Code: #include <cstdio> using namespace std; const int mod = 100000000; int n, m, a; int p[1…
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't b…
题意:给你一个序列,问将序列倒过来后,对于每个点,在再碰到第一个比它大的点之前,有多少比它小的? 求出比它小的个数的和 样例: 610374122 output: 5 倒序后:2    12    4    7    3   10   6 答案:   0     1     0     1    0     3    0 因此最终输出1+1+3=5 虽然是单调栈裸题(打完暴力才刚看出来) 不过我还是坚持写了暴力(明明是刚开始没思路) #include<cstdio> #include<i…
·求1到n的严格次短路. [题解] dijktra魔改?允许多次入队,改了次短路的值也要入队. #include<cstdio> #include<algorithm> #define M 200010 #define N 5010 #define rg register using namespace std; int n,m,tot,last[N],dis[N],d2[N],pos[N]; struct edge{ int to,pre,dis; }e[M]; struct h…
http://poj.org/problem?id=3255 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15680   Accepted: 5510 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get…
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't b…
给一手链接 https://www.luogu.com.cn/problem/P2865 这道题其实就是在维护最短路的时候维护一下次短路就okay了 #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> using namespace std; ,inf=1e9; int read(){ ,f=,c=getchar(); ; c…
一个次短路的问题,可以套用dijkstra求最短路的方法,用dis[0][i]表示最短路:dis[1][i]表示次短路,优先队列中存有最短路和次短路,然后每次找到一条道路对他进行判断,更新最短或次短路, 注意求次短路时不要打标记,因为有可能再次访问到该节点. 最后的答案就是dis[1][n]. 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define N 200010 4 int tot,head[N],to[N],w[N],nxt[…