牛客小白月赛66
原比赛链接
A - 先交换
一共就3种情况,要么开头直接是基数就不用操作(次数为0),要么从后面交换一次就可以解决(次数为1),要么就不能交换(次数为-1)。
A - AC代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int a[N], t, n;
int read() {
int x = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * w;
}
void solve() {
memset(a, 0, sizeof a);
n = read();
for (int i = 1; i <= n; i++)
a[i] = read();
if (a[1] % 2 == 1) {
cout << 0 << endl;
return;
}
for (int i = 2; i <= n; i++)
if (a[i] < a[1] && a[i] % 2 == 1) {
cout << 1 << endl;
return;
}
cout << -1 << endl;
}
int main() {
t = read();
while (t--)
solve();
return 0;
}
B - 再交换
主要注意可能会出现A<B的情况同样需要交换,只是说交换之后A仍然比B小。因此,这种情况只需要看是否是第一位。如果是,就后一位交换。如果不是,就前一位交换。
如果A>B,那么就输出不同的那一位交换。因为就是因为这一位两数不同而导致B比A大,因此就交换这一位。
B - AC代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
char c1[N], c2[N];
int len, pos, t;
int read() {
int x = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * w;
}
void solve() {
scanf("%d%s%s", &len, c1 + 1, c2 + 1);
for (int i = 1; i <= len; i++) {
if (c1[i] != c2[i]) {
if (c1[i] > c2[i]) {
cout << i << " " << i << endl;
return;
}
else if (c1[i] < c2[i]) {
if (i == 1) {
cout << i + 1 << " " << i + 1 << endl;
return;
}
else {
cout << i - 1 << " " << i - 1 << endl;
return;
}
}
}
}
}
int main() {
t = read();
while (t--)
solve();
return 0;
}
C - 空洞骑士
终点一共就这么几种。要么是0,1;要么是1e9-1,1e9;要么是0和1e9。因此,只需要考虑最小的吉欧和最大的吉欧的位置。
C - AC代码
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 10;
int a[N];
int g = inf, G = -inf;
int n, l, r, s, t;
int read() {
int x = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * w;
}
void write(int res) {
if (res < 0) {
putchar('-');
res = -res;
}
if (res >= 10)
write(res / 10);
putchar(res % 10 + '0');
}
void solve() {
memset(a, 0, sizeof a);
n = read();
for(int i = 1; i <= n; i++) {
a[i] = read();
g = min(a[i], g);
G = max(a[i], G);
}
l = g, r = G, s = 0, t = 1e9;
int ans = inf;
if (r * 2 - 1 > ans) {
s = 0;
t = 1;
ans = r * 2 - 1;
}
if ((1e9 - l) * 2 - 1 > ans) {
s = 1e9;
t = 1e9 - 1;
}
write(s); printf(" "); write(t);
}
signed main() {
solve();
return 0;
}
D - 障碍
根据题意模拟一下即可。
D - AC代码
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int N = 2e5 + 10;
int n, m, maxn = -inf;;
int a[N];
int read() {
int x = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * w;
}
int main() {
n = read(), m = read();
vector <int> v(m, 0);
for (int i = 0; i < m; i++)
cin >> v[i];
v.push_back(0);
v.push_back(n);
sort(v.begin(), v.end());
int len = v.size();
for (int i = 0; i < min(len, 500); i++)
for (int j = 0; j < len; j++)
if (j + i + 1 < len)
maxn = max(maxn, v[j + i + 1] - v[j] - (i * i));
cout << maxn << endl;
return 0;
}
E - 生成树与路径
E - AC代码
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
int t, n, m, p = 1;
int read() {
int x = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * w;
}
void solve() {
n = read(), m = read();
p = 1;
for (int i = 1; i <= n && p <= m; i++)
for (int j = 0; j < n - i && p <= m; j++)
cout << j + 1 << " " << j + i + 1 << " " << p++ << endl;
return;
}
int main() {
t = read();
while (t--)
solve();
return 0;
}
F - 球球大作战
F - AC代码
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 1e5 + 10;
struct node {
int val, id;
bool operator < (const node &W) const {
return val < W.val;
}
} a[N];
int n, ans_pos[N];
int read() {
int x = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * w;
}
bool check(int k) {
int temp = a[n].val;
for (int i = n - 1; i > k; --i)
temp = (temp + a[i].val) / 2;
for (int i = k - 1; i > 0; --i)
temp = (temp + a[i].val) / 2;
if (temp <= a[k].val)
return 1;
return 0;
}
void write(int res) {
if (res < 0) {
putchar('-');
res = -res;
}
if (res >= 10)
write(res / 10);
putchar(res % 10 + '0');
}
int main() {
n = read();
for (int i = 1; i <= n; ++i) {
a[i].val = read();
a[i].id = i;
}
sort(a + 1, a + 1 + n);
int l = 1, r = n, mid, ans = n;
while (l <= r) {
mid = (l + r) / 2;
if (check(mid)) {
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
for (int i = ans; i <= n; i++)
ans_pos[a[i].id] = 1;
for (int i = 1; i <= n; i++)
write(ans_pos[i]);
return 0;
}
给个赞吧!