Atcoder ABC 070

Atcoder ABC 070

ABC

抗压能力不太行,打点模拟赛调整下状态

A

判定一个串是否是回文。

#define judge
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifndef judge
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  string s;
  cin >> s;
  int tag = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] != s[s.size() - i - 1]) {
      tag = 1;
      break;
    }
  }
  if (tag) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
  }

  return 0;
}

B

给定两个区间,求出共同区间的大小,共同区间的左端点等于两个区间的左端点最大值,右端点等于两个区间右端点的最小值。
区间为空时应该是输出 0。

#define judge
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifndef judge
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  cout << max(0, min(b, d) - max(a, c)) << endl;
  return 0;
}

C

p和q最大公约数* p和q的最小公倍数 = p*q

一些数字的共同最小公倍数等于对每一个数组求最小公倍数:

// #define judge
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e2 + 10;
int n;
ll gcd(ll a, ll b) {
  if (b > a) swap(a, b);
  while (b != 0) {
    ll r = b;
    b = a % b;
    a = r;
  }
  return a;
}
ll low(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
#ifndef judge
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  cin >> n;
  ll x = 1, y;
  for (int i = 0; i < n; i++) {
    cin >> y;
    x = low(x, y);
  }
  cout << x << endl;
  return 0;
}

D

题意:给定一棵树,和一个指定的节点 k,对于节点 a 和 b ,有 q 个询问,询问从 a 到达 b 的距离的最小值。
解决:权值是非负,最短路问题,做以 k 为起点的 dijsktra。dist[a]+dist[j] 为答案。

注意:数组尽量开大一点,无向图的边数是点数的 2 倍,因此是\(2*maxn\)

#define judge
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;
static int faster_iostream = []() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  return 0;
}();
ll q, a, b, c, x, y, k;
const int maxn = 5e5 + 10;
int n, m;
ll h[maxn], w[maxn * 2], e[maxn], ne[maxn * 2], idx;
ll dist[maxn];
bool used[maxn];

void init() {
  memset(h, -1, sizeof h);
  idx = 0;
}
void add(int a, int b, int c) {
  e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

void dij(int k) {
  memset(dist, 0x3f, sizeof dist);
  dist[k] = 0;
  priority_queue<PII, vector<PII>, greater<PII>> heap;
  heap.push(make_pair(0, k));
  while (heap.size()) {
    PII t = heap.top();
    heap.pop();

    ll ver = t.second, distance = t.first;
    if (used[ver]) continue;
    used[ver] = true;
    for (int i = h[ver]; i != -1; i = ne[i]) {
      ll j = e[i];
      if (dist[j] > distance + w[i]) {
        dist[j] = distance + w[i];
        heap.push(make_pair(dist[j], j));
      }
    }
  }
}

int main() {
#ifndef judge
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  cin >> n;
  init();
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b >> c;
    add(a, b, c);
    add(b, a, c);
  }
  cin >> q >> k;
  dij(k);
  while (q--) {
    cin >> x >> y;
    cout << dist[x] + dist[y] << endl;
  }

  return 0;
}

猜你喜欢

转载自www.cnblogs.com/adameta/p/12315617.html