标签:tor stand character cpp ace turn any hint ast
依赖性的关系 : 就是指其后一个的状态由其前一个所决定,这种优化方法可以用在很多的地方,
例如 : 一串东西,有正有反,每次只能操作一段区间,将此区间正反颠倒,向这种模型就可以建立成依赖型的模型,改变一个区间的【a, b】的时候,实则我只要改变 a 的依赖值与 b+1 的依赖值。那么对于整个区间便进行了操作,复杂度为 0(1)。
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 bought an automatic cow turning machine. Since he purchased the discount model, it must be irrevocably preset to turn K (1 ≤ K ≤ N)cows at once, and it can only turn cows that are all standing next to each other in line. Each time the machine is used, it reverses the facing direction of a contiguous group of K cows in the line (one cannot use it on fewer than K cows, e.g., at the either end of the line of cows). Each cow remains in the same *location* as before, but ends up facing the *opposite direction*. A cow that starts out facing forward will be turned backward by the machine and vice-versa.
Because FJ must pick a single, never-changing value of K, please help him determine the minimum value of K that minimizes the number of operations required by the machine to make all the cows face forward. Also determine M, the minimum number of machine operations required to get all the cows facing forward using that value of K.
7 B B F B F B BSample Output
3 3Hint
/* * Author: ry * Created Time: 2017/10/30 16:18:08 * File Name: 1.cpp */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <time.h> using namespace std; const int eps = 1e6+5; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; #define Max(a,b) a>b?a:b #define Min(a,b) a>b?b:a #define ll long long int n; int pre[5005]; int arr[5005]; int main() { cin >> n; char ch, last = ‘F‘; getchar(); int k = 1; for(int i = 0; i < n; i++){ scanf("%c", &ch); getchar(); if (ch != last){ pre[k++] = 1; } else pre[k++] = 0; last = ch; } //for(int i = 0; i < k; i++){ //printf("%d\t", pre[i]); //} int ans = 1<<30, ans2 = 0; for(int i = 1; i <= n; i++){ memcpy(arr, pre, sizeof(pre)); int temp = 0; for(int j = 1; j <= n-i+1; j++){ if (arr[j]){ temp++; arr[j+i] ^= 1; } } for(int j = n-i+2; j <= n; j++){ if (arr[j]){ temp = 1 << 30; break; } } if (temp < ans){ ans = temp; ans2 = i; } } printf("%d %d\n", ans2, ans); return 0; }
标签:tor stand character cpp ace turn any hint ast
原文地址:http://www.cnblogs.com/ccut-ry/p/7755335.html