Codeforces Round #198 (Div. 1) B,C 动态规划
比赛时,开了大号去做,算了半天发现不会做A,囧。于是跑去看B,发现很水?于是很快敲完了,但是A不会,没敢交。于是去看C,一直找规律啊,后来总算调了出来,看了一下榜,发现还是算了吧,直接去睡觉了。第二天一起床把代码一交,居然A了,发现交的话rating还能涨一点,囧。
B:其实就是求一个最长不下降子序列的长度。注意到数据范围,使用二分的方式求解。
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ /******** program ********************/ const int MAXN = 100005; int a[MAXN];
int dp[MAXN];
int q[MAXN];
int n; void cc(){
int top = 0;
rep1(i,n){
if(top==0||dp[top]<a[i]){
dp[++top] = a[i];
continue;
}
int l = 1 , r = top;
int ans = 1;
while(l<=r){
int mid = (l+r)>>1;
if(dp[mid]<a[i])
l = mid+1;
else{
ans = mid;
r = mid-1;
}
}
dp[ans] = a[i];
}
cout<<top<<endl;
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif while(cin>>n){
rep1(i,n)
RD(a[i]);
cc();
} return 0;
}
C:注意变与不变的部分。
假设现在有位置:1,2,3,4,5,6
需要填入的数为:1,2,3,4,7,8
则可以分成两个部分:右边的可以匹配任意位置。
位置:1,2,3,4 5,6
数: 1,2,3,4 7,8
1.当左边的数1匹配上左边时,不妨假设1-2,则左边消除了两个,右边增加一个:数2可以匹配其他的所有位置
2.当左边的数1匹配上右边时,不妨假设1-5,则左边消除了一个,右边数目不变(消除了5,添加了1)
3.当右边的数7匹配上左边时,不妨假设7-1,则左边消除了一个,右边数目不变(消除了7,添加了1)
4.当右边的数7匹配上右边时,不妨假设7-5,则左边不变,右边数目减少一个
所以可以dp,dp[i][j]表示左边有i个,右边有j个时的方案数。
1.当i=0时,为p[j](p[j]表示j的排列)
2.当i=1时,左边任意匹配右边一位,右边数目不变,因此方案数为为p[j]*j。
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ /******** program ********************/ const int MAXN = 2005;
const int MOD = 1e9+7; ll dp[MAXN][MAXN];
int a[MAXN],n;
bool use[MAXN];
ll p[MAXN]; ll dfs(int x,int y,int res){
if(res<=0) return p[y];
if(x==1)return p[y]*y%MOD;
if(x==0)return p[y];
if(~dp[x][y])return dp[x][y]; ll tmp = (x-1);
tmp *= dfs(x-2,y+1,res-2);
tmp += dfs(x-1,y,res-1)*y;
return dp[x][y] = tmp%MOD;
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif p[0] = p[1] = 1;
for(int i=2;i<MAXN;i++)
p[i] = p[i-1]*i%MOD; while(cin>>n){
Clear(use);
rep1(i,n){
RD(a[i]);
if(~a[i])
use[ a[i] ] = true;
} int x = 0 , y = 0;
rep1(i,n){
if(a[i]==-1){
if(use[i])y ++;
else x ++;
}
} memset(dp,-1,sizeof(dp));
cout<<dfs(x,y,x+y)<<endl;
} return 0;
}
Codeforces Round #198 (Div. 1) B,C 动态规划的更多相关文章
- Codeforces Round #198 (Div. 2)A,B题解
Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...
- [置顶] Codeforces Round #198 (Div. 1)(A,B,C,D)
http://codeforces.com/contest/341 赛后做的虚拟比赛,40分钟出了3题,RP爆发. A计数问题 我们可以对每对分析,分别对每对<a, b>(a走到b)进行统 ...
- Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理
题目链接:http://codeforces.com/contest/340/problem/E E. Iahub and Permutations time limit per test 1 sec ...
- Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*
D. Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this prob ...
- Codeforces Round #198 (Div. 2)
A.The Wall 题意:两个人粉刷墙壁,甲从粉刷标号为x,2x,3x...的小块乙粉刷标号为y,2y,3y...的小块问在某个区间内被重复粉刷的小块的个数. 分析:求出x和y的最小公倍数,然后做一 ...
- Codeforces Round #198 (Div. 2) —— D
昨天想了一下D题,有点思路不过感觉很麻烦,就懒得去敲了: 今天上午也想了一下,还是没有结果,看了一下官方题解,证明得很精彩: 这道题目其实就是一道裸地最大上升子序列的题: 看到这里,直接怒码···· ...
- Codeforces Round #198 (Div. 2) —— C
C题很容易看懂题目,不过两个循环肯定会TLE,所以得用点小聪明: 首先排好序,因为是全排列,乱序和顺序的结果是一样的: 然后呢···· 如果是数列 1 2 3 4 5 元素1 被 2 3 4 5每个减 ...
- Codeforces Round #198 (Div. 2) —— B
B题是一个计算几何的题,虽然以前看过计算几何的ppt,但一直都没有写过: 昨晚比赛的时候本来想写的,但是怕不熟练浪费时间,太可惜了! 其实没必要选出一个最大的矩形: 以矩形的一条对角线为轴,向上或者向 ...
- Codeforces Round #198 (Div. 2) —— A
最水的题,可惜当时赶时间没有注意数据范围:暴力超时了! 其实应该用x,y的最大公约数来判断: 代码: #include<iostream> using namespace std; int ...
随机推荐
- 消息系统Kafka介绍 - 董的博客
1. 概述 Kafka是Linkedin于2010年12月份开源的消息系统,它主要用于处理活跃的流式数据.活跃的流式数据在web网站应用中非常常见,这些数据包括网站的pv.用户访问了什么内容,搜索了 ...
- wpf图片切换,幻灯效果
xaml: <Window x:Class="WpfApplication1.PicShow" xmlns="http://schemas.microsoft.co ...
- Nginx系列~Nginx服务启动不了
Nginx服务有时起动不了了,原因是80端口为其它应用程序占用了,这时,我们需要查看是哪个程序占用了它,可能是IIS的某个站点,或者Tomat,Apache等,都有可能,所以,我们需要查看一下电脑80 ...
- Ehcache(04)——设置缓存的大小
http://haohaoxuexi.iteye.com/blog/2116749 设置缓存的大小 目录 1 CacheManager级别 2 Cache级别 3 大小衡量 4 ...
- XHTML标签的嵌套规则--很基础很重要
XHTML的标签有许多:div.ul.li.dl.dt.dd.h1~h6.p.a.addressa.span. strong……我们在运用这些标签搭建页面结构的时候,是可以将它们无限嵌套的,但是,嵌套 ...
- Codeforces Round #340 (Div. 2) C. Watering Flowers 暴力
C. Watering Flowers 题目连接: http://www.codeforces.com/contest/617/problem/C Descriptionww.co A flowerb ...
- Educational Codeforces Round 4 B. HDD is Outdated Technology 暴力
B. HDD is Outdated Technology 题目连接: http://www.codeforces.com/contest/612/problem/B Description HDD ...
- HTML的disabled属性及readonly属性
disabled属性的input不会提交到服务器. readonly属性的input会提交到服务器.
- iOS开发——UI篇Swift篇&UIImageView
UIImageView override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleString //通过坐标和大 ...
- cocos2d-x jsbinding 在线更新策略设计
在线更新是用脚本编写游戏逻辑的特有功能,由于脚本语言是边解释边编译的特性,使得游戏在运行的时候可以通过下载最新的脚本来执行游戏逻辑.在不修改Native接口的情况下,在线更新每次更新只需要下载一个(5 ...