P2882 Face The Right Way - USACO07MAR】的更多相关文章

这道题没有一个比较详细的题解,我来提供一份. 首先我们可以知道,反转区间的顺序对结果没有影响,而且一个区间如果翻转两次以上是没有意义的,所以,问题就变成了求哪些区间需要反转. 我们枚举k.对于每一个k,我们设计一个calc函数来判断k的操作次数. 显然的,我们可以设计出一种方法,就是每一次都检查最左端,然后进行反转,很容易写出下面的calc函数. int calc(int k) { int ans = 0; int i; for(i = 1; i + k - 1 <= N; i++) { if(…
P2882 [USACO07MAR]面对正确的方式Face The Right Way $n<=5000$?枚举翻转长度,顺序模拟就ok了 对于每次翻转,我们可以利用差分的思想,再搞搞前缀和. (输出反了还交,真菜) #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define N 10010 int n,a[N],s[N],p[N],ans,mnd=1e9;…
题目描述 Farmer John has arranged his N (1 ≤ N ≤ 5,000) cows in a row and many of them are facing forward, like good cows. Some of them are facing backward, though, and he needs them all to face forward to make his life perfect. Fortunately, FJ recently…
题目描述 N头牛排成一列1<=N<=5000.每头牛或者向前或者向后.为了让所有牛都 面向前方,农夫每次可以将K头连续的牛转向1<=K<=N,求操作的最少 次数M和对应的最小K. 简单题意:给你一个01串,每次可以对长度为K的区间进行异或,求异或的最少次数以及对应的K. 解析: 这是一道有毒的模拟,我做了一个下午加加减减鼓捣半天都快吐(秃)了. 看到题第一反应二分答案,不过仔细一想,K和M并不相关,答案不具备单调性,无法二分出解. 所以,这道题其实是个模拟. 一个显而易见的贪心:从…
题目传送门 题目描述 Farmer John has arranged his N (1 ≤ N ≤ 5,000) cows in a row and many of them are facing forward, like good cows. Some of them are facing backward, though, and he needs them all to face forward to make his life perfect. Fortunately, FJ rec…
题目描述 Farmer John has arranged his N (1 ≤ N ≤ 5,000) cows in a row and many of them are facing forward, like good cows. Some of them are facing backward, though, and he needs them all to face forward to make his life perfect. Fortunately, FJ recently…
发现选定一个长度后,怎么翻转是固定的. 那我们直接选定一个长度去操作就行. 优化操作过程 类似于堆里打持久化标记一样的感觉. [USACO07MAR]Face The Right Way G // Problem: P2882 [USACO07MAR]Face The Right Way G // Contest: Luogu // URL: https://www.luogu.com.cn/problem/P2882 // Memory Limit: 125 MB // Time Limit:…
P2883 [USACO07MAR]牛交通Cow Traffic 对于每一条边$(u,v)$ 设入度为0的点到$u$有$f[u]$种走法 点$n$到$v$(通过反向边)有$f2[v]$种走法 显然经过这条边的方案数为$f[u]*f2[v]$ 两边递推处理$f$数组,然后枚举每条边取个$max$. #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namesp…
P2884 [USACO07MAR]每月的费用Monthly Expense 二分经典题 二分每个段的限制花费,顺便统计下最大段 注意可以分空段 #include<iostream> #include<cstring> #include<cstdio> using namespace std; int max(int &a,int &b){return a>b?a:b;} #define N 100001 int a[N],n,m,ans=2e9;…
P2883 [USACO07MAR]牛交通Cow Traffic 随着牛的数量增加,农场的道路的拥挤现象十分严重,特别是在每天晚上的挤奶时间.为了解决这个问题,FJ决定研究这个问题,以能找到导致拥堵现象的瓶颈所在. 牧场共有M条单向道路,每条道路连接着两个不同的交叉路口,为了方便研究,FJ将这些交叉路口编号为1..N,而牛圈位于交叉路口N.任意一条单向道路的方向一定是是从编号低的路口到编号高的路口,因此农场中不会有环型路径.同时,可能存在某两个交叉路口不止一条单向道路径连接的情况. 在挤奶时间到…