HDOJ 5773 (2016多校联合训练 Training Contest 4) The All-purpose Zero
2016-07-30 14:49
453 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5773
题意,0可以变成任何数,问我们可以得到的最长的上升序列的长度为多少。
首先0可以变成任何数,那么最长的上升序列不一定包含所有的0,但是包含了所有的0的序列一定可以达到最长上升序列的长度,所以我们采取的策略就是把每一个数都减去它之前的0的个数,然后再进行一次LIS,最后再加上0的总个数,这样我们就能够保证一定求得的序列含有所有的0.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000000+5;
int a[maxn], num[maxn], dp[maxn], n;
int LIS()
{
dp[0] = -1;
int len = 0;
for(int i=1; i<=n; i++)
{
if(num[i] == num[i-1]+1) continue;
if(a[i] > dp[len]) dp[++len] = a[i];
else
{
int pos = lower_bound(dp, dp+len, a[i]) - dp;
dp[pos] = a[i];
}
}
return len;
}
int main()
{
int T, t=1;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
num[0] = 0;
for(int i=1; i<=n; i++)
{
scanf("%d", &a[i]);
num[i] = num[i-1] + (a[i]==0);
if(a[i]) a[i] -= num[i];
}
int len = LIS();
//for(int i=1; i<=len; i++) printf("%d ", dp[i]);
printf("Case #%d: %d\n", t++, len+num
);
}
}
题意,0可以变成任何数,问我们可以得到的最长的上升序列的长度为多少。
首先0可以变成任何数,那么最长的上升序列不一定包含所有的0,但是包含了所有的0的序列一定可以达到最长上升序列的长度,所以我们采取的策略就是把每一个数都减去它之前的0的个数,然后再进行一次LIS,最后再加上0的总个数,这样我们就能够保证一定求得的序列含有所有的0.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000000+5;
int a[maxn], num[maxn], dp[maxn], n;
int LIS()
{
dp[0] = -1;
int len = 0;
for(int i=1; i<=n; i++)
{
if(num[i] == num[i-1]+1) continue;
if(a[i] > dp[len]) dp[++len] = a[i];
else
{
int pos = lower_bound(dp, dp+len, a[i]) - dp;
dp[pos] = a[i];
}
}
return len;
}
int main()
{
int T, t=1;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
num[0] = 0;
for(int i=1; i<=n; i++)
{
scanf("%d", &a[i]);
num[i] = num[i-1] + (a[i]==0);
if(a[i]) a[i] -= num[i];
}
int len = LIS();
//for(int i=1; i<=len; i++) printf("%d ", dp[i]);
printf("Case #%d: %d\n", t++, len+num
);
}
}
相关文章推荐
- Baidu地图在自己的项目中使用(二)
- poj 1681 Painter's Problem 【高斯消元 枚举自由变元】
- HDOJ 5775 (2016多校联合训练 Training Contest 4) Bubble Sort
- LeetCode--373. Find Pairs with Smallest Sums
- HDOJ 5763 (2016多校联合训练 Training Contest 4) Another Meaning
- Composer:Failed to execute git clone --mirror ...Permission denied (publickey). fatal: Could not
- HDU5724 Chess 阶梯博弈变形 2016 Multi-University Training Contest 1
- Assign directions to edges so that the directed graph remains acyclic
- HDU 2476 String painter(动态规划)★
- Rails 中 View 层 haml 初步
- Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from http://repo.maven.apache.org/ maven2 was cached in the local repository, resolution will not be reattempted until the update interv
- POJ 1363 Rails(栈,模拟地铁出站)
- HDU 1082 Matrix Chain Multiplication(栈和map的使用)
- wait、notify、notifyAll和Condition
- [iPhone]Xcode process launch failed: Security
- CURL命令报错:dyld: lazy symbol binding failed: Symbol not found: _SSL_load_error_strings解决办法
- foxmail导入outlook部分邮件
- POJ 1218 THE DRUNK JAILER
- Aizu 2677 Breadth-First Search by Foxpower
- [USACO Training] Section 1.1