1. 程式人生 > 實用技巧 >Codeforces Goodbye2020部分題解

Codeforces Goodbye2020部分題解

Problem A

問有多少不同面積的三角形,資料量較小,那麼我們暴力掃過去丟set就好了。

Problem B

首先我掃完一邊後一定是有些位置被站住了,那麼根據桶排的思想,每個位置可能存在多個,那麼這些多個我們要使它儘可能的多做貢獻,就貪心的往後加,最後統計一共有多少位置就可以了。

Problem C

最少要多少次操作可以破環原串的迴文性質,那麼模擬可以發現,一個迴文串只要超出了3,那麼他肯定存在字串長度為2或者3的迴文串,那麼我們只要掃一遍並且在過程中破壞2和3的迴文串就可以了。

Problem D

圖論題面,其實是思維題,我們統計度數,然後每次在度數還允許的條件下加上點權,可以最優化答案。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
void check_max (int &a,int b) {a=max (a,b);}
void check_min (int &a,int b) {a=min (a,b);}
typedef long long ll;
const int maxn=1e5+10;
ll w[maxn];
ll degree[maxn];
ll v[maxn];
vector <ll> ans;

inline bool cmp (int x,int y) {
	return x>y;
}

int main () {
	int t; scanf ("%d",&t);
	while (t--) {
		int n; scanf ("%d",&n);
		ll now=0;
		ans.clear ();
		for (int i=1;i<=n;i++) scanf ("%lld",&w[i]),now+=w[i];
		for (int i=1;i<=n-1;i++) {
			int x,y; scanf ("%d%d",&x,&y);
			degree[x]++;
			degree[y]++;
		}
		int cnt=0;
		for (int i=1;i<=n;i++) 
			for (int j=1;j<degree[i];j++) v[++cnt]=w[i];
		sort (v+1,v+cnt+1,cmp);
		for (int i=1;i<n;i++) printf ("%lld ",now),now+=v[i];
		printf ("\n");
	}
	return 0;
}