Typical90编码技巧总结
f - Smallest Subsquence
标签:贪心、模拟
学到的编码技巧:
while(x--)可以执行循环体x次。
Final Code:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int n, m, k;
signed main() {
cin >> n >> k;
string s;
cin >> s;
int pos = 0;
while(k--) {
char x = 'z' + 1;
for(int i = pos ; i < n - k ; i++) if(s[i] < x) x = s[i], pos = i;
cout << x; pos++;
}
return 0;
}av - I will not drop out
标签:贪心、排序
学到的编码技巧:
-
nth_element函数可以 将第 小的数放到正确位置上,并使左边的数小于它、右边的树大于它。在不关心顺序的情景下(如求和)可以替代sort。 -
accumulate函数可以 求出一段区间的和。好用,无需多言。要注意第三个参数会决定函数的返回值类型,如果希望返回long long,那么应该填入0ll。
Final Code:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, k;
cin >> n >> k;
vector<ll> a;
for(int i = 1, x, y ; i <= n ; i++) {
cin >> x >> y;
a.push_back(y), a.push_back(x - y);
}
nth_element(a.begin(), a.begin() + k, a.end(), greater<>());
cout << accumulate(a.begin(), a.begin() + k, 0ll) << '\n';
return 0;
}am - Tree Distance
标签:树上问题、组合计数
学到的编码技巧:
-
在
for循环中,可以把其它变量跟着i一起声明,在condition部分输入。 -
fill_n函数可以 将一段区间的内容统一填充成一个值。有时候比较好用。
Final Code:
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, ans, siz[N];
vector<int> a[N];
void dfs(int u, int f) {
for(int v : a[u]) {
if(v == f) continue;
dfs(v, u), siz[u] += siz[v];
}
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1, u, v ; i < n && cin >> u >> v ; i++) a[u].push_back(v), a[v].push_back(u);
fill_n(siz + 1, siz + n + 1, 1), dfs(1, 0);
for(int i = 1 ; i <= n ; i++) ans += siz[i] * (n - siz[i]);
cout << ans << '\n';
return 0;
}t - Log Inequality
标签:数学、快速幂
学到的编码技巧:
-
善用三目运算符,要注意它的优先级较低。
-
快速幂的简洁新写法。如果要取模可以这么写:
int fpow(int a, int b) {
int res = 1;
for(a %= mod ; b ; (res *= a) %= mod, (a *= a) % mod);
return res;
}其中最亮眼的莫过于对于 a = (a + b) % mod 类式子的简化:(a += b) %= mod。
Final Code:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int fpow(int a, int b) {
int res = 1;
for( ; b ; (res *= a), (a *= a));
return res;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int a, b, c;
cin >> a >> b >> c;
cout << (a < fpow(c, b) ? "Yes" : "No") << endl;
return 0;
}g - CP Classes
标签:二分
学到的编码技巧:
- 如果只是在二分查找某个数组的后继,那么可以直接用
lower_bound和upper_bound函数。如果是找前驱,那么减去 即可。要注意返回.end()或者a + n + 1的特殊情况。
Final Code:
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, q; cin >> n;
vector<int> a(n);
for(int i = 0 ; i < n ; i++) cin >> a[i];
sort(a.begin(), a.end());
cin >> q;
for(int i = 1, x ; i <= q ; i++) {
cin >> x;
int p = lower_bound(a.begin(), a.end(), x) - a.begin();
int ans = INT_MAX;
if(p < n) ans = min(ans, abs(a[p] - x));
if(p > 0) ans = min(ans, abs(a[p - 1] - x));
cout << ans << '\n';
}
return 0;
}b - Encyclopedia of Parentheses
标签:枚举、栈
学到的编码技巧:
-
我所使用的是状压枚举情况的写法。核心科技是
kth(i, j)求出二进制状态i的第j位(0-index)。 -
这题也可以用
next_permutation函数编码。该函数的使用方法通常是:do{statements;} while(nex_permutation(start, end));。并且它会跳过相同排列!!!实在太好用了。
Final Code:
#include <bits/stdc++.h>
#define kth(i, j) (((i) >> (j)) & 1)
using namespace std;
int n, top;
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 0 ; i < (1 << n) ; i++) {
top = 0;
for(int j = n - 1 ; j >= 0 ; j--) {
kth(i, j) ? top-- : top++;
if(top < 0) break;
}
if(top == 0) {
for(int j = n - 1 ; j >= 0 ; j--) cout << '(' + kth(i, j);
cout << '\n';
}
}
return 0;
}