ABC396

ABC396

A

按题意模拟即可

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

int n, a[N];

int main() {
	ios :: sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; cin >> a[i++]) {
	}	
	for (int i = 1; i < n - 1; ++i) {
		if (a[i] == a[i + 1] && a[i + 1] == a[i + 2]) {
			cout << "Yes";
			return 0;
		}
	}
	return cout << "No", 0;
}

B

注意读题,然后开个栈模拟

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

int n;

vector<int> s;

int main() {
	ios :: sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 100; i--; s.push_back(0)) {
	} 
	for (int op, x; n--; ) {
		cin >> op;
		if (op == 1) {
			cin >> x;
			s.push_back(x);
		} else {
			cout << s.back() << endl;
			s.pop_back();
		}
	}
	return 0;
}

C

拿到数组后先按从大到小排个序,然后贪心选出所有 \(a_i \geq 0\) 的数,记次数为 \(cnt\),然后统计 \(\sum_{i = 1}^{i \leq \min(cnt, m)} \max(b_i, 0)\),加和即为答案。

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

int n, m, sum, a[N], b[N];
long long ans;

int main() {
	ios :: sync_with_stdio(0), cin.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; cin >> a[i++]) {
	}
	for (int i = 1; i <= m; cin >> b[i++]) {
	}
	sort (a + 1, a + n + 1, greater<int>());
	sort (b + 1, b + m + 1, greater<int>());
	for (int i = 1; i <= max(n, m); ++i) {
		if (a[i] >= 0 && i <= n) {
			if (b[i] > 0) {
				ans += a[i] + b[i];
			} else {
				ans += a[i];
				++sum;
			}
		} else if (a[i] + b[i] > 0 && i <= n) {
			ans += a[i] + b[i];
		} else if (sum && b[i] > 0) {
			ans += b[i], --sum;
		} else {
			break;
		}
	}
	return cout << ans, 0;
}

D

注意 \(n \leq 10\),然后直接阶乘级搜索。

#include <bits/stdc++.h>

#define int long long

using namespace std;
using pii = pair<int, int>;

const int N = 10 + 5;

int n, m, ans = LLONG_MAX >> 1;
bool v[N];

vector<pii> g[N];

void s( int x, int Xor ) {
  if (v[x])  {
    return ;
  }
  if (x == n) {
    ans = min(ans, Xor);
    return ;
  }
  v[x] = 1;
  for (auto to : g[x]) {
    s(to.first, Xor ^ to.second);
  }
  v[x] = 0;
}

signed main() {
	ios :: sync_with_stdio(0), cin.tie(0);
	cin >> n >> m;
  for (int i = 1, x, y, c; i <= m; ++i) {
    cin >> x >> y >> c;
    g[x].push_back(make_pair(y, c)), g[y].push_back(make_pair(x, c));
  }
  s(1, 0);
	return cout << ans, 0;
}

E

不会,按位加二分图。

F

先使用奇特的科技算出没改变时所有的逆序对数量,然后使用桶玩一下:将 \(\sum(a_i = i) \times i - 1\) 放进 \(t_{a_i}\),就是这个数值的数前面有多少个数,每次将整个数组增加取模的时候观察,逆序对数量减少(设当前枚举到了 \(a_i + k\),有 \(s\)\(a_i\)\(s \times (n - 1 - t_{a_i})\),增加了 \(t_{a_i}\) 个逆序对,然后将枚举 \(a_i\) 改为枚举数值,就做完了。

#include <iostream>

#include <vector>

#include <algorithm>

#include <climits>

#include <map>

#define endl '\n'

#define int long long

using namespace std;

const int N = 5e5 + 5;

int n, m, sum, rk[N], b[N], a[N], t[N], T[N], s[N], ans;

int lowbit(int x){
  return x & -x;
}

void add(int x, int k){
  for(int i = x; i <= n; i += lowbit(i)) t[i] += k;
}

int query(int x){
  int ret = 0;
  for(int i = x; i > 0; i -= lowbit(i)) ret += t[i];
  return ret;
}

signed main(){
  ios :: sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; cin >> a[i++]) {
  }
  for (int i = 1; i <= n; ++i) {
    ++a[i];
    b[i] = a[i];
  }
  sort (b + 1, b + n + 1);
  for (int i = 1; i <= n; ++i) {
    rk[b[i]] = i;
  }
  for(int i = 1; i <= n; ++i){
    T[a[i]] += i - 1, ++s[a[i]];
    ans += query(n) - query(rk[a[i]]);
    add(rk[a[i]], 1);
  }
  cout << ans << endl;
  for (int i = m; i > 1; --i) {
    ans += T[i];
    ans -= (n - 1) * s[i] - T[i];
    cout << ans << endl;
  }
  return 0;
}

总结

回归的第一场打的跟史一样,E 都没做出来

posted @ 2025-03-08 23:11  Ja_Morant  阅读(97)  评论(0)    收藏  举报