分析:非常神的一道题.迭代加深搜索+rand可以骗得20分.状压n的话只有24分,必须对问题进行一个转化.

在爆搜的过程中,可以利用差分来快速地对一个区间进行修改,把一般的差分改成异或型的差分:

b[i] = a[i] ^ a[i + 1],每次翻转操作实际上就是在b[l-1]取反,b[r]上取反.那么先对原序列建一个差分数组,实际上的操作就是在对这个差分数组进行操作:每次可以选两个数取反,问多少次能够把这个数组中的所有元素全部变成1.这是一个很神奇的转化.

每次取反的两个数长度都是固定的.两种情况:1.将1,0取反. 2.将0,0取反.其实第一种操作完全是不必要考虑的,因为这种情况下就是把0挪到了另一个位置罢了.对于第二种操作,可以理解为把这两个0给“消”去.那么题目就变成了最少用多少次操作能够把所有的0给消掉.每一次操作都会把0移动固定的位置,只有当两个0的位置重叠,这两个0才会被消掉。

问题还可以接着转化:有n个物品,每次取出还没有被消掉的两个物品,将它们消掉,问最小的代价.k非常小,这就是典型的状压dp了,和noip2016愤怒的小鸟差不多,只不过那道题可以消掉多个.先对每个0求到其他能到达的位置的最短路(最小代价),f[i]表示i这个状态的最小代价,如果i上的第j位为1,说明第j个0已经被消掉了,状态转移方程很明显,每次找两个没消掉的消就好了,具体见代码.

20分暴力:

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int n, k, m, a[], b[], cha[], dep = , vis[][], temp[];
  11. bool flag = false;
  12.  
  13. bool check()
  14. {
  15. memcpy(temp, cha, sizeof(cha));
  16. for (int i = ; i <= n; i++)
  17. {
  18. temp[i] += temp[i - ];
  19. if ((temp[i] + a[i]) % != )
  20. return false;
  21. }
  22. return true;
  23. }
  24.  
  25. void dfs(int d)
  26. {
  27. if (d == dep + )
  28. {
  29. if (check())
  30. flag = ;
  31. return;
  32. }
  33. for (int i = ; i <= m; i++)
  34. for (int j = ; j + b[i] - <= n; j++)
  35. if (!vis[i][j])
  36. {
  37. cha[j]++;
  38. cha[j + b[i]]--;
  39. vis[i][j] = ;
  40. dfs(d + );
  41. cha[j]--;
  42. cha[j + b[i]]++;
  43. vis[i][j] = ;
  44. }
  45. }
  46.  
  47. int main()
  48. {
  49. scanf("%d%d%d", &n, &k, &m);
  50. for (int i = ; i <= n; i++)
  51. a[i] = ;
  52. for (int i = ; i <= k; i++)
  53. {
  54. int t;
  55. scanf("%d", &t);
  56. a[t] = ;
  57. }
  58. for (int i = ; i <= m; i++)
  59. scanf("%d", &b[i]);
  60. if (n <= )
  61. {
  62. for (;; dep++)
  63. {
  64. dfs();
  65. if (flag)
  66. {
  67. printf("%d\n", dep);
  68. break;
  69. }
  70. }
  71. }
  72. else
  73. {
  74. srand(time());
  75. cout << rand() % << endl;
  76. }
  77.  
  78. return ;
  79. }

std:

  1. #include <cmath>
  2. #include <queue>
  3. #include <vector>
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <iostream>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10. const int inf = 0x7ffffff;
  11.  
  12. typedef long long ll;
  13.  
  14. int n, k, m, b[], a[], cnt, d[][], f[ << ];
  15. pair <int, int>p[];
  16. queue <int> q;
  17.  
  18. void bfs(pair <int, int> s)
  19. {
  20. for (int i = ; i < ; i++)
  21. d[s.first][i] = inf;
  22. q.push(s.second);
  23. d[s.first][s.second] = ;
  24. while (!q.empty())
  25. {
  26. int x = q.front();
  27. q.pop();
  28. for (int i = ; i <= m; i++)
  29. {
  30. if (x - b[i] >= && d[s.first][x - b[i]] > d[s.first][x] + )
  31. {
  32. d[s.first][x - b[i]] = d[s.first][x] + ;
  33. q.push(x - b[i]);
  34. }
  35. if (x + b[i] <= n && d[s.first][x + b[i]] > d[s.first][x] + )
  36. {
  37. d[s.first][x + b[i]] = d[s.first][x] + ;
  38. q.push(x + b[i]);
  39. }
  40. }
  41. }
  42. }
  43.  
  44. int solve(int statuu)
  45. {
  46. if (f[statuu] != -)
  47. return f[statuu];
  48. if (statuu == )
  49. return ;
  50. int &ret = f[statuu];
  51. ret = inf;
  52. for (int i = ; i < * k; i++)
  53. if (statuu & ( << i))
  54. for (int j = ; j < * k; j++)
  55. if (statuu & ( << j) && j != i)
  56. ret = min(ret, solve(statuu ^ ( << j) ^ ( << i)) + d[j][p[i].second]);
  57. return ret;
  58. }
  59.  
  60. int main()
  61. {
  62. scanf("%d%d%d", &n, &k, &m);
  63. for (int i = ; i <= k; i++)
  64. {
  65. int t;
  66. scanf("%d", &t);
  67. a[t] = ;
  68. }
  69. for (int i = ; i <= m; i++)
  70. scanf("%d", &b[i]);
  71. for (int i = ; i <= n; i++)
  72. if (a[i] != a[i + ]) //实质上就是差分
  73. {
  74. p[cnt] = make_pair(cnt, i);
  75. cnt++;
  76. }
  77. for (int i = ; i < cnt; i++)
  78. bfs(p[i]);
  79. memset(f, -, sizeof(f));
  80. printf("%d\n", solve(( << cnt) - ));
  81.  
  82. return ;
  83. }

noip模拟赛 星空的更多相关文章

  1. NOIP模拟赛20161022

    NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...

  2. contesthunter暑假NOIP模拟赛第一场题解

    contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...

  3. NOIP模拟赛 by hzwer

    2015年10月04日NOIP模拟赛 by hzwer    (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...

  4. 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程

    数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...

  5. 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...

  6. 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...

  7. 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...

  8. CH Round #58 - OrzCC杯noip模拟赛day2

    A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...

  9. CH Round #52 - Thinking Bear #1 (NOIP模拟赛)

    A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...

随机推荐

  1. 嵌套查询--------关联一对多关系----------collection

    参考来源:   http://www.cnblogs.com/LvLoveYuForever/p/6689577.html <resultMap id="BaseResultMap&q ...

  2. vue watch监听对象及对应值的变化

    data:{ a:1, b:{ value:1, type:1, } }, watch:{ a(val, oldVal){//普通的watch监听 console.log("a: " ...

  3. ORA-14074: partition bound must collate higher than that of the last partition

    There is a error happen in crotab: CREATE parttion report ORA-14074:ORA-14074: partition bound must ...

  4. [转]Using the Interop Activity in a .NET Framework 4 Workflow

    本文转自:http://msdn.microsoft.com/en-us/library/ee264174(v=vs.100).aspx This topic applies to Windows W ...

  5. 12 DOM操作应用

    1.创建子元素oLi=document.creatElement('li') 2.将元素附给父级元素oUl.appendChild(oLi) 3.将元素插入到父级元素里的第一位子元素之前oUl.ins ...

  6. Scala基础篇-02函数与代码块

    1.block 代码块也是表达式,其最终求得的值是最后一个表达式的值. {exp1;exp2} { exp1 exp2 } 2.function def funtionName(param:Param ...

  7. iOS Programming Dynamic Type 2

    iOS Programming Dynamic Type  2       You will need to update two parts of this view controller for ...

  8. 手机酷派4G5316 5313s 黑砖 求转成功 9008端口 9006端口 少走弯路选对镜像

    首先要有资料 里面有教程  http://pan.baidu.com/s/1bpjxP6n 1.用其他手机 or u 盘往sd卡放进“强制进入下载模式的文件” 2. 驱动 3.刷机工具 下载镜像   ...

  9. Java多线程编程核心技术---Lock的基本概念和使用

    Lock接口: ReentrantLock的基本功能: ReentrantLock的lock和unlock方法进行加锁,解锁.可以起到和synchronized关键字一样的效果: 选择性通知!!!: ...

  10. ES6特性的两点分析

    块级作用域声明let.constES6中const 和let的功能,转换为ES5之后,我们会发现实质就是在块级作用改变一下变量名,使之与外层不同.ES6转换前: let a1 = 1; let a2 ...