YLQS-J-R1 部分分代码

· · 个人记录

括号内的是期望得分。

::::info[\text{T1}]{open} 测试点 1 略(10 pts)。 :::info[特殊性质 A(30 pts)]

//A
#include<iostream>
#include<cmath>

using namespace std;

int main(){
    int n;
    cin>>n;
    cout<<log2(n)<<'\n';
    return 0;
}

::: :::info[特殊性质 B(20 pts)]

//B
#include<iostream>

using namespace std;

int main(){
    int n,m=0;
    cin>>n;
    n--;m++;
    while(n%2==0){
        n/=2;m++;
    }
    while(n%3==0){
        n/=3;m++;
    }
    cout<<m<<'\n';
    return 0;
}
:::
::::info[\text{T2}]{open}
测试点 1,5,6 略(15 pts)。
:::info[直接模拟(40 pts)]
```cpp line-numbers
//直接模拟
#include<iostream>
#include<cmath>

using namespace std;

define LL long long

define LD double

const LL N=3e8;

LL A,B,C,D,E,F,G; LL T,n; LD fp=1;

int main(){ cin>>A>>B>>C>>D>>E>>F>>G; cin>>T; while(T--){ cin>>n; LL ans=((A1ll)<<n)+((Bn)nn)+((Cn)n)+En+G; LL p=floor(fpnlog2(n)),q=floor(fplog2(n)); ans+=Dp+Fq; if(ans>N)cout<<"Time Limit Exceeded\n"; else cout<<"Accepted\n"; } return 0; }

:::
特殊性质 B 略(25 pts)。
::::
::::info[$\text{T3}$]{open}
:::info[特判(10 pts)]
```cpp line-numbers
//1,2
#include<iostream>

using namespace std;

#define LL long long

int main(){
    int n;
    int c[3];
    int u,v,w;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>c[i];
    }
    cin>>u>>v>>w;
    if(n==1){
        cout<<0<<'\n';
    }
    else{
        if(w==1)cout<<min(c[1],c[2])<<'\n';
        else cout<<c[1]+c[2]<<'\n';
    }
    return 0;
}

::: :::info[暴力(20 pts)]

//by deepseek
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct Edge {
    int u, v, w;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vector<ll> c(n + 1);
    for (int i = 1; i <= n; i++) cin >> c[i];

    vector<Edge> edges;
    for (int i = 1; i < n; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        edges.push_back({u, v, w});
    }

    ll ans = LLONG_MAX;
    int total = 1 << n;           // 2^n 种方案

    for (int mask = 0; mask < total; mask++) {
        ll cost = 0;
        // 计算当前方案的总花费
        for (int i = 1; i <= n; i++) {
            if (mask & (1 << (i - 1))) {
                cost += c[i];
            }
        }

        // 检查所有边是否满足要求
        bool ok = true;
        for (const auto &e : edges) {
            int cnt = ((mask >> (e.u - 1)) & 1) + ((mask >> (e.v - 1)) & 1);
            if (cnt < e.w) {
                ok = false;
                break;
            }
        }

        if (ok) {
            ans = min(ans, cost);
        }
    }

    cout << ans << '\n';
    return 0;
}

::: :::info[特殊性质 A(20 pts)]

//by deepseek
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll INF = 4e18;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vector<ll> c(n + 1);
    for (int i = 1; i <= n; i++) cin >> c[i];

    vector<vector<pair<int, int>>> g(n + 1);
    for (int i = 1; i < n; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }

    // 特判只有一个点:没有边,最小花费为 0
    if (n == 1) {
        cout << 0 << '\n';
        return 0;
    }

    // 1. 找链的起点(度数为 1 的节点)
    int start = 1;
    for (int i = 1; i <= n; i++) {
        if (g[i].size() == 1) {
            start = i;
            break;
        }
    }

    // 2. 沿着链走,拉出顺序
    vector<int> order;
    vector<int> weight; // weight[i] 表示 order[i] 与 order[i+1] 的边权
    int prev = 0, cur = start;

    while (cur != 0) {
        order.push_back(cur);
        int nxt = 0, w = 0;
        for (auto [to, wt] : g[cur]) {
            if (to != prev) {
                nxt = to;
                w = wt;
                break;
            }
        }
        prev = cur;
        cur = nxt;
        if (cur != 0) weight.push_back(w);
    }

    // 3. 链上的线性 DP
    // dp0: 当前节点不选, dp1: 当前节点选
    ll dp0 = 0;
    ll dp1 = c[order[0]];

    for (int i = 1; i < n; i++) {
        int w = weight[i - 1];
        ll new_dp0, new_dp1;

        if (w == 1) {
            // 当前不选 -> 上一个必须选
            new_dp0 = dp1;
            // 当前选   -> 上一个可选可不选
            new_dp1 = min(dp0, dp1) + c[order[i]];
        } else { // w == 2
            // 当前不选 -> 非法(两端都必须选)
            new_dp0 = INF;
            // 当前选   -> 上一个也必须选
            new_dp1 = dp1 + c[order[i]];
        }

        dp0 = new_dp0;
        dp1 = new_dp1;
    }

    cout << min(dp0, dp1) << '\n';
    return 0;
}

::: :::info[特殊性质 B(20 pts)]

//by deepseek
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int MAXN = 5e5 + 5;
vector<int> g[MAXN];
ll c[MAXN];
ll dp0[MAXN], dp1[MAXN]; // dp0: 不选, dp1: 选

void dfs(int u, int fa) {
    dp0[u] = 0;
    dp1[u] = c[u];

    for (int v : g[u]) {
        if (v == fa) continue;
        dfs(v, u);

        // 边权 w=1:至少一端选
        dp0[u] += dp1[v];                 // u 不选,v 必须选
        dp1[u] += min(dp0[v], dp1[v]);    // u 选,v 可选可不选
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++) cin >> c[i];

    for (int i = 1; i < n; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        // 特殊性质 B:w 恒为 1,忽略 w 即可
        g[u].push_back(v);
        g[v].push_back(u);
    }

    dfs(1, 0);
    cout << min(dp0[1], dp1[1]) << '\n';

    return 0;
}

::: :::info[特殊性质 C(10 pts)]

//by deepseek
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vector<ll> c(n + 1);
    for (int i = 1; i <= n; i++) cin >> c[i];

    vector<int> deg(n + 1, 0);
    vector<tuple<int, int, int>> edges;
    edges.reserve(n - 1);

    for (int i = 1; i < n; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        edges.emplace_back(u, v, w);
        deg[u]++, deg[v]++;
    }

    // 只有一个节点,没有边
    if (n == 1) {
        cout << 0 << '\n';
        return 0;
    }

    // 找到中心(菊花图中度数为 n-1 的节点)
    int root = 1;
    for (int i = 1; i <= n; i++) {
        if (deg[i] == n - 1) {
            root = i;
            break;
        }
    }

    ll sum_all = 0;  // 所有叶子花费之和
    ll sum_w2 = 0;   // 边权为 2 的叶子花费之和
    bool has_w2 = false;

    for (auto [u, v, w] : edges) {
        int leaf = (u == root ? v : u);
        sum_all += c[leaf];
        if (w == 2) {
            has_w2 = true;
            sum_w2 += c[leaf];
        }
    }

    ll ans;
    if (has_w2) {
        // 中心必选,且所有 w=2 的叶子必选,w=1 的叶子不选
        ans = c[root] + sum_w2;
    } else {
        // 所有边权均为 1:中心选(叶子不选)或中心不选(所有叶子选),取最小
        ans = min(c[root], sum_all);
    }

    cout << ans << '\n';
    return 0;
}
:::
::::info[\text{T4}]{open}
测试点 1 略(5 pts)。
:::info[暴力枚举 1(时间复杂度 O(Tn^2(m+1)^n))(10 pts)]
```cpp line-numbers
//by deepseek
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int n; // 双方城市数量(各 n 个) ll m; // 总兵力 vector<ll> v, L; // v[i]: 小L第i个城市的防御度, L[i]: 小L的驻军 vector<ll> cur, best; // 当前枚举方案、最优方案 bool win; // 是否已找到胜利方案

/ 模拟战斗,Z 为小Z的兵力分配,返回是否最终获胜 / bool simulate(const vector<ll>& Z) { vector<ll> A = Z, B = L; // A:小Z兵力,B:小L兵力 while (true) { // 1. 找小Z最近对方基地的城市(编号最大的有兵城市) int a = -1; for (int i = n - 1; i >= 0; --i) if (A[i] > 0) { a = i; break; } if (a == -1) return false; // 无兵可攻,失败

    // 2. 找小L最远离己方基地的城市(编号最小的有兵城市)
    int b = -1;
    for (int i = 0; i < n; ++i)
        if (B[i] > 0) { b = i; break; }
    if (b == -1) return true;       // 防守方无兵,进攻胜利

    // 3. 战斗
    ll s = A[a], t = B[b];
    ll def = max(0LL, t + v[b]);    // 实际防御阈值
    if (s == def) {
        A[a] = B[b] = 0;
    } else if (s > def) {
        B[b] = 0;
        A[a] = s - def;
    } else { // s < def
        if (s < t) { A[a] = 0; B[b] = t - s; }
        else       { A[a] = B[b] = 0; }
    }
}

}

/ 递归枚举每个城市的兵力(0 ~ m),当分配完所有城市时检查 / void dfs(int idx) { if (win) return; // 已找到方案,提前终止 if (idx == n) { ll sum = 0; for (ll x : cur) sum += x; if (sum == m && simulate(cur)) { best = cur; win = true; } return; } for (ll x = 0; x <= m; ++x) { // 枚举当前城市兵力 cur[idx] = x; dfs(idx + 1); } }

int main() { ios::sync_with_stdio(false); cin.tie(nullptr);

int T;
cin >> T;
while (T--) {
    cin >> n >> m;
    v.resize(n);
    L.resize(n);
    for (int i = 0; i < n; ++i) cin >> v[i];
    for (int i = 0; i < n; ++i) cin >> L[i];

    cur.assign(n, 0);
    win = false;
    dfs(0);

    if (win) {
        cout << "win\n";
        for (int i = 0; i < n; ++i) {
            if (i) cout << ' ';
            cout << best[i];
        }
        cout << '\n';
    } else {
        cout << "lost\n";
        // 输出任意合法方案(全部兵力给第一个城市)
        cout << m;
        for (int i = 1; i < n; ++i) cout << " 0";
        cout << '\n';
    }
}
return 0;

}

:::
:::info[暴力枚举 2(时间复杂度 $O(TnC_{n-1}^{m+n-1})$)(25 pts)]
```cpp line-numbers
//by deepseek
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int n;
ll m;
vector<ll> v, L;          // 防守方防御度、初始兵力
vector<ll> cur, best;     // 当前枚举方案、获胜方案
bool winFound;

// 模拟战斗(双指针),返回小Z是否获胜
bool simulate(const vector<ll>& Z) {
    vector<ll> A = Z, B = L;
    int pa = n - 1, pb = 0; // pa:进攻方最右, pb:防守方最左
    while (true) {
        while (pa >= 0 && A[pa] == 0) pa--;
        if (pa < 0) return false;      // 无进攻兵力,失败
        while (pb < n && B[pb] == 0) pb++;
        if (pb >= n) return true;      // 防守方无兵,胜利

        ll s = A[pa], t = B[pb];
        ll def = max(0LL, t + v[pb]);  // 实际防御阈值

        // 战斗判定
        if (s == def) A[pa] = B[pb] = 0;
        else if (s > def) { B[pb] = 0; A[pa] = s - def; }
        else {
            if (s < t) { A[pa] = 0; B[pb] = t - s; }
            else       { A[pa] = B[pb] = 0; }
        }
    }
}

// 组合数枚举:分配 rem 兵力给 idx 及之后的城市
void dfs(int idx, ll rem) {
    if (winFound) return;
    if (idx == n - 1) {                // 最后一个城市,全部分配
        cur[idx] = rem;
        if (simulate(cur)) { best = cur; winFound = true; }
        return;
    }
    for (ll x = 0; x <= rem; ++x) {
        cur[idx] = x;
        dfs(idx + 1, rem - x);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T; cin >> T;
    while (T--) {
        cin >> n >> m;
        v.resize(n); L.resize(n);
        for (int i = 0; i < n; ++i) cin >> v[i];
        for (int i = 0; i < n; ++i) cin >> L[i];

        cur.assign(n, 0);
        winFound = false;
        dfs(0, m);

        if (winFound) {
            cout << "win\n";
            for (int i = 0; i < n; ++i) {
                if (i) cout << ' ';
                cout << best[i];
            }
            cout << '\n';
        } else {
            cout << "lost\n";
            cout << m;
            for (int i = 1; i < n; ++i) cout << " 0";
            cout << '\n';
        }
    }
    return 0;
}

::: :::info[特殊性质 B(10 pts)]

//B
#include<iostream>

using namespace std;

#define LL long long

const int N=2e3+5;

LL T,n,m,k,cnt,ans,inf=1e18;
LL v[N],w[N];

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>T;
    while(T--){
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            cin>>v[n+i];
        }
        ans=0;
        for(int i=1;i<=n;i++){
            cin>>w[n+i];ans+=w[n+i];
        }
        if(m>ans){
            cout<<"win\n";
        }
        else{
            cout<<"lost\n";
        }
        cout<<m;
        for(int i=1;i<n;i++){
            cout<<" 0";
        }
        cout<<'\n';
    }
    return 0;
}

::: :::info[特殊性质 C(35 pts)]

//C
#include<iostream>

using namespace std;

#define LL long long

const int N=2e6+5;

LL T,n,m,k,cnt,ans,inf=1e18;
LL v[N],w[N];

void opt(){
    for(int i=n;i>1;i--)cout<<v[i]<<' ';
    cout<<v[1]<<'\n';
    return ;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>T;
    while(T--){
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            cin>>v[n+i];
        }
        for(int i=1;i<=n;i++){
            cin>>w[n+i];
        }
        cnt=ans=0;
        for(int i=1;i<=n;i++){
            w[i]=w[n+i];
            ans+=w[i];
            if(w[i]==0)cnt++;
        }
        if(ans>=m){//兵力已经不足
            cout<<"lost\n";
            for(int i=1;i<n;i++){
                v[i]=0;
            }
            v[n]=m;
            opt();
            continue;
        }
        if(cnt!=0){//波次足够
            cout<<"win\n";
            v[n]=0;
            for(int i=1;i<=n;i++){
                v[i]=w[i];
            }
            v[n]+=m-ans;
            opt();
            continue;
        }
        k=inf;
        for(int i=1;i<=n;i++){
            k=min(k,v[n+i]);
        }
        ans+=k;
        if(ans<m){
            cout<<"win\n";
            w[n+1]=0;
            for(int i=1;i<=n;i++){
                if(k==v[n+i]){
                    w[i]+=k+w[i+1];
                    k=i;break;
                }
            }
            for(int i=1;i<=min(n-1,k);i++){
                v[i]=w[i];
            }
            for(int i=k+2;i<n;i++){
                v[i-1]=w[i];
            }
            if(k==n){
                v[n]=w[n]+m-ans;
            }
            else if(k==n-1){
                v[n]=m-ans;
            }
            else{
                v[n-1]=w[n];v[n]=m-ans;
            }
            opt();
        }
        else{
            cout<<"lost\n";
            for(int i=1;i<n;i++){
                v[i]=0;
            }
            v[n]=m;
            opt();
        }
    }
    return 0;
}

::: :::info[特殊性质 D(35 pts)]

//D
#include<iostream>

using namespace std;

#define LL long long

const int N=2e6+5;

LL T,n,m,k,cnt,ans,inf=1e18;
LL v[N],w[N];

void opt(){
    for(int i=n;i>1;i--)cout<<v[i]<<' ';
    cout<<v[1]<<'\n';
    return ;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>T;
    while(T--){
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            cin>>v[n+i];
        }
        for(int i=1;i<=n;i++){
            cin>>w[n+i];
        }
        ans=0;
        for(int i=1;i<=n;i++){
            w[n+i]=max(0ll,w[n+i]+v[n+i]);
            ans+=w[n+i];
        }
        if(m>ans){
            cout<<"win\n";
            for(int i=1;i<n;i++){
                cout<<w[n+i]<<' ';
            }
            cout<<w[n*2]+m-ans<<'\n';
        }
        else{
            cout<<"lost\n";
            cout<<m;
            for(int i=1;i<n;i++){
                cout<<" 0";
            }
            cout<<'\n';
        }
    }
    return 0;
}
:::