经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
BZOJ1802: [Ahoi2009]checker(性质分析 dp)
来源:cnblogs  作者:自为风月马前卒  时间:2019/1/28 9:45:33  对本文有异议

题意

题目链接

Sol

一个不太容易发现但是又很显然的性质:

如果有两个相邻的红格子,那么第一问答案为0, 第二问可以推

否则第一问答案为偶数格子上的白格子数,第二问答案为偶数格子上的红格子数

  1. #include<bits/stdc++.h>
  2. #define Pair pair<int, int>
  3. #define MP(x, y) make_pair(x, y)
  4. #define fi first
  5. #define se second
  6. //#define int long long
  7. #define LL long long
  8. #define Fin(x) {freopen(#x".in","r",stdin);}
  9. #define Fout(x) {freopen(#x".out","w",stdout);}
  10. using namespace std;
  11. const int MAXN = 1001, mod = 1e9 + 7, INF = 1e9 + 10;
  12. const double eps = 1e-9;
  13. template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
  14. template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
  15. template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
  16. template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
  17. template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
  18. template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
  19. template <typename A> inline void debug(A a){cout << a << '\n';}
  20. template <typename A> inline LL sqr(A x){return 1ll * x * x;}
  21. inline int read() {
  22. char c = getchar(); int x = 0, f = 1;
  23. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  24. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  25. return x * f;
  26. }
  27. int N, a[MAXN];
  28. LL f[MAXN];
  29. signed main() {
  30. N = read();
  31. int ans[2] = {0, 0}, flag = 0;
  32. memset(f, 0x3f, sizeof(f));
  33. for(int i = 1; i <= N; i++) {
  34. a[i] = read();
  35. if(i > 2 && a[i] && a[i] == a[i - 1]) flag = 1;
  36. if((!(i & 1))) ans[a[i]]++;
  37. if(a[i]) f[i] = 1;
  38. }
  39. if(!flag) {printf("%d\n%d", ans[0], ans[1]); return 0;}
  40. for(int i = 2; i < N; i++) {
  41. if(a[i] && a[i + 1]) {
  42. for(int j = i - 1; j > 1; j--) chmin(f[j], f[j + 1] + f[j + 2]);
  43. for(int j = i + 2; j < N; j++) chmin(f[j], f[j - 1] + f[j - 2]);
  44. }
  45. }
  46. LL out = 0;
  47. for(int i = 2; i < N; i += 2) out += f[i];
  48. cout << 0 << "\n" << out;
  49. return 0;
  50. }
  51. /*
  52. 5
  53. 0 0 1 1 0
  54. */

原文链接:http://www.cnblogs.com/zwfymqz/p/10321697.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号