跳到正文

个人记录

2025.05.28训练2

yangchengrun约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年5月30日
收录
2026年5月17日
更新
2026年10月5日
篇幅
约 4 分钟

版本

  1. v2当前
  2. v1

T1

C++
/*
author:webnie
email:webnie@foxmail.com
time:2025-05-29 15:21:27
problem:甯傝禌T3 spy.cpp
*/

#include<bits/stdc++.h>
using namespace std;

int T,n,k;
int h[100010],a[100010],b[100010],tmp[100010],dp[100010];
int INF=0x3f3f3f3f;
int suit[5];
int LIS(){

	fill(dp+1,dp+n+1,INF);
	for(int i=1;i<=n;i++) *upper_bound(dp+1,dp+n+1,tmp[i]) = tmp[i];
	int len = lower_bound(dp+1,dp+n+1,INF) - dp-1;//姹傜殑鏈€闀夸笉涓嬮檷瀛愬簭鍒楅暱搴?
	return n-len;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);cout.tie(nullptr);
	cin>>T;
	while(T--){


		cin>>n>>k;
		for(int i=1;i<=n;i++) cin>>h[i];
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			b[i]=a[i];
		}

		sort(b+1,b+n+1);
		int len=unique(b+1,b+n+1)-b-1;
		for(int i=1;i<=n;i++)
			a[i]=lower_bound(b+1,b+len+1,a[i])-b-1;
		//鍊熷姪b鏁扮粍鎶奱鏁扮粍璧嬪€兼垚绂绘暎鍖栧悗鐨勬暟鍊硷紝鑼冨洿鍦?e5浠ュ唴

		for(int i=1;i<=k;i++) suit[i]=i;
		//鍒濆鍖栫墝鐨勮姳鑹叉暟缁?
		int ans=INF;
		//鍒濆鍖栫瓟妗?
		do{
			for(int i=1;i<=n;i++) {
				for(int j=1;j<=k;j++) if(suit[j]==h[i]) tmp[i]=a[i]+100000*(j-1);
			}


			ans = min(ans,LIS());

		}while(next_permutation(suit+1,suit+k+1));
		//閫氳繃鑺辫壊鏁扮粍鐨勬帓鍒楋紝閫氳繃瀵瑰簲鑺辫壊鐨勫簭鍙峰绂绘暎鍖栫殑a鏁扮粍杩涜hash锛?
		//鐒跺悗姹俬ash鍚庣殑鏁扮粍temp[]鐨勬渶闀夸笉涓嬮檷瀛愬簭鍒楃殑闀垮害鍜宯鐨勫樊鍊笺€?

		cout<<ans<<endl;

	}
	return 0;
}

T2

C++
#include<bits/stdc++.h>
using namespace std;

int n;
int h[100010];
stack<int> st;


int main() {
	cin>>n;
	for(int i=1;i<=n;i++) cin>>h[i];
	int ans=0;
	st.push(0);
	//st.push(h[1]);
	for(int i=1;i<=n;i++) {
		while(!st.empty()&&st.top()>h[i]) st.pop(),ans++;
		if(!st.empty()&& st.top()<h[i]) st.push(h[i]);

	}
	cout<<ans+st.size()-1<<'\n';

	return 0;
}

T3

C++
#include<bits/stdc++.h>
using namespace std;

int n,k;
int a[1000010];
deque<int> dq;


int main() {
	cin>>n>>k;
	for(int i=1;i<=n;i++) cin>>a[i];

	for(int i=1;i<=n;i++) {
	 while(!dq.empty() && i-dq.front()>=k) dq.pop_front();
	 while(!dq.empty() && a[dq.back()]> a[i])dq.pop_back();
	 dq.push_back(i);
	 if(i>=k)cout<<a[dq.front()]<<" ";
	}

	cout<<'\n';
	dq.clear();

	for(int i=1;i<=n;i++) {
		while(!dq.empty() && i-dq.front()>=k) dq.pop_front();
		while(!dq.empty() && a[dq.back()]< a[i])dq.pop_back();
		dq.push_back(i);
		if(i>=k)cout<<a[dq.front()]<<" ";
	}

	return 0;
}
原文