4.15学长自选题

D题
题意:
把给定的一个数字数列放到对角线上,其他位置填写min(横,竖)。
要求找到一个矩形,分别把所有的1,2,3....k 都包含起来
输出其长+宽
思路:
找到最远的那个即可,然后(mx - mn + 1) * 2

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;

int t , n , k , ans[N];
struct node {
	int x , id;
}a[N];

signed main(){

	IOS
	cin >> t;
	
	while(t --) {
		cin >> n >> k;
		for(int i = 1 ; i <= n ; i ++) {
			cin >> a[i].x ;//输入数据 
			a[i].id = i;//记录在对角线上的位置 
		}
		sort(a + 1 , a + 1 + n ,
			[&](node a, node b){
				return a.x < b.x;
			}//按照数据的大小来排序 
		);
		int mx = -9e18 , mn = 9e18;
		for(int i = n ; i >= 1 ; i --){
			int now = a[i].x;//最大值, 
			int id  = a[i].id;//最大值的位置 
			mx = max(mx , id);//x的列 
			mn = min(mn , id);//x的行 
			ans[now] = (mx - mn + 1) * 2; 
		}
		
		for(int i = 1 ; i <= k ; i ++) {
			cout << ans[i] << " ";
			ans[i] = 0;
		}
		cout << "\n";
	}

	return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);

posted @ 2024-04-15 23:15  涤生yang  阅读(10)  评论(0)    收藏  举报