第一题是 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 ]的更多相关文章

  1. MMM 状压dp学习记

    状压dp学习记 by scmmm 开始日期 2019/7/17 前言 状压dp感觉很好理解(本质接近于爆搜但是又有广搜的感觉),综合了dp的高效性(至少比dfs,bfs优),又能解决普通dp难搞定的问 ...

  2. [多校联考2019(Round 5 T1)] [ATCoder3912]Xor Tree(状压dp)

    [多校联考2019(Round 5)] [ATCoder3912]Xor Tree(状压dp) 题面 给出一棵n个点的树,每条边有边权v,每次操作选中两个点,将这两个点之间的路径上的边权全部异或某个值 ...

  3. Codeforces Round #363 LRU(概率 状压DP)

    状压DP: 先不考虑数量k, dp[i]表示状态为i的概率,状态转移方程为dp[i | (1 << j)] += dp[i],最后考虑k, 状态表示中1的数量为k的表示可行解. #incl ...

  4. 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 ...

  5. 状压dp Codeforces Beta Round #8 C

    http://codeforces.com/contest/8/problem/C 题目大意:给你一个坐标系,给你一个人的目前的坐标(该坐标也是垃圾桶的坐标),再给你n个垃圾的坐标,这个人要捡完所有的 ...

  6. Educational Codeforces Round 13 E. Another Sith Tournament 状压dp

    E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...

  7. Codeforces Round #321 (Div. 2) D. Kefa and Dishes(状压dp)

    http://codeforces.com/contest/580/problem/D 题意: 有个人去餐厅吃饭,现在有n个菜,但是他只需要m个菜,每个菜只吃一份,每份菜都有一个欢乐值.除此之外,还有 ...

  8. 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 ...

  9. 【loj6177】「美团 CodeM 初赛 Round B」送外卖2 Floyd+状压dp

    题目描述 一张$n$个点$m$条边的有向图,通过每条边需要消耗时间,初始为$0$时刻,可以在某个点停留.有$q$个任务,每个任务要求在$l_i$或以后时刻到$s_i$接受任务,并在$r_i$或以前时刻 ...

随机推荐

  1. python打印9宫格,25宫格等奇数格,且横竖斜相加和相等

    代码如下: #!/usr/bin/env python3#-*- coding:utf-8 -*-num = int(input('请输入一个奇数:'))# 定义一个长为num的列表high = [[ ...

  2. linux配置防火墙 Centos7下 添加 端口白名单

    最近在阿里云服务器centos7上部署项目 要开启8484端口 , CentOS 7默认使用的是firewall作为防火墙 在firewall下开启端口白名单 1.查看下防火墙的状态:systemct ...

  3. Vue2.0---webpack打包知识点-1

    打包上线或者将项目做成产品的肯定不希望暴露自己源码 在config的index.js中将productionGzip设置为false即可.(使之不生成.map文件). 对Vue-cli的webpack ...

  4. python 简易计算器

    import tkinter import tkinter.messagebox import math ''' 波波版计算器可实现的功能 1.能进行简单的加减惩处 2.能进行开根号操作 3.能进行后 ...

  5. 微信小程序发送红包功能。填坑记录

    微信官方文档 1.开通条件 (1)商户号已入驻90日 (2)商户号有连续30天正常交易 (3)只有企业资质的商户才有资格申请 2.注意事项 (1)目前小程序红包仅支持用户微信扫码打开小程序 (2)小程 ...

  6. rafy使用笔记

    1.rafy里实体字段string类型映射成数据库varchar(2000). 解决:“DomainApp“下“OnRuntimeStarting“方法里添加“DbMigrationSettings. ...

  7. NOIP2015D1T2 信息传递

    题目描述 有 n 个同学(编号为 1 到 n )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i 的同学的信息传递对象是编号为 Ti​ 的同学. 游戏开始时,每人都只 ...

  8. SVD和SVD++

    参考自:http://blog.csdn.net/wjmishuai/article/details/71191945 http://www.cnblogs.com/Xnice/p/4522671.h ...

  9. matplotlib系列——饼图

    import matplotlib.pyplot as plt import numpy as np import matplotlib import sys 1.主体函数 #饼图 def die(l ...

  10. SpringBoot-技术专区-配置文件加密

    工程中的配置文件如果把数据库的用户名密码写成明文的话是一件很危险的事情,之前也看见网上说可以对密码进行加密,用的时候再解密,因此今天我就尝试如何在spring boot 中的项目中实现关键信息的加密解 ...