PAT_1067. Sort with Swap(0,*)

#include <iostream>
using namespace std;
int a[100005],position[100005],N,inPositionNum=0,count=0,SwapTo=0;
int FindFirst(int begin)
{
	for(int i=begin;i<N;i++)
		if(a[i]!=i)
			return i;
	return -1;
}
int main() 
{ 
	scanf("%d",&N); 
	for(int i=0;i<N;i++) 
	{ 
		scanf("%d",&a[i]); 
		position[a[i]]=i; 
	} 
	while(FindFirst(SwapTo)!=-1) 
	{ 
		if(a[0]==0) 
		{ 
			SwapTo = FindFirst(SwapTo); 
			if(SwapTo == -1) 
				break; 
			a[0] = a[SwapTo]; 
			a[SwapTo] = 0; 
			
			position[0] = SwapTo; 
			position[a[0]] = 0; 

		} 
		else 
		{ 
			a[position[0]] = position[0]; 
			a[position[position[0]]]=0; 

			int temp = position[0];
			position[0] = position[temp];
			position[temp] = temp;
		}
		count++;
	}
	printf("%d",count);
	return 0;
}
		

  

输入:10 3 5 7 2 6 4 9 0 8 1
输出:9

一道模拟题,细节注意就能过,太久没做题,这题做了很久,也参考了网上的代码,但最终还是修改自己的第一份代码过的。

整体思路:手工操作一次就能明白模拟顺序是这样的,用数组a[]保存输入数据,即要被排序的序列,用数组position[]保存输入数据的下标,

如position[3]=0表示元素3在原始数组中的下标为0。

然后不断地去交换0和当时0所在位置应该填入的元素,直到0被换到下标为0的位置时,要进行一次废操作,即把0与一个位置摆放不对的元素进行交换。

整个循环结束的时机就是当原始数组中不存在a[i]!=i的情况。


一开始超时,后来看了别人的代码,对FindFirst函数进行了优化,就不超时了,不需要每一次都从0开始检测,这里还不是很明白,回头再看看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include "lib_prot_pd.h" #include "mod_input_output.h" #include "mod_display.h" #include "mod_queue.h" #include "app_typec_deal.h" #include "app_init.h" #include "lib_multi_protocol.h" uint8_t typeca_ufp= 0; void typeca_hard_reset_cb(void) { static uint8_t b_typec_hard_reset_lock= RESET; if(h_pd.output.b_source_read_hard|| \ h_pd.output.b_sink_read_hard) { if(b_typec_hard_reset_lock== RESET) { b_typec_hard_reset_lock= SET; mod_queue_send(PLUG_CA_RST_P); } }else { if(b_typec_hard_reset_lock) { b_typec_hard_reset_lock= RESET; mod_queue_send(PLUG_CA_RST_N); } } } void typecb_hard_reset_cb(void) { static uint8_t b_typec_hard_reset_lock= RESET; if(h_pd.output.b_source_read_hard|| \ h_pd.output.b_sink_read_hard) { if(b_typec_hard_reset_lock== RESET) { b_typec_hard_reset_lock= SET; mod_queue_send(PLUG_CB_RST_P); } }else { if(b_typec_hard_reset_lock) { b_typec_hard_reset_lock= RESET; mod_queue_send(PLUG_CB_RST_N); } } } void typeca_pr_swap_cb(void) { static uint8_t b_pd_power_role_swap_lock; if(h_pd.output.b_pr_swap_en) { if(h_pd.output.b_source_read_pr_swap) { b_pd_power_role_swap_lock= SET; /*用户代码*/ //关 VBus }else if(h_pd.output.b_sink_read_pr_swap) { /*用户代码*/ //DC-DC 进入放电状态,升压,打开 VBus } }else { b_pd_power_role_swap_lock= RESET; } } void typecb_pr_swap_cb(void) { static uint8_t b_pd_power_role_swap_lock; if(h_pd.output.b_pr_swap_en) { if(h_pd.output.b_source_read_pr_swap) { b_pd_power_role_swap_lock= SET; /*用户代码*/ //关 VBus }else if(h_pd.output.b_sink_read_pr_swap) { /*用户代码*/ //DC-DC 进入放电状态,升压,打开 VBus } }else { b_pd_power_role_swap_lock= RESET; } } bool typeca_vbus_exist_cb(void) { //判断一下0.8V VBus电压 return mod_io_typeca_acin(); } bool typecb_vbus_exist_cb(void) { //判断一下0.8V VBus电压 return mod_io_typecb_acin(); } void typeca_attached_src_cb(void) { /** * !!! * 兼容性测试需要 */ mod_io_a1d_out(); mod_queue_send(PLUG_CAD_IN); } void typecb_attached_src_cb(void) { mod_io_a1d_out(); mod_queue_send(PLUG_CBD_IN); } void typeca_attached_snk_cb(void) { mod_queue_send(PLUG_CAC_IN); typeca_ufp = 1; } void typecb_attached_snk_cb(void) { mod_queue_send(PLUG_CBC_IN); typeca_ufp = 1; } void typeca_unattached_cb(void) { mod_queue_send(PLUG_CAX_OUT); typeca_ufp = 0; } void typecb_unattached_cb(void) { mod_queue_send(PLUG_CBX_OUT); }
06-06

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值