CodeForces Good Bye 2020 A-D
阿新 • • 發佈:2021-01-01
A. Bovine Dilemma
題目分析
題意:給你一組數,看他們兩兩組合的差有多少種情況 。
那麼直接求出兩兩組合的差,然後去重即可得出答案。去重既可以選擇開一個數組標記,也可以選擇用STL的unique實現
AC程式碼
#include<iostream> #include<cstring> using namespace std; const int N = 1e6 + 100; int q[N], T, n, ans; bool s[N];//陣列標記 int main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> T; while(T--){ memset(s, 0, sizeof(s)); cin >> n; ans = 0; for(int i = 0; i < n; i++) cin >> q[i]; for(int i = 1; i < n; i++){ for(int j = 0; j < i; j++) if(!s[q[i] - q[j]]) s[q[i] - q[j]] = 1, ans++; } cout << ans << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; const int N = 1e6 + 1000; int q[N]; vector<int> arr;//vector儲存,然後unqiue去重 int T, n, m; int main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> T; while(T--){ cin >> n; for(int i = 0; i < n; i++) cin >> q[i]; for(int i = 1; i < n; i++) for(int j = 0; j < i; j++) arr.push_back(q[i] - q[j]); sort(arr.begin(), arr.end()); arr.erase(unique(arr.begin(), arr.end()), arr.end()); cout << arr.size() << endl;//最後輸出去重後剩下的元素的數量 arr.clear(); } return 0; }
B. Last minute enhancements
題目分析
題意:給你一組數字,你能給每一個數字加上1,問你進行多次操作後最多能讓這組數字有幾個不同的數字
我們只需要遍歷陣列,如果數字未出現過,那麼給它標記一下,然後ans++;如果數字出現過,再看看它加上1之後有沒有出現過,如果加上1之後沒有出現過,那麼標記一下,ans++, 最終輸出答案。
注意
- 注意x的資料範圍
- 本題的解題思路有一個前提:所給的資料是有序的。雖然給的樣例是有序的陣列,但是題目並沒有保證測試資料會給有序的陣列
- 每一次詢問結束之後清空標記的陣列,同時也要選擇適當的陣列,防止TLE
AC程式碼
#include<bits/stdc++.h> using namespace std; const int N = 1e6 + 1000; int q[N]; bool s[N]; int T, n; int main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> T; while(T--){ memset(s, 0, sizeof(s)); ans = 0; cin >> n; for(int i = 0; i < n; i++) cin >> q[i]; sort(q, q + n); for(int i = 0; i < n; i++){ if(!s[q[i]]) s[q[i]] = 1, ans++;//不改變值的情況 else if(!s[q[i] + 1]) s[q[i] + 1] = 1, ans++;//加上1的情況 } cout << ans << endl; } return 0; }
C. Canine poetry
題目分析
題意:給幾個字串,問操作幾次能讓字串所有的字串不構成迴文
這道題是一道貪心題目,我們只需考慮aa
和 aba
這兩種情況,用一個非小寫字母破壞掉就可以達成目的了
AC程式碼
#include<bits/stdc++.h>
using namespace std;
int T, ans;
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> T;
while(T--){
ans = 0;
string str;
cin >> str;
for(int i = 1; i < str.length(); i++){
if(str[i] == str[i - 1]){
str[i] = '?', ans++;
}
else if(str[i] == str[i - 2] && i > 1){//小細節要處理好
str[i] = '?', ans++;
}
}
cout << ans << endl;
}
return 0;
}
D. 13th Labour of Heracles
題目分析
本題應對點考慮貢獻度,點權越大的點計算次數越多,對於答案的貢獻度也就越多(答案的值也就越大),所以應該採用貪心策略,從最大的權值開始,同時記錄一下點出現的情況。詳情請見程式碼
AC程式碼
//qwq
#include<bits/stdc++.h>
using namespace std;
const int N = 100000+50;
int T,n,m;
long long ans;
struct node{
int d,a;
}tree[N];
bool cmp(node x,node y){
return x.a>y.a;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n); ans=0;
memset(tree,0,sizeof(tree));
for (int i=1; i<=n; ++i){
scanf("%d",&tree[i].a); ans+=tree[i].a;
}
for (int i=1,q1,q2; i<n; ++i){
scanf("%d%d",&q1,&q2);
tree[q1].d++; tree[q2].d++;
}
sort(tree+1,tree+n+1,cmp);
printf("%lld",ans);
for (int i=1; i<=n; ++i){
while (tree[i].d>1){
ans+=tree[i].a;
tree[i].d--;
printf(" %lld",ans);
}
}
cout << endl;
}
return 0;
}