搭桥问题
【pay attention】:low_bound(a,a+len,b)的具体意思是
在a数组中寻找第一个大于等于数字b的数字,
并且返回其位置。
Problem Description
‘Oh no, they’ve done it again’, cries the chief designer at the Waferland chip factory. Once more the routing designers have screwed up completely, making the signals on the chip connecting the ports of two functional blocks cross each other all over the place. At this late stage of the process, it is too
expensive to redo the routing. Instead, the engineers have to bridge the signals, using the third dimension, so that no two signals cross. However, bridging is a complicated operation, and thus it is desirable to bridge as few signals as possible. The call for a computer program that finds the maximum number of signals which may be connected on the silicon surface without rossing each other, is imminent. Bearing in mind that there may be housands of signal ports at the boundary of a functional block, the problem asks quite a lot of the programmer. Are you up to the task?

Figure 1. To the left: The two blocks’ ports and their signal mapping (4,2,6,3,1,5). To the right: At most three signals may be routed on the silicon surface without crossing each other. The dashed signals must be bridged.

A typical situation is schematically depicted in figure 1. The ports of the two functional blocks are numbered from 1 to p, from top to bottom. The signal mapping is described by a permutation of the numbers 1 to p in the form of a list of p unique numbers in the range 1 to p, in which the i:th number pecifies which port on the right side should be connected to the i:th port on the left side.
Two signals cross if and only if the straight lines connecting the two ports of each pair do.

Input
On the first line of the input, there is a single positive integer n, telling the number of test scenarios to follow. Each test scenario begins with a line containing a single positive integer p<40000, the number of ports on the two functional blocks. Then follow p lines, describing the signal mapping: On the i:th line is the port number of the block on the right side which should be connected to the i:th port of the block on the left side.

Output
For each test scenario, output one line containing the maximum number of signals which may be routed on the silicon surface without crossing each other.

Sample Input
4
6
4
2
6
3
1
5
10
2
3
4
5
6
7
8
9
10
1
8
8
7
6
5
4
3
2
1
9
5
8
9
2
3
1
7
4
6

Sample Output
3
9
1
4

其实AC代码很简短(但是lower_bound函数值得我们学习):
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 40010;
int main()
{int cas;scanf("%d",&cas);//测试的样例个数 while(cas--){int n;int len;int a[maxn];int b[maxn];//一个测试样例中的样例个数 scanf("%d",&n);//输入对应需要帮助的村庄 for(int i=1; i<=n; ++i)scanf("%d",&a[i]); b[1] = a[1];//先初始化一条边 len = 1;for(int i=2; i<=n; ++i){if(a[i]>b[len])b[++len] = a[i];else{int  pos = lower_bound(b,b+len,a[i]) - b;//在b数组中找到第一个比它大的数, b[pos] = a[i];//将那个比现在大的数向更小的方向转化,//在符合题意的情况下//(即不影响后面输入判断的情况下) , // 正确地将后面的数字接在相应的数字后,//因为这么做可以让相应位置更小,//至少不比原来大的数字差,//略带有些贪心思想,符合极大升序列的寻找规律 }}printf("%d\n",len);}return 0;}

【important】

TKO 6-6 DP入门之村庄援助相关推荐

  1. hdu 2089 数位dp入门

    HDU 2089 题意:中文题 思路:数位dp入门题 AC代码: #include "iostream" #include "string.h" #includ ...

  2. SCU - 1114 数字三角(dp入门ing)

    SCU - 1114 数字三角(dp入门ing) 下图是个数字三角,请编写一个程序计算从顶部至底部某处一条路径,使得该路径所经过的数字总和最大. 7 3 8 8 1 0 2 7 4 4 1. 每一步可 ...

  3. 数位dp入门题 洛谷P2657 [SCOI2009] windy 数

    题干 传送门 windy 定义了一种 windy 数. 题目描述 不含前导零且相邻两个数字之差至少为 2的正整数被称为 windy 数.windy 想知道,在 a 和 b 之间,包括 a 和 b ,总 ...

  4. TKO 6-3 DP入门之肥胖的力量

    胖子的速度 问题描述 胖子认为老鼠越胖,跑得越快.为了反驳这一点,您希望将小鼠集合的数据,并将尽可能多的数据子集放入一个序列中,以便重量增加,但速度正在下降. 输入 输入包含一堆鼠标的数据,每行一只鼠 ...

  5. TKO 6-2 DP入门之2084(基本dp之数塔问题)

    数塔 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之 ...

  6. TKO 6-4 DP入门之最少拦截系统

    Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高 ...

  7. TKO 6-1 DP入门之1058(寻找第n个因数只有2、3、5、7的数字)

    杭电1058: Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble numb ...

  8. 树形DP入门题目推荐以及解析

    关于树形DP几道入门题目 今天恶补树形DP,感觉海星. 其实挺简单的. 介绍几道例题,我会的. 1.洛谷P1352 没有上司的舞会 我的一篇题解 我们可以考虑每一个节点都是有两种情况. 一个是被邀请: ...

  9. hdu_Anniversary party_(树形DP入门题)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意:有N个人,N-1个人有自己的上司,每个人有一个快乐值,如果这个人参加了聚会,那么这个人的直 ...

最新文章

  1. PyTorch全连接ReLU网络
  2. 300秒搞定第一超算1万年的计算量,量子霸权时代已来?
  3. 【转载】Asp.Net MVC3网站并成功的连接了MongoDB
  4. android adb恢复出厂设置,android开发分享擦除数据/通过ADB恢复出厂设置
  5. 【JUC并发编程13】异步回调
  6. 对 cas 操作的理解
  7. 对某机构为“转移内部矛盾”而嫁祸于我们的事件之真相大起底
  8. [蓝桥杯2019初赛]完全二叉树的权值-完全二叉树的性质
  9. Membership学习(三)Membership Providers介绍[xgluxv]
  10. 做好前端的话HTML和CSS基础必须夯实!
  11. 如何使用CORS解决跨域问题
  12. 【BERT中文改进版】预训练ALBERT模型:参数更少,效果更好,拿下13项NLP任务
  13. 记一次mybatis-plus遇到的问题
  14. 搜狗输入法linux版的主程序,搜狗输入法linux安装包下载
  15. 布局改变时的过场动画
  16. 加个ing是什么意思_ing是什么意思?
  17. hive-create table
  18. Cognos的下载地址分享
  19. Python将文字转成语音并读出来
  20. “好学若饥、谦卑若愚”

热门文章

  1. Java基础_107. private 关键字的使用
  2. 51单片机之程序模块化
  3. 穆迪分析在Chartis信用风险报告中被评为类别领导者
  4. VTP:Cisco VLAN Trunking Protocol
  5. upan插入ubuntu系统出错-显示read only file system
  6. 倾角传感器的性能精度理解文章
  7. 表达式计算 JexlEngine
  8. WiFi万能钥匙App可一键登录全国215个城市的“爱WiFi”
  9. 【C++修行之路】C/C++内存管理
  10. 傲腾内存 可以用ghost系统_创新无止境!英特尔2020继续用“芯”改变世界