1.24今日总结

上午学习了快排。

快排的平均时间复杂度:nlog2 n。

最复杂的情况:每次所选的基准数是当前序列中的最大或最小元。这样,长度为n的数据表的快速排序需要经过n趟划分,使得整个排序算法的时间复杂度为n的平方。

快排有四种优化方法。

1.取中间值

在选取基准数时,通过比较第一个元素,最后一个元素,和中间位置元素的数据,找到在两者之间的数作为基准数。

2.当分割后的数据较少时改用直接插入排序

当排序的数据过小的时候,采用直接插入排序会更简单,直接插入排序平均复杂度为n的平方。

直接插入排序模板:

	for(int i=1;i<10;i++)
	{
		j=i;
		t=a[i];
	 while(j>0&&a[j-1]>t)
	 {
	 	a[j]=a[j-1];
	 	j--;
			 }
		a[j]=t; 		
	 }

排序10个数。每次向前甄选小大于a[i]的数,并进行交换。

3.将与基准数相同的数聚集在一起

即把与基准数相同的数据放在基准数附近

排序前6 4 6 7 1 6 7 6 8 6

排序后1 4 6 6 6 6 6 7 8 7

4.优化递归操作

下午补了P7939 [B1] Alice Wins! (easy version)这个题

P7939 [B1] Alice Wins! (easy version)

题目描述

The difference between the versions is the limit of operations.

Alice is a cute girl who has a lot of dolls.

There are 4\cdot n4⋅n dolls playing rock-paper-scissors. They are divided into two teams: Team A and Team B. Each team contains 2\cdot n2⋅n dolls.

A total of 2\cdot n2⋅n rounds of the game will be played. In the ii-th round, the ii-th doll in Team A will play against the ii-th doll in Team B. If the doll in Team A wins, Team A will get 11 point. If it loses, Team A will lose 11 point. If it ties, Team A will not get points.

Alice knows all the dolls' choices in this game. To be precise, she uses two arrays aa and bb to represent the choices of the dolls in the two teams. a_iai​ means the choice of the ii-th doll in Team A, and b_ibi​ means the choice of the ii-th doll in Team B. In this question, we use 11 for rock, 22 for scissors, and 33 for paper.

Now for each team, Alice wants to change the choices of at most nn dolls to make the score of Team A as high as possible.

Find the maximum score of Team A and its construction method. If there are multiple answers print any of them (you still have to maximize the score of Team A).

输入格式

Each test contains multiple testcases. The first line contains an integer TT, the number of test cases.

For each test case, the first line contains one integer nn.

Then two lines follow, containing an array aa of length 2\cdot n2⋅n and an array bb of length 2\cdot n2⋅n, respectively.

输出格式

For each test case, print three lines.

The first line contains one integer, the maximum score of Team A.

The second line contains an array a'a′ of length 2\cdot n2⋅n, which represents the aa array after Alice's modification. For integers 11 to 2\cdot n2⋅n, if a_i \ne a'_iai​=ai′​, then it means you have modified the choice of one player in Team A.

The third line contains an array b'b′ of length 2\cdot n2⋅n, which represents the bb array after Alice's modification. For integers 11 to 2\cdot n2⋅n, if b_i \ne b'_ibi​=bi′​, then it means you have modified the choice of one player in Team B.

题意翻译

AB 每队 2n2n 人正在玩石头剪刀布。A 队第 ii 个人出 a_iai​,B 队第 ii 个人出 b_ibi​。编号相同的人会对战。若 A 队赢则加一分,平不得分,输扣一分。你可以至多改变每队 nn 个人的出拳方案,使得 A 队的得分最高。输出得分的最大值和任意一组构造方案。

本题中,我们用 11 代表石头,22 代表剪刀,33 代表布。

输入输出样例

输入 #1复制

2
1
1 2
1 2
2
2 3 1 3
1 2 2 1

输出 #1复制

2
1 1
2 2
4
3 1 1 3
1 2 2 1

说明/提示

Explanation

For the first test case, we can change a_2a2​ to 11 and b_1b1​ to 22. Then Team A can get 22 points. It can be proved that this is the maximum score that Team A can get.

For the second test case, we can change a_1a1​ to 33 and a_2a2​ to 11.

Constraints

1\le T,n \le 10^5;\ 1\le a_i,b_i \le 31≤T,n≤105; 1≤ai​,bi​≤3. The sum of nn over all test cases \le 10^5≤105.

题目意思是最多通过n次改变a中出的方式,最多改变b出的方式来达到a取得分数最多。

这个题采用我开始采用穷举法,即把每种情况列出来,但是有问题

错误代码

#include<stdio.h>
int a[1000000];
int b[1000000];
int main()
{
	int n;
	scanf("%d",&n);
	for(int j=0;j<n;j++)
	{
		int x;
		int k=0;
		int d=0;
		int sum=0;
		scanf("%d",&x);
		for(int i=1;i<=2*x;i++)
		scanf("%d",&a[i]);
		for(int i=1;i<=2*x;i++)
		scanf("%d",&b[i]);
		for(int i=1;i<=2*x;i++)
		{
			if(k<x)
			{
				if(a[i]==b[i]&&a[i]!=1)
				{
					a[i]-=1;
					k++;
					continue;
				}
			    if(a[i]==b[i]&&a[i]==1)
			    {
			    	a[i]=3;
			    	k++;
			    	continue;
				}
				if(b[i]-a[i]==1||a[i]-b[i]==2)
				continue;
			     if(a[i]==1&&b[i]==3)
			     {
			     	a[i]=2;
			     	k++;
			     	continue;
				 }
				 if(a[i]==2&&b[i]==1)
				 {
				 	a[i]=3;
				 	k++;
				 	continue;
				  } 
				 if(a[i]==3&&b[i]==2)
				 {
				 	a[i]=1;
				 	k++;
				 	continue;
				  } 
			}
			if(k==x)
			{ 
			  if(d<x)
			  {
			  	if(a[i]==b[i]&&a[i]!=3)
				{
					b[i]+=1;
					d++;
					continue;
				}
			    if(a[i]==b[i]&&a[i]==3)
			    {
			    	b[i]=1;
			    	d++;
			    	continue;
				}
				if(b[i]-a[i]==1||a[i]-b[i]==2)
					continue;
			     if(a[i]==1&&b[i]==3)
			     {
			     	b[i]=2;
			     	d++;
			     	continue;
				 }
				 if(a[i]==2&&b[i]==1)
				 {
				 	b[i]=3;
				 	d++;
				 	continue;
				  } 
				 if(a[i]==3&&b[i]==2)
				 {
				 	b[i]=1;
				 	k++;
				 	continue;
				  } 
			  }
				}
		}
		printf("%d\n",2*x);
		for(int i=1;i<=2*x;i++)
		printf("%d ",a[i]);
		printf("\n");
		for(int i=1;i<=2*x;i++)
		printf("%d ",b[i]);
		printf("\n"); 
	}
 } 

我认为这应该没有问题,把平局,输局,赢局三种情况列举出来.

但还是不太行。

后面采用的代码才行:

#include<stdio.h>
int a[1000000];
int b[1000000];
int main()
{
	int n;
	scanf("%d",&n);
	for(int j=0;j<n;j++)
	{
		int x;
		scanf("%d",&x);
		for(int i=1;i<=2*x;i++)
		scanf("%d",&a[i]);
		for(int i=1;i<=2*x;i++)
		scanf("%d",&b[i]);
		printf("%d\n",2*x);
		for(int i=1;i<=x;i++)
		{
		if(b[i]==1)
		a[i]=3;
		if(b[i]==2)
		a[i]=1;
		if(b[i]==3)
		a[i]=2; 
	}
	     for(int i=x+1;i<=x*2;i++)
	     {
		 if(a[i]==1)
	     b[i]=2;
	     if(a[i]==2)
	     b[i]=3;
	     if(a[i]==3)
	     b[i]=1;
	}
	 for(int i=1;i<=2*x;i++)
	 printf("%d ",a[i]);
	 printf("\n"); 
	 for(int i=1;i<=x*2;i++)
	 printf("%d ",b[i]);
	 printf("\n");
 } 
}

即前n个数据,不论a中的值,判断b中的值,对a进行改变。

后n个数据,不论b中的值,判断a中的值对b进行改变。

今日总结:今天学习比较少,主要是在理解,而且复习了之前学习的直接插入排序,感觉印象更加深刻了。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Super TreeView 1.24是一个强大的、高度可定制的树状视图控件。它是基于TreeView控件的扩展,提供了更多的功能和样式选择。 Super TreeView 1.24具有以下主要特点: 1. 多级树状结构:Super TreeView 1.24支持多级的树状结构,可以轻松创建具有父子关系的节点。这使得它成为展示复杂数据结构的理想选择。 2. 数据绑定:它可以与各种数据源进行绑定,包括数据库、XML文件等。通过数据绑定,我们可以轻松地将数据呈现为树状结构,而无需手动创建和管理节点。 3. 界面样式定制:Super TreeView 1.24提供了丰富的样式选择,允许用户自定义节点的外观和样式。我们可以更改节点的背景颜色、字体、图标等,以实现各种不同的视觉效果。 4. 交互功能:它支持各种交互操作,例如展开/折叠节点、选中节点、拖放节点等。这些功能使得用户能够轻松地与树状结构进行交互,并对树的状态进行操作。 5. 编辑功能:Super TreeView 1.24还具有编辑功能,允许用户在树状结构中直接编辑节点的文本内容。这对于需要在运行时动态更新节点的应用程序非常有用。 总之,Super TreeView 1.24是一个功能强大且高度可定制的树状视图控件。它提供了多种功能和样式选择,使用户能够轻松地创建、管理和操作树形数据结构。无论是在桌面应用程序还是Web应用程序中,Super TreeView 1.24都是一个强大而实用的工具。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值