2024-07-18-ABC362

A - Buy a Pen

给定红蓝绿三支笔的价格,并不买指定颜色的笔,问买一支笔最少需要多少钱。

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
 
int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int r, g, b;
    string c;
    cin >> r >> g >> b >> c;
    if (c[0] == 'R') {
        r = 999;
    } else if (c[0] == 'G') {
        g = 999;
    } else {
        b = 999;
    }
    cout << min({r, g, b}) << '\n';
 
    return 0;
}
 

B - Right Triangle

给定三点坐标,问是否形成直角三角形。

#include <bits/stdc++.h>
using namespace std;
int main(){
  int xA, yA;
  cin >> xA >> yA;
  int xB, yB;
  cin >> xB >> yB;
  int xC, yC;
  cin >> xC >> yC;
  int a = (xB - xC) * (xB - xC) + (yB - yC) * (yB - yC);
  int b = (xC - xA) * (xC - xA) + (yC - yA) * (yC - yA);
  int c = (xA - xB) * (xA - xB) + (yA - yB) * (yA - yB);
  if (max({a, b, c}) * 2 == a + b + c){
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

C - Sum = 0

找到一个满足条件的数组:

image-20240717234938592

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<int> L(N), R(N);
  for (int i = 0; i < N; i++){
    cin >> L[i] >> R[i];
  }
  long long SL = 0, SR = 0;
  for (int i = 0; i < N; i++){
    SL += L[i];
    SR += R[i];
  }
  if (SR < 0 || SL > 0){
    cout << "No" << endl;
  } else {
    long long D = -SL;
    for (int i = 0; i < N; i++){
      long long a = min((long long) R[i] - L[i], D);
      L[i] += a;
      D -= a;
    }
    cout << "Yes" << endl;
    for (int i = 0; i < N; i++){
      cout << L[i];
      if (i < N - 1){
        cout << ' ';
      }
    }
    cout << endl;
  }
}

D - Shortest Path 3

求最短路

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  vector<vector<pair<int, int>>> E(N);
  for (int i = 0; i < M; i++){
    int U, V, B;
    cin >> U >> V >> B;
    U--;
    V--;
    E[U].push_back(make_pair(B, V));
    E[V].push_back(make_pair(B, U));
  }
  vector<long long> d(N, -1);
  priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;\
  pq.push(make_pair(A[0], 0));
  while (!pq.empty()){
    long long c = pq.top().first;
    int v = pq.top().second;
    pq.pop();
    if (d[v] == -1){
      d[v] = c;
      for (pair<int, int> e : E[v]){
        int w = e.second;
        if (d[w] == -1){
          pq.push(make_pair(c + e.first + A[w], w));
        }
      }
    }
  }
  for (int i = 1; i < N; i++){
    cout << d[i];
    if (i < N - 1){
      cout << ' ';
    }
  }
  cout << endl;
}

E - Count Arithmetic Subsequences

问数组中子序列为等差数列的数量

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
 
const int mo = 998244353;
 
int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto& x : a)
        cin >> x;
    vector<int> diff;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            diff.push_back(a[j] - a[i]);
        }
    }
    sort(diff.begin(), diff.end());
    diff.erase(unique(diff.begin(), diff.end()), diff.end());
    vector<int> ans(n + 1, 0);
    ans[1] = n;
    if (n > 1)
        ans[2] = n * (n - 1) / 2;
    for (auto d : diff) {
        vector<vector<int>> tr(n);
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < i; ++j)
                if (a[i] - a[j] == d)
                    tr[i].push_back(j);
 
        vector<vector<int>> dp(n, vector<int>(n + 1, 0));
        for (int i = 0; i < n; ++i) {
            dp[i][1] = 1;
            for (int j = 1; j <= i; ++j) {
                for (auto& k : tr[i]) {
                    dp[i][j + 1] += dp[k][j];
                    if (dp[i][j + 1] >= mo) {
                        dp[i][j + 1] -= mo;
                    }
                }
            }
        }
        for (int i = 0; i < n; ++i) {
            for (int j = 3; j <= n; ++j) {
                ans[j] += dp[i][j];
                if (ans[j] >= mo) {
                    ans[j] -= mo;
                }
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        cout << ans[i] << " \n"[i == n];
    }
 
    return 0;
}
 

F - Perfect Matching on a Tree

给定一棵树,俩俩配对,收益是两个点的最短路边数。

构造配对方案,使得收益最大。

#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
const ll MAXN=2e5+5;
vector<ll>adj[MAXN];
ll n;
ll sz[MAXN],core,num;
void dfs(ll u,ll fa){
    sz[u]=1;
    ll val=-1e18;
    for(auto v:adj[u]){
        if(v==fa){
            continue;
        }
        dfs(v,u);
        val=max(val,sz[v]);
        sz[u]+=sz[v];
    }
    val=max(val,n-sz[u]);
    if(val<num){
        num=val;
        core=u;
    }
}
ll a[MAXN],tot;
void ga(ll u,ll fa){
    if(u!=core){
        a[++tot]=u;
    }
    for(auto v:adj[u]){
        if(v==fa){
            continue;
        }
        ga(v,u);
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n;
    num=1e18;
    for(int i=1;i<n;++i){
        ll u,v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1,0);
    if(!(n&1)){
        a[++tot]=core;
    }
    ga(core,0);
    for(int i=1;i<=n/2;++i){
        cout<<a[i]<<" "<<a[i+n/2]<<endl;
    }
    return 0;
}
posted @ 2024-11-30 23:27  ~清渠~  阅读(0)  评论(0编辑  收藏  举报