2019 TCO Round 1B——[ 状压DP ]
第一题是 EllysSki 。
题意:给n个数,求两个方向的最长递减区间。
可以O(n)。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
int Mx(int a,int b){return a>b?a:b;} class EllysSki{
public:
int getMax(vector <int> height);
}; int EllysSki::getMax(vector <int> height)
{
int n=height.size(),ans=;
for(int i=;i<n;i++)
{
int j=i;
while(j+<n&&height[j+]<=height[j])
j++;
ans=Mx(ans,j-i+); i=j;
}
for(int i=n-;i>=;i--)
{
int j=i;
while(j&&height[j-]<=height[j])
j--;
ans=Mx(ans,i-j+); i=j;
}
return ans;
}
第二题是 EllysTeleport 。
题意:连边,找最长链。
自己写了tarjan,没调出来。其实 n2 暴力也行。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
int Mx(int a,int b){return a>b?a:b;}
int Mn(int a,int b){return a<b?a:b;}
const int N=1e4+;
int n,h[N],to[N]; bool vis[N];
class EllysTeleport{
int fnd(int x)
{
int l=,r=n,ret=;
while(l<=r)
{
int mid=l+r>>;
if(h[mid]<=x)ret=mid,l=mid+;
else r=mid-;
}
return ret;
}
public:
int getMax(int N, int H1, int A, int B, int P, int Q, int M)
{
n=N; h[]=H1;
for(int i=;i<=n;i++)
h[i]=((ll)h[i-]*A+B)%M;
sort(h+,h+n+);
for(int i=;i<=n;i++)
{
int d=((ll)h[i]*P+Q)%M;
to[i]=fnd(d);
}
int ans=;
for(int i=;i<=n;i++)
{
memset(vis,,sizeof vis);
int cnt=,cr=to[i]; vis[i]=;
while(cr&&!vis[cr])
{
cnt++;vis[cr]=;cr=to[cr];
}
ans=Mx(ans,cnt);
}
return ans;
}
};
第三题是 EllysPearls 。
题意:n(<=50) 个球排成一排,每个球是 m(<=15) 种颜色之一。一次操作可以拿出序列中任意一个球,再插进序列的任意一个位置。求最少操作次数使得相同颜色的球是一个区间。
题解:https://www.topcoder.com/blog/2019-topcoder-open-algorithm-round-1b-editorials/
颜色只有 15 ,考虑状压。 1 表示该颜色之前已经确定了一个位置,即当前球必须取出、融入之前自己颜色的区间。
特殊情况就是当前球不动,就能合法;所以再记目前最后的颜色是什么(可以是空)。
还可能遇到当前球,但是把它放在后面,即尚不确定当前颜色。发现这种情况和“融入之前自己颜色的区间”都是要花费1的代价,并且不改变状压的状态。
所以 dp[ i ][ j ][ s ] 表示前 i 个、末尾颜色是 j 、已经确定位置的颜色集合是 s ,即可转移。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=,K=,M=(<<)+;
int n,m,a[N],bin[K],dp[][K][M];
class EllysPearls{
void cz(int &x,int y){if(y<x)x=y;}
public:
int getMin(int tN, int tM, vector <int> pearls)
{
n=tN; m=tM; for(int i=;i<n;i++)a[i+]=pearls[i];
bin[]=;for(int i=;i<=m;i++)bin[i]=bin[i-]<<;
memset(dp[],0x3f,sizeof dp[]);
dp[][][]=; bool u=,v=;
for(int i=;i<=n;i++,swap(u,v))
{
memset(dp[v],0x3f,sizeof dp[v]);
for(int j=;j<=m;j++)
for(int s=;s<bin[m];s++)//[m]not[n]
{
int tp=dp[u][j][s]; if(tp>N)continue;
int d=bin[a[i]-];
cz(dp[v][j][s],tp+);
if(!j||j==a[i])
cz(dp[v][a[i]][s|d],tp);
if(!(s&d))
cz(dp[v][a[i]][s|d],tp);
}
}
int ans=N;
for(int j=;j<=m;j++)
for(int s=;s<bin[m];s++)
cz(ans,dp[u][j][s]);
return ans;
}
};
2019 TCO Round 1B——[ 状压DP ]的更多相关文章
- MMM 状压dp学习记
状压dp学习记 by scmmm 开始日期 2019/7/17 前言 状压dp感觉很好理解(本质接近于爆搜但是又有广搜的感觉),综合了dp的高效性(至少比dfs,bfs优),又能解决普通dp难搞定的问 ...
- [多校联考2019(Round 5 T1)] [ATCoder3912]Xor Tree(状压dp)
[多校联考2019(Round 5)] [ATCoder3912]Xor Tree(状压dp) 题面 给出一棵n个点的树,每条边有边权v,每次操作选中两个点,将这两个点之间的路径上的边权全部异或某个值 ...
- Codeforces Round #363 LRU(概率 状压DP)
状压DP: 先不考虑数量k, dp[i]表示状态为i的概率,状态转移方程为dp[i | (1 << j)] += dp[i],最后考虑k, 状态表示中1的数量为k的表示可行解. #incl ...
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp
题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...
- 状压dp Codeforces Beta Round #8 C
http://codeforces.com/contest/8/problem/C 题目大意:给你一个坐标系,给你一个人的目前的坐标(该坐标也是垃圾桶的坐标),再给你n个垃圾的坐标,这个人要捡完所有的 ...
- Educational Codeforces Round 13 E. Another Sith Tournament 状压dp
E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes(状压dp)
http://codeforces.com/contest/580/problem/D 题意: 有个人去餐厅吃饭,现在有n个菜,但是他只需要m个菜,每个菜只吃一份,每份菜都有一个欢乐值.除此之外,还有 ...
- Codeforces Beta Round #8 C. Looking for Order 状压dp
题目链接: http://codeforces.com/problemset/problem/8/C C. Looking for Order time limit per test:4 second ...
- 【loj6177】「美团 CodeM 初赛 Round B」送外卖2 Floyd+状压dp
题目描述 一张$n$个点$m$条边的有向图,通过每条边需要消耗时间,初始为$0$时刻,可以在某个点停留.有$q$个任务,每个任务要求在$l_i$或以后时刻到$s_i$接受任务,并在$r_i$或以前时刻 ...
随机推荐
- Codechef BINOMSUM
题意:(复制sunset的)有\(T\)天,每天有\(K\)个小时,第\(i\)天有\(D+i−1\)道菜,第一个小时你选择\(L\)道菜吃,接下来每个小时你可以选择吃一道菜或者选择\(A\)个活动中 ...
- get the deadlock information from sql server
https://stackoverflow.com/questions/12422986/sql-query-to-get-the-deadlocks-in-sql-server-2008 You c ...
- 用 Flask 来写个轻博客 (20) — 实现注册表单与应用 reCAPTCHA 来实现验证码
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 reCAPTCHA 应用 reCAPTCHA 前文列表 用 Flask ...
- swoole webSocket 聊天室示例
swoole1.7.9增加了内置的WebSocket服务器支持,通过几行PHP代码就可以写出一个异步非阻塞多进程的WebSocket服务器. 基于swoole websocket的用户上下线通知,在线 ...
- c++全局变量,局部变量,内存布局,默认初始化
全局变量 定义在所有函数之外的变量,main函数之内的变量也是局部变量,Globle variable 未显示初始化时执行默认初始化 局部变量 定义在函数之内的变量,Local variable 未 ...
- CRF条件随机场在机器视觉中的解释
CRF是一种判别模型,本质是给定观察值集合的马尔科夫随机场(MRF),而MRF是加了马尔科夫性质限制的随机场. 马尔科夫性质:全局.局部.成对 随机场:看做一组随机变量的集合(对应于同一个样本空间), ...
- HDU 1398 Square Coins(DP)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 45.Sort List(链表排序)
Level: Medium 题目描述: Sort a linked list in O(n log n) time using constant space complexity. Example ...
- 使用批处理发布 QT 的程序
1. 将 QT Creator 生成的 exe 文件拷贝到一个目录中 :C:\Users\zyy\Desktop\qtrelease 2.新建 bat 文件:qt.bat . 编辑文件,注意替换 QT ...
- adb]ADB server didn't ACK
遇到上述问题 此时由于不正常退出 会在进程中遗留Android debug进程 ,需要强制删除