关于TJOI2014的一道题——Alice and Bob
B Alice and Bob
•输入输出文件: alice.in/alice.out
•源文件名: alice.cpp/alice.c/alice.pas
• 时间限制: 1s 内存限制: 128M
题目描述
Alice 和 Bob 发明了一个新的游戏。给定一个序列{x0,x1,··· ,xn−1}。Alice得 到一个序列 {a0,a1,··· ,an−1},其中 ai 表示以 xi 结尾的最长上升子序列的长 度;Bob 得到一个序列 {b0,b1,··· ,bn−1},其中 bi 表示以 xi 开头的最长下降 子序列的长度。Alice的得分是序列 {a0,a1,··· ,an−1} 的和,Bob的得分是 {b0,b1,··· ,bn−1}的和。 输输输入入入 输入的第一行是 n,第二行是序列{a0,a1,··· ,an−1}。数据保证序列 a 可以由 至少一个 1 到 n 的排列得到。
输出
输出包含一行,表示 Bob 能得到的最高分数。
样例输入1
4 1 2 2 3
样例输出1
5
样例输入2
4 1 1 2 3
样例输出2
5
数据范围 对于 30%
的数据,N ≤ 1000
对于 100% 的数据,N ≤ 10^5
这道题一看到的时候觉得可做,马上写出了一个转移方程 f[i]表示从后往前值为 i 时最长的答案,觉得似乎只能从后面 的 a值小于i的地方转移过来,然后就比SCOIDay1T1简单,用同样的思路加树状数组就出来了。但是后来再检查的时候仔细推了一下,发现有些不对。
对于两个位置p,q(p<q) 如果a[p]>=a[q] 那么x[p]>x[q] ,这一点很显而易见,但是如果a[p]<a[q]并不能推出x[p]<x[q]比如x为1 6 2 5 3 4时a值为1 2 2 3 3 4 a[2]<a[6]但是x[2]>x[6],所以说有一定问题。我们发现,如果满足a[p]<a[q]&&x[p]>x[q]则一定存在另一个序列从p后面等于a[p]这个值开始,不断累加可以得到,比如a[2]后面a[3]==a[2] ,a[4]==a[3]+1,a[6]==a[4]+1,所以第6个位置可能比第二个位置小
然后就得到了一个暴力的方法,小于的像原来那样弄,大于的N^2来搞,这样子可以得30分
然后想优化,思想上比较麻烦,写起来很简单,直接看代码吧:
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
int lowbit(int x) {return x&-x;} int num[];
int n;
void change(int data,int pos)
{
while (pos<=n)
{
num[pos]=max(num[pos],data);
pos+=lowbit(pos);
}
} int getans(int pos)
{
int ret=;
while (pos>)
{
ret=max(ret,num[pos]);
pos-=lowbit(pos);
}
return ret;
} int a[];
int f[];
int fself[];
int nextc[];
int nextb[];
int last[];
int maxd[];
void pre()
{
for (int i=n;i>=;--i)
{
nextc[i]=last[a[i]];
last[a[i]]=i;
nextb[i]=last[a[i]+];
}
} void trans(int k)
{
fself[k]=fself[nextb[k]];
fself[k]=max(fself[k],maxd[a[k]]);
} int getbigans(int k)
{
return fself[nextc[k]]+;
} int viodp()
{
int ret=;
for (int i=n;i>=;--i)
{
f[i]=;
for (int j=i+;j<=n;++j)
{
if (a[j]<=a[i])
f[i]=max(f[i],f[j]+);
}
int now=a[i];
for (int j=i+;j<=n;++j)
{
if (a[j]<=now&&a[j]>a[i])
f[i]=max(f[i],f[j]+);
if (a[j]==now)
{
now=now+;
}
}
ret+=f[i];
}
// cout<<endl;
return ret;
} long long dp()
{
long long ret=;
for (int i=n;i>=;--i)
{
int f=getans(a[i])+;
f=max(f,getbigans(i));
maxd[a[i]]=max(maxd[a[i]],f);
trans(i);
change(f,a[i]);
ret+=f;
}
return ret;
} int main()
{
freopen ("alice.in","r",stdin);
freopen ("alice.out","w",stdout);
scanf("%d",&n);
for (int i=;i<=n;++i)
{
scanf("%d",&a[i]);
}
if (n<=)
{
cout<<viodp()<<endl;
return ;
}
pre();
cout<<dp()<<endl;
return ;
}
Viodp就是暴力的DP
自己看着理解理解吧。。。其实我觉得这道DP不错,有些意思。似乎其他人写了什么拓扑排序只有50分。
关于TJOI2014的一道题——Alice and Bob的更多相关文章
- ACdream 1112 Alice and Bob(素筛+博弈SG函数)
Alice and Bob Time Limit:3000MS Memory Limit:128000KB 64bit IO Format:%lld & %llu Submit ...
- XTU OJ 1209 Alice and Bob 2014(嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛)
Problem Description The famous "Alice and Bob" are playing a game again. So now comes the ...
- UOJ #266 【清华集训2016】 Alice和Bob又在玩游戏
题目链接:Alice和Bob又在玩游戏 这道题就是一个很显然的公平游戏. 首先\(O(n^2)\)的算法非常好写.暴力枚举每个后继计算\(mex\)即可.注意计算后继的时候可以直接从父亲转移过来,没必 ...
- Foj 2296 Alice and Bob(博弈、搜索)
Foj 2296 Alice and Bob 题意 两个人博弈,规则如下:轮流取0~9中的数字,最后Alice所得的数字个数为1~n中,数位在Alice所取集合中出现奇数次的. 双方想获得尽量多,问A ...
- hdu 4111 Alice and Bob 记忆化搜索 博弈论
Alice and Bob Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- bzoj4730: Alice和Bob又在玩游戏
Description Alice和Bob在玩游戏.有n个节点,m条边(0<=m<=n-1),构成若干棵有根树,每棵树的根节点是该连通块内编号最 小的点.Alice和Bob轮流操作,每回合 ...
- Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...
- sdutoj 2608 Alice and Bob
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2608 Alice and Bob Time L ...
随机推荐
- 谈一谈a:link、a:visited、a:hover、a:active的正确使用顺序
前端路上,未来还远,所以基础部分必须扎实,走好现在脚下的每一步才是现在最重要的. 下面进入正题吧. 1. <a>标签 我们先说一说<a>标签是干啥用的. <a> 标 ...
- 数组的复制 --System.arraycopy()
import java.util.Arrays; public class HellowWorld { public static void main(String[] argv ) { int[] ...
- 前端自动化构建工具gulp使用
1. 全局安装 gulp: $ npm install --global gulp 2. 作为项目的开发依赖(devDependencies)安装: $ npm install --save-dev ...
- How To:防火墙规则去重
主要命令 iptables-save| awk ' !x[$0]++ | iptables-restore 演示: [root@testname ~]# iptables -vL Chain INPU ...
- 回溯法、DFS
回溯法 为了求得问题的解,先选择某一种可能情况向前探索,在探索过程中,一旦发现原来的选择是错误的,就退回上一步重新选择条件,继续向前探索,如此反复进行,直至得到解或证明无解. DFS DFS模板 vo ...
- python--(十五步代码学会进程)
python--(十五步代码学会进程) 一.进程的创建 import time import os #os.getpid() 获取自己进程的id号 #os.getppid() 获取自己进程的父进程id ...
- hdu2009 求数列的和【C++】
求数列的和 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- noip模拟赛 解谜游戏
题目描述LYK进了一家古董店,它很想买其中的一幅画.但它带的钱不够买这幅画.幸运的是,老板正在研究一个问题,他表示如果LYK能帮他解出这个问题的话,就把这幅画送给它.老板有一个n*m的矩阵,他想找一个 ...
- Method and apparatus for speculative execution of uncontended lock instructions
A method and apparatus for executing lock instructions speculatively in an out-of-order processor ar ...
- Hadoop2.2.0 注意事项
1.启动前必须把防火墙关了,要不然会导致nodemanager启动不了. 关闭防火墙:service iptables stop 永久关闭(重启后默认关闭):chkconfig iptables of ...