题目来源: HackerRank
基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题
 收藏
 取消关注
给出一个整数数组A,你可以将任何一个数修改为任意一个正整数,最终使得整个数组是严格递增的且均为正整数。问最少需要修改几个数?
Input
第1行:一个数N表示序列的长度(1 <= N <= 100000)。
第2 - N + 1行:每行1个数,对应数组元素。(0 <= A[i] <= 10^9)
Output
输出最少需要修改几个数使得整个数组是严格递增的。
Input示例
5
1
2
2
3
4
Output示例
3

发现规律就是如果该位置的数a[i]-i,这个数如果小于零了,那么这个数是一定要修改的。

剩下的那些a[i]-i就是要找最长的非严格递增序列了。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int n;
int val[100005];
int f[100005];
int soar[100005]; int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); int i, k, num, ans;
scanf("%d", &n); num = 0;
ans = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &val[i]);
val[i] = val[i] - (i + 1); if (val[i] < 0)
{
ans++;
}
else
{
f[num++] = val[i];
}
} fill(soar,soar+num,-1);
k = 0; for (i = 0; i < num; i++)
{
if (f[i] >= soar[k])
{
soar[++k] = f[i];
}
else
{
int pos = upper_bound(soar, soar + k + 1, f[i]) - soar;
soar[pos] = f[i];
}
}
printf("%d\n", num - k + ans);
//system("pause");
return 0;
}

序列变换

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 952    Accepted Submission(s): 375

Problem Description
我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增。其中无论是修改前还是修改后,每个元素都必须是整数。

请输出最少需要修改多少个元素。
 
Input
第一行输入一个T(1≤T≤10),表示有多少组数据



每一组数据:



第一行输入一个N(1≤N≤105),表示数列的长度



第二行输入N个数A1,A2,...,An。



每一个数列中的元素都是正整数而且不超过106。
 
Output
对于每组数据,先输出一行



Case #i:



然后输出最少需要修改多少个元素。
 
Sample Input
2
2
1 10
3
2 5 4
 
Sample Output
Case #1:
0
Case #2:
1
 

和之前的没什么区别,把大于0的条件删掉就OK。

代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int n;
int val[100005];
int f[100005];
int soar[100005]; int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); int i, k, num; int test, cas = 1;
scanf("%d", &test);
while (test--)
{
scanf("%d", &n); num = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &val[i]);
val[i] = val[i] - (i + 1);
f[num++] = val[i];
} fill(soar, soar + num, -1000005);
k = 0; for (i = 0; i < num; i++)
{
if (f[i] >= soar[k])
{
soar[++k] = f[i];
}
else
{
int pos = upper_bound(soar, soar + k + 1, f[i]) - soar;
soar[pos] = f[i];
}
}
printf("Case #%d:\n", cas++);
printf("%d\n", num - k);
}
//system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

51nod 1294 :修改数组 && HDU 5256:序列变换的更多相关文章

  1. 51Nod 1294 修改数组 —— LIS

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1294 1294 修改数组  题目来源: HackerRank ...

  2. hdu 5256 序列变换 (LIS变形)

    序列变换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  3. HDU 5256 - 序列变换 ,树状数组+离散化 ,二分法

    Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数.请输出最少需要修改多少 ...

  4. hdu 5256 序列变换(LIS最长上升子序列)

    Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数. 请输出最少需要修改多 ...

  5. 51nod 1294 修改数组

    若a[i]-i(i从1开始)的值小于0,那么a[i]必须改变 若a[i]-i的值大于等于0,将a[i]-i存入新的数组中,求出新数组的最长非严格上升子序列,所得即最多的,不用改变的数. #includ ...

  6. hdu 5256 序列变换

    最长上升子序列 nlogn;也是从别人的博客学来的 #include<iostream> #include<algorithm> #define maxn 100000+5 u ...

  7. LIS 2015百度之星初赛2 HDOJ 5256 序列变换

    题目传送门 题意:中文题面 分析:LIS(非严格):首先我想到了LIS,然而总觉得有点不对:每个数先减去它的下标,防止下面的情况发生:(转载)加入序列是1,2,2,2,3,这样求上升子序列是3,也就是 ...

  8. hdu 5248 序列变换(二分枚举)

    Problem Description 给定序列A={A1,A2,...,An}, 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:Bi<Bi+,≤i<N). 我们 ...

  9. 51nod1294 修改数组

    看题解的...就是将必须要修改的数去掉后求最长的不递减子序列. upper_bound+lower_bound要理解.有时候-1有时候不用是有原因的. #include<cstdio> # ...

随机推荐

  1. 【PAT甲级】1006 Sign In and Sign Out (25 分)

    题意: 给出学生人数M,输入M组学生ID,到机房的时间,离开机房的时间.输出最早到机房的学生的ID,空格,最后离开机房的学生的ID.(M大小未给出,就用了1e5) AAAAAccepted code: ...

  2. 计算xx年xx月xx日是星期几

    代码: #include <iostream> #include <string> #include <vector> using namespace std; i ...

  3. Docker 问题[Warning] IPv4 forwarding is disabled. Networking will not work.

    Docker 问题[Warning] IPv4 forwarding is disabled. Networking will not work. 在使用Dockerfile创建Docker镜像的时候 ...

  4. CSS - 布局流程

    一.为了提高网页制作的效率,布局时通常需要遵守一定的布局流程,具体如下: "版心"(可视区) 是指网页中主体内容所在的区域.一般在浏览器窗口中水平居中显示,常见的宽度值为960px ...

  5. Fescar分布式事务实现原理解析探秘

    前言 fescar发布已有时日,分布式事务一直是业界备受关注的领域,fescar发布一个月左右便受到了近5000个star足以说明其热度.当然,在fescar出来之前,已经有比较成熟的分布式事务的解决 ...

  6. js通过cookie对两个没有关系的jsp页面进行传值

    //Cookie取值 function readCookie (name) { var cookieValue = ""; var search = name + "=& ...

  7. JAVA培训—线程同步--卖票问题

    线程同步方法: (1).同步代码块,格式: synchronized (同步对象){ //同步代码 } (2).同步方法,格式: 在方法前加synchronized修饰 问题: 多个人同时买票. 1. ...

  8. 获取navigationController中的控制器

    @implementation UIViewController (UIViewControllerExt) - (void)popViewController:(NSString *)control ...

  9. 攻防世界web进阶区(1)

    1.题目地址:http://111.198.29.45:43589 页面提示打开robots文件,则: 页面有提示输入fl0g.php,那么 获取flag. 2.题目地址:http://111.198 ...

  10. PAT A1025 pat ranking

    有n个考场,每个考场都有若干数量个考生,现给出各个考场中考生的准考证号和分数,要求将所有考生的分数从高到低排序,并输出 #include<iostream> #include<str ...