codeforces 1197A. DIY Wooden Ladder ,B. Pillars, C. Array Splitting

A. DIY Wooden Ladder

题目链接:codeforces 1197A

题意:

   给出n块木板,问能组成的梯子最多有多少节,

题解:

   选最长的两块木板作为两边,其余都可以作为踏板,然后比较下就可以了

#include 
using namespace std;
int main(){
	int t;
	cin >> t;
	while(t--){
		int n, a[200005];
		cin >> n;
		for(int i = 1; i <= n; i++){
			cin >> a[i];
		}
		sort(a+1, a+1+n);
		cout << min(a[n-1]-1, n-2) << endl;
	}
	return 0;
} 

B. Pillars

题目链接: codeforces 1197B

题意:

   给出n个盘子半径,只能将小盘子放在大盘子上,问能否最终将所有盘子放在一个上面

题解:

    模拟,找出最大的,然后用两个指针向左右进行操作

#include
using namespace std;
#define ll long long
int main(){
   int n, m;
   while(cin >> n){
   	int a[500005], maxx = 0, k = 0, L = 0, R = 0, f = 1;
   	for(int i = 1; i <= n; i++){
   		cin >> a[i];
   		if(a[i] > maxx){
   			maxx = a[i];
   			k = i;
			}
		}
		L = k - 1; R = k + 1;
		a[0] = 0;
		a[n+1] = 0;
		while(1){
			if(L == 0 && R == n+1){    // 操作完毕的情况
				break;
			}
			if(a[L] >= a[k] || a[R] >= a[k]){  // 两边盘子都大于要放的盘子,不成立
				f = 0;
				break;
			}
			else{
				if(a[L] > a[R] && L > 0){  // 向左
					k = L;
					L--;
				}
				else{     // 向右
					k = R;
					R++;
				}
				
			}
		}
		if(f == 1 && L == 0 && R == n+1){
			cout << "YES" << endl;
		}
		else{
			cout << "NO" << endl;
		}
	}
	return 0;
}

C. Array Splitting

题目链接:codeforces 1197C

题意:

   给定n个从小到大排好序的数组,将其分为k个非空相邻的子数组,每组的值为 最大值减去最小值,求最小总和

题解:

   如果不分组,那么就是数组最大减去数组最小, 也就是相邻的元素之间的差之和

  贪心的思想,将相邻的元素之差排序,减去后k个就是答案

#include 
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 5;
ll a[maxn], d[maxn];
int main(){
	int n, k;
	ll ans = 0;
	cin >> n >> k;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		d[i] = a[i] - a[i-1];
		ans += d[i];
	}
	ans = ans - d[1];
	d[1] = 0;
	sort(d+1, d+1+n);
	k--;
	while(k--){
		ans = ans - d[n];
		n--;
	}
	cout << ans << endl;
	return 0;
} 

   

你可能感兴趣的:(codeforces)