C语言动态规划(1)___Bridging signals

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 crossing each other, is imminent. Bearing in mind that there may be thousands 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? 

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 specifies 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


样例:

#include <stdio.h>
int f[40010];
int longest(int a,int l,int r)   //定义二分查找函数
{
	int m;
	while(l<=r)
	{
		m=(l+r)/2;
		if(f[m]==a)   //如果出现当前值和查找的值相同那么自然就更新当前的f[]值,也可理解为不用更新
		{
			l=m;
			return l;
		}
		else if(f[m]>a)
			r=m-1;
		else
			l=m+1;
	}
	return l;   //每次返回的都是在f[]中最大的j能够使得f[j]<a[i]的j+1.
}
int main()
{
	int N,n,a,i,t,k,j;
	scanf("%d",&N);
	while(N--)
	{
		t=0;
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			scanf("%d",&a);  //每次输入一个数就处理一个数
			k=longest(a,1,t);
			if(k<=t)          //如果k<=t说明是情况3.
				f[k]=a;
			else              //否则为情况1,表示最大长度可以更新
			{t++;f[t]=a;}
		}
		printf("%d\n",t);
	}
	return 0;
}

题目大概意思:

先输入一个数N表示有N组测试数据

每组数据先输入一个数n表示有n个

接下来的n排分别表示左边1~n与右边的连接序号.

输出最多有多少不交叉的连接.


这道题实质就是数列中最长不下降子序列问题.   具体:(2)最长不下降子序列问题____动态规划



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用桥接(bridge)将NSString对象转换为CFStringRef类型,然后使用CFStringGetCString或CFStringGetCharacters等函数将其转换为C语言字符串或Unicode字符数组。例如: ``` NSString *str = @"Hello, world!"; CFStringRef cfStr = (__bridge CFStringRef)str; const char *cStr = CFStringGetCStringPtr(cfStr, kCFStringEncodingUTF8); ``` 注意,CFStringRef和NSString并不是相同的类型,它们只是在Core Foundation和Foundation框架中相互转换的对象。因此,在使用CFStringRef时需要遵循Core Foundation的内存管理规则,如需要手动释放内存等。 ### 回答2: 要将NSString*转换为struct __CFString *,可以使用桥接(bridge)方法来实现。 在Objective-C中,NSString和CFString是可以相互转换的,因为它们彼此兼容。所以我们可以使用__bridge动态转换不同的指针类型。 具体步骤如下: 1. 首先,引入CoreFoundation框架,因为CFString是属于CoreFoundation的一部分。在文件头部添加以下代码: ```objc #import <CoreFoundation/CoreFoundation.h> ``` 2. 在需要进行转换的地方,使用__bridge关键字进行类型转换。代码如下: ```objc NSString *str = @"Hello, World!"; struct __CFString *cfString = (__bridge struct __CFString *)str; ``` 这样,通过将NSString指针转换为struct __CFString指针,就可以实现类型的转换。 需要注意的是,这种转换是一个桥接操作,不会进行实际数据的复制,只是让两种类型的指针相互引用同一块内存区域。 另外,为了避免出现内存管理问题,如果CFString在ARC环境下使用,还需要使用__bridge_retained进行转换,确保CFString对象在不再使用时被正确释放。 总结起来,NSString*转换为struct __CFString*的关键在于使用__bridge关键字进行转换操作。 ### 回答3: NSString*转换为struct __CFString *的方法是通过桥接(bridging)操作来实现的。在Objective-C中,可以使用__bridge关键字将NSString对象转换为指向CFStringRef的指针。 具体转换方法如下: 1. 首先,要确保项目中引入了CoreFoundation框架。 2. 使用__bridge关键字将NSString对象转换为struct __CFString *。代码示例如下: NSString *myString = @"Hello World"; struct __CFString *myCFString = (__bridge struct __CFString *)myString; 在上述代码中,myString是一个NSString对象,将其转换为struct __CFString *类型的指针myCFString。 3. 进行转换后,可以使用CFStringRef的函数和方法来操作myCFString。例如,可以使用CFStringGetLength()函数来获取myCFString的长度。 CFIndex length = CFStringGetLength(myCFString); 需要注意的是,转换后的指针只是一个指向NSString对象底层CoreFoundation框架中的数据结构的指针,并不是一个完全独立的对象。因此,在操作完myCFString后,不需要手动释放内存,系统会自动处理内存管理。 总之,通过使用__bridge关键字,可以方便地将NSString*转换为struct __CFString *类型的指针,从而在需要使用CoreFoundation框架的场景中使用NSString对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值