1672D - Cyclic Rotation

这篇博客介绍了一种使用逆向思维和双指针技术解决Cyclic Rotation问题的方法。通过对数组b进行旋转,判断是否能转换成数组a。通过维护一个multiset来检查元素出现情况,并更新指针i和j,最终确定能否完成旋转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1672D - Cyclic Rotation

逆向思维:

b 能否转成 a ?

选择一个位置 j, b j − 1 = = b j b_{j-1} == b_j bj1==bj, 那么可以把 j-1 这个位置的值放到前面去

双指针实现:

i f ( b j = = a i ) if(b_j ==a_i) if(bj==ai) i ++, j ++;

i f ( b j = = b j − 1 ) if(b_j == b_{j-1}) if(bj==bj1) 在mutiset中寻找是否出现过 b j − 1 b_{j-1} bj1 ,如果出现过意味着可以把它插入之前的某个位置

e l s e   c n t [ a [ i ] ] + + else \ cnt[a[i]]++ else cnt[a[i]]++ 插入到multiset 中去,检查后边是否出现过?

	while(T--)
	{
		int n;
		cin >> n;
		vector<int> a(n), b(n), cnt(n+1,0);
		for(auto& x : a) cin >> x;
		for(auto& x : b) cin >> x;
		int i = 0, j = 0;
		bool flag = true;
		while(j < n) {
			if(i < n && j < n && a[i] == b[j]) {i++; j++;continue;}
			if(cnt[b[j]] > 0 && b[j] == b[j-1]) cnt[b[j++]]--;
			else if(i < n) { cnt[a[i++]]++; }
			else {flag = false; break; }
		}
		if(flag) cout << "YES" << endl;
		else cout << "NO" << endl;
	}

#!/usr/bin/env python3 import logging import os import outlier import visualize from datetime import datetime from extract_config import get_pandas_sqlinfo, get_pandas_sqlinfo_dynamic_status from config import LOG_DATA_PATH, PP_LOG_DIR, LOG_LEVEL_POSTPROCESSING_PIPELINE def get_changed_firmwares(): changed_firmwares = [] try: automation_runs = get_pandas_sqlinfo_dynamic_status("SELECT * FROM automation_runs ORDER BY run_id DESC LIMIT 1000") automation_runs = automation_runs.sort_values(['run_id', 'firmware']).groupby('firmware').tail(2) df = automation_runs.drop_duplicates(subset=["firmware"]) changed_firmwares = list(df["firmware"]) except: logging.exception("could not fetch last firmwares from db") return changed_firmwares def generate_outlier_and_visualization(current_firmware): logging.info(f"Starting Cyclic Data Generation for {current_firmware}") print(f"Generating Outlier for {current_firmware}") outlier.outliergenerator(current_firmware) print(f"Generating Visualizations for {current_firmware}") visualize.run_visualization("", current_firmware) logging.info(f"performed cyclic generation of outlier and statistic plots for {current_firmware}") print("Finished outlier calculation!") if __name__ == "__main__": now = datetime.now() now_dtstring = now.strftime("%d-%m-%Y-%H-%M-%S") os.makedirs(os.path.join(PP_LOG_DIR, "outlier_logs"), exist_ok=True, mode=0o775) logfile = os.path.join(PP_LOG_DIR, "outlier_logs", f"outlier.log") #For now, only create one logfile. This avoids bloat. Also, with outlier calculation, usually only the most recent call is relevant. #TODO: Write single comprehensive logfile rotation handler for all logfiles! if os.path.exists(logfile): os.remove(logfile) fh = logging.FileHandler(logfile) sh = logging.StreamHandler() logger = logging.basicConfig(level=LOG_LEVEL_POSTPROCESSING_PIPELINE, format='%(asctime)s:%(levelname)s:%(message)s', handlers=[ fh, sh] ) if firmware_override := os.environ.get("OUTLIER_FIRMWARE", None): logging.info(f"Outlier calculation for {firmware_override}") generate_outlier_and_visualization(firmware_override) else: for firmware in get_changed_firmwares(): logging.info(f"Outlier calculation for {firmware}") try: generate_outlier_and_visualization(firmware) except: logging.exception(f"Could not generate outlier for {firmware}")
最新发布
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值