div3
作者:互联网
http://codeforces.com/contest/1462
A. Favorite Sequence*
#include <bits/stdc++.h>
using namespace std;
int a[500];
int main() {
int t;
cin >> t;
while(t --) {
int n;
cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1, l = 1, r = n; i <= n; i ++)
printf("%d%c", a[i & 1 ? l ++ : r --], "\n " [i < n]); //?
cout << endl;
}
return 0;
}
B. Last Year’s Substring
decimal digits 十进制数
#include <bits/stdc++.h>
using namespace std;
int _;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
for (int i = 0; i <= 4; i++) {
if (s.substr(0, i) + s.substr(n - 4 + i, 4 - i) == "2020") {
cout << "YES" << endl;
return;
}
}
cout << "NO" << endl;
}
int main() {
cin >> _;
while (_--) {
solve();
}
return 0;
}
C. Unique Number
打表:
#include <bits/stdc++.h>
using namespace std;
int a[] = {0,1,2,3,4,5,6,7,8,9,
19,29,39,49,59,69,79,89,
189,289,389,489,589,689,789,
1789,2789,3789,4789,5789,6789,
16789,26789,36789,46789,56789,
156789,256789,356789,456789,
1456789,2456789,3456789,
13456789,23456789,
123456789,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int main() {
int n;
cin >> n;
while(n --) {
int t;
cin >> t;
cout << a[t] << endl;
}
return 0;
}
循环:
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
while(n --) {
int t;
cin >> t;
int ans = 0, p = 1;
for(int i = 9; i > 0; i --) {
if(t >= i) {
t -= i;
ans = ans + i * p;
p *= 10;
}
}
if(t) cout << -1 << endl;
else cout << ans << endl;
}
return 0;
}
D. Add to Neighbour and Remove*
denote 意味着
#include <bits/stdc++.h>
using namespace std;
main() {
int t; cin >> t;
while (t--) {
int n;
cin >> n;
int a[n], S = 0;
for (int &i : a) cin >> i, S += i;
int ans = n - 1;
for (int i = n - 2; i >= 0; --i) {
int b = n - i, y = 1, s = 0;
if (S % b == 0) {
for (int j = 0; j < n; ++ j) {
s += a[j];
if (s > S / b) y = 0;
if (s == S / b) s = 0;
}
if(y) ans = i;
}
}
cout << ans << "\n";
}
}
E1. Close Tuples (easy version)
duplicates 复制
i.e. 也就是说
guaranteed 保证
exceed 超过
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll a[200005];
int main() {
ll t;
cin >> t;
while(t --) {
ll n;
cin >> n;
for(ll i = 1; i <= n; i ++) cin >> a[i];
sort(a + 1, a + 1 + n);
ll j = 2, ans = 0;
for(ll i = 1; i <= n; i ++) {
while(a[j] - a[i] <= 2 && j <= n) j ++;
j --;
ans += (j - i) * (j - i - 1) / 2; //1 + 2 + 3 + ... + n公式
}
cout << ans << endl;
}
return 0;
}
E2. Close Tuples (hard version)*
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int mod = 1000000007;
int nck[200010][110];
int main() {
for (int n = 0; n < 200010; n++) { //初始化
nck[n][0] = 1;
for (int k = 1; k <= min(n, 100); k++) {
nck[n][k] = (nck[n - 1][k] + nck[n - 1][k - 1]) % mod;
}
}
int t;
cin >> t;
while (t--) {
int n, m, k;
cin >> n >> m >> k;
ll a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
long long count = 0;
for (int i = 0; i < n - m + 1; i++) {
ll x = upper_bound(a + i, a + n, a[i] + k) - a - i - 1;
count = (count + nck[x][m - 1]) % mod;
}
cout << count << endl;
}
return 0;
}
F. The Treasure of The Segments
Treasure 珍宝
Segments 线段、片段、部门
coordinates 坐标、协调
respectively 各自
intersects 交叉
intersection 交点
题意:让剩下的区间能连成一个整体,可以有公共区间或只有一个公共点,求最小可以删除的区间
学习:lower_bound,非降序列,二分查找第一个大于等于所要查找元素的地址
upper_bound,大于目标值的第一个元素
#include <bits/stdc++.h>
using namespace std;
int main() {
int t; cin >> t;
while(t --) {
int n;
cin >> n;
vector<int> l(n), r(n), a, b;
for(int i = 0; i < n; i ++) {
cin >> l[i] >> r[i];
a.push_back(l[i]);
b.push_back(r[i]);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
int ans = n - 1;
for(int i = 0; i < n; i++) {
int x = lower_bound(b.begin(), b.end(), l[i]) -b.begin();
int y = a.end()- upper_bound(a.begin(), a.end(), r[i]);
ans = min(ans, x + y);
}
cout << ans << endl;
}
return 0;
}
标签:int,ll,cin,--,while,using,div3 来源: https://blog.csdn.net/wenya13/article/details/112833099