SMU Spring 2023 Trial Contest Round 10
A. Remove Duplicates
#include <bits/stdc++.h>
using namespace std;
#define int long long
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
int32_t main() {
int n = read();
vector<int> a(n), b;
set<int> st;
for (auto &i: a) i = read();
reverse(a.begin(), a.end());
for (auto i: a) {
if (st.insert(i).second) b.push_back(i);
}
reverse(b.begin(), b.end());
cout << b.size() << "\n";
for (auto i: b)
printf("%lld ", i);
return 0;
}
B. File Name
连续的 x 删到只剩两个
#include <bits/stdc++.h>
using namespace std;
#define int long long
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
int32_t main() {
int n;
string s;
cin >> n >> s;
int res = 0, cnt = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'x') cnt++;
else {
if (cnt > 2) res += cnt - 2;
cnt = 0;
}
}
if (cnt > 2) res += cnt - 2;
cout << res;
}
C. Letters
前缀和套二分就行了。
#include <bits/stdc++.h>
using namespace std;
#define int long long
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
int32_t main() {
int n = read(), m = read();
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) a[i] = read() + a[i - 1];
for (int x, l, r, mid, res; m; m--) {
x = read(), l = 0, r = n;
while (l <= r) {
mid = (l + r) >> 1;
if (a[mid] < x) res = mid, l = mid + 1;
else r = mid - 1;
}
printf("%lld %lld\n", res + 1, x - a[res]);
}
}
D. Almost Arithmetic Progression
考虑暴力的枚举\(a_1,a_2\)的值,然后计算出差值,\(O(n)\)的计算出最小修改次数即可。
#include <bits/stdc++.h>
using namespace std;
#define int long long
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
int32_t main() {
int n = read(), res = LLONG_MAX;
if (n <= 2) cout << "0", exit(0);
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) a[i] = read();
for (int x = -1, cnt, d; x <= 1; x++)
for (int y = -1; y <= 1; y++) {
auto b = a;
b[1] += x, b[2] += y, d = b[2] - b[1], cnt = abs(x) + abs(y);
for (int i = 3, t; cnt < res && i <= n; i++) {
t = b[i] - b[i - 1];
if (t == d) continue;
else if (t == d + 1) cnt++, b[i]--;
else if (t == d - 1) cnt++, b[i]++;
else cnt = LLONG_MAX;
}
res = min(res, cnt);
}
if (res == LLONG_MAX) res = -1;
cout << res;
}
E. Bus Video System
前缀和一遍,找出最大最小值,然后计算出第一站最多最少能有多少人。
#include <bits/stdc++.h>
using namespace std;
#define int long long
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
int32_t main() {
int n = read(), m = read();
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) a[i] = a[i - 1] + read();
int l = *min_element(a.begin() + 1, a.end()), r = *max_element(a.begin(), a.end());
l = max(0ll, -l), r = min(m, m - r);
cout << max(0ll, r - l + 1);
}
F. Mentors
统计出每个人有多少人的技能比他小,再统计出技能比他小的人有多少个和他吵架
#include <bits/stdc++.h>
using namespace std;
#define int long long
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
int32_t main() {
int n = read(), m = read();
vector<int> a(n + 1), b(n + 1), c(n + 1);
for (int i = 1; i <= n; i++) a[i] = b[i] = read();
sort(a.begin() + 1, a.end());
for (int i = 1; i <= n; i++)
c[i] = lower_bound(a.begin() + 1, a.end(), b[i]) - a.begin() - 1;
for (int x, y; m; m--) {
x = read(), y = read();
if (b[x] < b[y]) c[y]--;
if (b[y] < b[x]) c[x]--;
}
for (int i = 1; i <= n; i++)
cout << c[i] << " ";
return 0;
}
G. Petya's Exams
把所有的考试按照考试时间排序,然后尽可能早的复习完所有的考试,按照这个思路贪心处理即可。
#include <bits/stdc++.h>
using namespace std;
#define int long long
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
struct Node{
int s , d , c , id ;
Node(int s = 0 , int d = 0 , int c = 0 , int id = 0 ) : s(s) , d(d) , c(c){};
bool operator < ( const Node & b ) const{
if( d != b.d ) return d < b.d;
}
};
int32_t main() {
int n = read() , m = read();
vector<int> day(n+1);
vector<Node> exm(m);s
for( int i = 0 ; i < m ; i ++ ){
cin >> exm[i].s >> exm[i].d >> exm[i].c , exm[i].id = i+1;
if( day[exm[i].d] ) cout << "-1" , exit(0);
day[exm[i].d] = m+1;
}
sort( exm.begin() , exm.end() );
for( auto [s,d,c, id ] : exm ){
for( int i = s ; c > 0 && i < d ; i ++ ){
if( day[i] ) continue;
day[i] = id , c --;
}
if( c == 0 ) continue;
cout << "-1";
return 0;
}
for( int i = 1 ; i <= n ; i ++ )
cout << day[i] << " ";
}