11.7模拟赛

· · 个人记录

111#### 分数 估分:100+100+0+15=215
得分:100+0+0+25=125
原因:T2贪心不是最优解

T1

思路:

注意到不走点 i 只会将 i-1\rightarrow i\rightarrow i+1 这条路径转化为 i-1\rightarrow i+1,对其他路径没有影响。所以考虑维护出所有点都走的权值,与不走某一个点会对权值造成的影响,时间复杂度 O(n)

代码:
#include<bits/stdc++.h>
using namespace std;

long long n, a[100010], b[100010], sum;

int main(){
    freopen("travel.in", "r", stdin);
    freopen("travel.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 2; i <= n + 1; i++){
        cin >> a[i];
    }
    for(int i = 2; i <= n + 1; i++){
        b[i] = abs(a[i + 1] - a[i]) + abs(a[i] - a[i - 1]) - abs(a[i + 1] - a[i - 1]);
    }
    for(int i = 2; i <= n + 2; i++){
        sum += abs(a[i - 1] - a[i]);
    }
    for(int i = 2; i <= n + 1; i++){
        cout << sum - b[i] << '\n';
    }
    return 0;
}

T2

思路:

考虑对于其中一位:

同时,操作3晚于操作2操作一定是最优的

考虑对于 1\rightarrow1 的位进行枚举,时间复杂度 O(n\cdot2^n),期望得分40

代码:
//请输入文本

T4

思路:

将每个集合与每个元素都看做一个点,构建出二分图,边代表包含关系。

找出代表元素 1 的点到代表元素 m 的点之间的最短路即可

代码:
#include<bits/stdc++.h>
using namespace std;

int n, m, dist[500001];
vector<int> adj[500001];

long long bfd(){
    queue<int> q;
    q.push(n + 1);
    dist[n + 1] = 0;
    while(!q.empty()){
        int x = q.front();
        q.pop();
        for(int y : adj[x]){
            if(dist[y] != -1){
                continue;
            }
            if(y == n + m){
                return ((dist[x] + 1) / 2) - 1;
            }
            q.push(y);
            dist[y] = dist[x] + 1;
        }
    }
    return -1;
}

int main(){
    freopen("merge.in", "r", stdin);
    freopen("merge.out", "w", stdout);
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        int x;
        cin >> x;
        for(int j = 1; j <= x; j++){
            int a;
            cin >> a;
            adj[n + a].push_back(i);
            adj[i].push_back(n + a);
        }
    }
    for(int i = 1; i <= n + m; i++){
        dist[i] = -1;
    }
    cout << bfd();
    return 0;
}