noip模拟赛 星空
分析:非常神的一道题.迭代加深搜索+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分暴力:
- #include <cstdio>
- #include <cstdlib>
- #include <ctime>
- #include <cstring>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int n, k, m, a[], b[], cha[], dep = , vis[][], temp[];
- bool flag = false;
- bool check()
- {
- memcpy(temp, cha, sizeof(cha));
- for (int i = ; i <= n; i++)
- {
- temp[i] += temp[i - ];
- if ((temp[i] + a[i]) % != )
- return false;
- }
- return true;
- }
- void dfs(int d)
- {
- if (d == dep + )
- {
- if (check())
- flag = ;
- return;
- }
- for (int i = ; i <= m; i++)
- for (int j = ; j + b[i] - <= n; j++)
- if (!vis[i][j])
- {
- cha[j]++;
- cha[j + b[i]]--;
- vis[i][j] = ;
- dfs(d + );
- cha[j]--;
- cha[j + b[i]]++;
- vis[i][j] = ;
- }
- }
- int main()
- {
- scanf("%d%d%d", &n, &k, &m);
- for (int i = ; i <= n; i++)
- a[i] = ;
- for (int i = ; i <= k; i++)
- {
- int t;
- scanf("%d", &t);
- a[t] = ;
- }
- for (int i = ; i <= m; i++)
- scanf("%d", &b[i]);
- if (n <= )
- {
- for (;; dep++)
- {
- dfs();
- if (flag)
- {
- printf("%d\n", dep);
- break;
- }
- }
- }
- else
- {
- srand(time());
- cout << rand() % << endl;
- }
- return ;
- }
std:
- #include <cmath>
- #include <queue>
- #include <vector>
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- const int inf = 0x7ffffff;
- typedef long long ll;
- int n, k, m, b[], a[], cnt, d[][], f[ << ];
- pair <int, int>p[];
- queue <int> q;
- void bfs(pair <int, int> s)
- {
- for (int i = ; i < ; i++)
- d[s.first][i] = inf;
- q.push(s.second);
- d[s.first][s.second] = ;
- while (!q.empty())
- {
- int x = q.front();
- q.pop();
- for (int i = ; i <= m; i++)
- {
- if (x - b[i] >= && d[s.first][x - b[i]] > d[s.first][x] + )
- {
- d[s.first][x - b[i]] = d[s.first][x] + ;
- q.push(x - b[i]);
- }
- if (x + b[i] <= n && d[s.first][x + b[i]] > d[s.first][x] + )
- {
- d[s.first][x + b[i]] = d[s.first][x] + ;
- q.push(x + b[i]);
- }
- }
- }
- }
- int solve(int statuu)
- {
- if (f[statuu] != -)
- return f[statuu];
- if (statuu == )
- return ;
- int &ret = f[statuu];
- ret = inf;
- for (int i = ; i < * k; i++)
- if (statuu & ( << i))
- for (int j = ; j < * k; j++)
- if (statuu & ( << j) && j != i)
- ret = min(ret, solve(statuu ^ ( << j) ^ ( << i)) + d[j][p[i].second]);
- return ret;
- }
- int main()
- {
- scanf("%d%d%d", &n, &k, &m);
- for (int i = ; i <= k; i++)
- {
- int t;
- scanf("%d", &t);
- a[t] = ;
- }
- for (int i = ; i <= m; i++)
- scanf("%d", &b[i]);
- for (int i = ; i <= n; i++)
- if (a[i] != a[i + ]) //实质上就是差分
- {
- p[cnt] = make_pair(cnt, i);
- cnt++;
- }
- for (int i = ; i < cnt; i++)
- bfs(p[i]);
- memset(f, -, sizeof(f));
- printf("%d\n", solve(( << cnt) - ));
- return ;
- }
noip模拟赛 星空的更多相关文章
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
- NOIP模拟赛 by hzwer
2015年10月04日NOIP模拟赛 by hzwer (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
- CH Round #52 - Thinking Bear #1 (NOIP模拟赛)
A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...
随机推荐
- 嵌套查询--------关联一对多关系----------collection
参考来源: http://www.cnblogs.com/LvLoveYuForever/p/6689577.html <resultMap id="BaseResultMap&q ...
- vue watch监听对象及对应值的变化
data:{ a:1, b:{ value:1, type:1, } }, watch:{ a(val, oldVal){//普通的watch监听 console.log("a: " ...
- 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 ...
- [转]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 ...
- 12 DOM操作应用
1.创建子元素oLi=document.creatElement('li') 2.将元素附给父级元素oUl.appendChild(oLi) 3.将元素插入到父级元素里的第一位子元素之前oUl.ins ...
- Scala基础篇-02函数与代码块
1.block 代码块也是表达式,其最终求得的值是最后一个表达式的值. {exp1;exp2} { exp1 exp2 } 2.function def funtionName(param:Param ...
- iOS Programming Dynamic Type 2
iOS Programming Dynamic Type 2 You will need to update two parts of this view controller for ...
- 手机酷派4G5316 5313s 黑砖 求转成功 9008端口 9006端口 少走弯路选对镜像
首先要有资料 里面有教程 http://pan.baidu.com/s/1bpjxP6n 1.用其他手机 or u 盘往sd卡放进“强制进入下载模式的文件” 2. 驱动 3.刷机工具 下载镜像 ...
- Java多线程编程核心技术---Lock的基本概念和使用
Lock接口: ReentrantLock的基本功能: ReentrantLock的lock和unlock方法进行加锁,解锁.可以起到和synchronized关键字一样的效果: 选择性通知!!!: ...
- ES6特性的两点分析
块级作用域声明let.constES6中const 和let的功能,转换为ES5之后,我们会发现实质就是在块级作用改变一下变量名,使之与外层不同.ES6转换前: let a1 = 1; let a2 ...