比赛时,开了大号去做,算了半天发现不会做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 动态规划的更多相关文章

  1. Codeforces Round #198 (Div. 2)A,B题解

    Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...

  2. [置顶] Codeforces Round #198 (Div. 1)(A,B,C,D)

    http://codeforces.com/contest/341 赛后做的虚拟比赛,40分钟出了3题,RP爆发. A计数问题 我们可以对每对分析,分别对每对<a, b>(a走到b)进行统 ...

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

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

  5. Codeforces Round #198 (Div. 2)

    A.The Wall 题意:两个人粉刷墙壁,甲从粉刷标号为x,2x,3x...的小块乙粉刷标号为y,2y,3y...的小块问在某个区间内被重复粉刷的小块的个数. 分析:求出x和y的最小公倍数,然后做一 ...

  6. Codeforces Round #198 (Div. 2) —— D

    昨天想了一下D题,有点思路不过感觉很麻烦,就懒得去敲了: 今天上午也想了一下,还是没有结果,看了一下官方题解,证明得很精彩: 这道题目其实就是一道裸地最大上升子序列的题: 看到这里,直接怒码···· ...

  7. Codeforces Round #198 (Div. 2) —— C

    C题很容易看懂题目,不过两个循环肯定会TLE,所以得用点小聪明: 首先排好序,因为是全排列,乱序和顺序的结果是一样的: 然后呢···· 如果是数列 1 2 3 4 5 元素1 被 2 3 4 5每个减 ...

  8. Codeforces Round #198 (Div. 2) —— B

    B题是一个计算几何的题,虽然以前看过计算几何的ppt,但一直都没有写过: 昨晚比赛的时候本来想写的,但是怕不熟练浪费时间,太可惜了! 其实没必要选出一个最大的矩形: 以矩形的一条对角线为轴,向上或者向 ...

  9. Codeforces Round #198 (Div. 2) —— A

    最水的题,可惜当时赶时间没有注意数据范围:暴力超时了! 其实应该用x,y的最大公约数来判断: 代码: #include<iostream> using namespace std; int ...

随机推荐

  1. Unity3d:加载Gif格式图片

    unity里不支持Gif格式的图片,网上搜索也没有相关资料,殊不知我们已经太相信度娘了,而没有了自己的分析,我们知道Gif图是由多个静态图做成的,那我们就回归本土,第一步:把gif拆成n个静态图放在集 ...

  2. 基于KVM的虚拟化研究及应用

    引言 虚拟化技术是IBM在20世纪70年代首先应用在IBM/370大型机上,这项技术极大地提高了大型机资源利用率.随着软硬件技术的迅速发展,这项属于大型机及专利的技术开始在普通X86计算机上应用并成为 ...

  3. WinForm设置窗体默认控件焦点

    winform窗口打开后文本框的默认焦点设置,进入窗口后默认聚焦到某个文本框,两种方法: ①设置tabindex 把该文本框属性里的tabIndex设为0,焦点就默认在这个文本框里了. ②Winfor ...

  4. DBCP连接池介绍

    DBCP连接池介绍 ----------------------------- 目前 DBCP 有两个版本分别是 1.3 和 1.4. DBCP 1.3 版本需要运行于 JDK 1.4-1.5 ,支持 ...

  5. VCL -- Understanding the Message-Handling System

    Understanding the Message-Handling System http://docwiki.embarcadero.com/RADStudio/XE7/en/Understand ...

  6. ThinkPHP模板(一)

    如何关闭ThinkPHP的模板缓存 ThinkPHP的模板缓存是无奈关闭的,因为内置的模板引擎是一个编译型的模板引擎,必须经过编译后生成一个可执行的缓存文件才能被执行.但是可以设置缓存的有效期,例如设 ...

  7. IE6下z-index失效

    一.匆匆带过的概念关于CSS中层级z-index的定义啊什么的不是本文的重点,不会花费过多篇幅详细讲述.这里就简单带过,z-index伴随着层的概念产生的.网页 中,层的概念与photoshop或是f ...

  8. Java带包编译运行

    package cn.togeek.job; public class Test { public static void main(String[] args) throws Exception { ...

  9. C++学习笔记之作用域为类的常量和作用域内的枚举

    一.作用域为类的常量 有些情况下,使符号常量的作用域为类很有用.如,类声明(不是定义)可能使用字面值30来指定数组长度,由于该常量对于所有对象来说都是相同的,因此创建一个由所有对象共享的常量是个不错的 ...

  10. [RDLC]报表根据字段列动态加载图片(二)

    参照:http://www.cnblogs.com/hcbin/archive/2010/03/26/1696803.html 改动地方value的值可以用报表的字段进行编辑. 效果: