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 ...
随机推荐
- EF小节
EF学习笔记——生成自定义实体类 http://blog.csdn.net/leftfist/article/details/24889819 --工具: 1.entity developer 2.D ...
- intval()和(int)转换使用与区别
<?php echo "<br/>数值强制转换:"; $string="2a"; $string1=intval($string); echo ...
- uC/OS-III学习2::uC/OS-III LED闪烁实验
1 前言: 看完了uC/OS-III的基本介绍之后,大致对这个操作系统有了点了解,但真正的理解还是要通过不断的去使用,在使用中体验uC/OS-III的乐趣和更深的理解其工作原理是非常重要的.因此,我在 ...
- Android Studio初步使用教程
今年的Google全球开发者大会虽然没有新的Android系统和设备,但是还是推出了一些不错的产品,Android Studio就是其中之一.这个基于Intellij IDEA开发的Android I ...
- cdoj 31 饭卡(card) 01背包
饭卡(card) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/31 Des ...
- C#将html导出到word(基于wps)
由于客户需要,我们需要实现将网页导出到word中的功能,在此过程中,尝试使用过openoffice.itext.wordapi等各种方法,都不尽如人意.openoffice导出的问题图片信息在word ...
- 多个非同源的shared_ptr管理对象引起double free
有多个不同源的shared_ptr管理对象时会出现多次释放对象,这里不同源是指多组间不是通过拷贝构造.复制等手段而来的,即几组shared_ptr是独立声明的. #include<iostrea ...
- windows 32位系统中进程最大可用内存空间为3GB (转)
http://msdn.microsoft.com/zh-cn/library/ms189334.aspx 进程地址空间 所有 32 位应用程序都有 4 GB 的进程地址空间(32 位地址最多可以映射 ...
- Microsoft Visual C++ Runtime Library Runtime Error的解决的方法
打开浏览器时,出现Microsoft Visual C++ Runtime Library Runtime Error错误,初步预计是软件冲突,可能有多种出错的方式,我的是浏览器自己主动关闭. 一. ...
- TP复习15
## ThinkPHP 3.1.2 URL#讲师:赵桐正微博:http://weibo.com/zhaotongzheng 本节课大纲:一.URL规则 1.默认是区分大小写的 2.如果我们不想区分大小 ...