7.28 day04 总结

· · 个人记录

7.28 dy04

得分情况与用时

T1: 100分,用时15分钟

T2: 0分,用时35分钟

T3: 10分,用时35分钟

T4: 20分,用时40分钟

T1题目解析

题意

在一个字符串中有一些数字

求这些数字能组成的最大的数

思路

直接遍历字符串,把每个数字存下来再排序即可

代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
string s;
int a[1000005], cnt;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> s;
    for(int i = 0; i < s.size(); i++) {
        if(s[i] >= '0' && s[i] <= '9') a[++cnt] = s[i] - '0';
    }
    sort(a + 1, a + cnt + 1);
    for(int i = cnt; i >= 1; i--) cout << a[i];
    return 0;
}

个人总结

简单

T2题目解析

题意

n个人要按给定的顺序用m个水龙头接水

思路

对于每个来接水的人:

选一个接完水最早的水龙头接水,将其接水时长加入该水龙头的总时长

最后取每个水龙头的最大值即可

代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int n, m, w[10005], a[105], maxn;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n >> m;
    memset(w, 0x3f, sizeof w);
    for(int i = 1; i <= n; i++) {
        cin >> w[i];
        sort(a + 1, a + m + 1);
        a[1] += w[i];
        maxn = max(maxn, a[1]);
    }
    cout << maxn;
    return 0;
}

个人总结

自己在考场上的思路是每个水龙头的时长实时变化,但是那种思路太复杂了。

听了课发现这种思路非常简单清晰

T3题目解析

题意

m * n的矩阵,部分点会成对

选一些纵向或横向的线破坏这些成对的点

求选哪些线破环的点最多

思路

统计出每条线能切开多少个点

用两个数组分别表示每条竖线和横线能切开的点的数量

统计出来后,先按数量从大到小 排序,再把前 k 项和 l 项按编号从小到大排序

代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
struct node {
    int x, id;
};
bool cmp1(node a, node b) {
    return a.x > b.x;
}
bool cmp2(node a, node b) {
    return a.id < b.id;
}
int m, n, k, l, d;
node a[1005], b[1005];
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> m >> n >> k >> l >> d;
    for(int i = 1; i <= d; i++) {
        int x, y, p, q, id, op;
        cin >> x >> y >> p >> q;
        if(x != p) {
            a[min(x, p)].x++;
            a[min(x, p)].id = min(x, p);
        }
        else {
            b[min(y, q)].x++;
            b[min(y, q)].id = min(y, q);
        } 
    }
    sort(a + 1, a + m + 1, cmp1);
    sort(b + 1, b + n + 1, cmp1);
    sort(a + 1, a + k + 1, cmp2);
    sort(b + 1, b + l + 1, cmp2);
    for(int i = 1; i <= k; i++) cout << a[i].id << " ";
    cout << endl;
    for(int i = 1; i <= l; i++) cout << b[i].id << " ";
    return 0;
}

个人总结

这道题只拿了10分,其实在考场上自己已经想到了大致思路,但是最后如何排序搞不懂,导致考场上的思路和代码都很乱

T4题目解析

题意

共n个人,每个人对3个社团分别有一个满意度,分配进一个社团是会增加对应的满意度,求一个分配方案满足以下两个要求:

思路

先把每个人分配到他最喜欢的社团

用3个vector记录现在每个社团的人数,此时有两种情况:

1.每个社团人数都没超,直接求和

2.有一个社团人数超了:调走一部分人,留下n / 2个人。 其余的人调到他第二喜欢的社团去

此时调走的人满足其最大满意度与第2满意度之差最小

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e5+5;

struct P {
    int x, id;  
};

int n; 
P a[N][4];
vector<int> q[3];

bool pmc(P a1, P a2) {
    return a1.x < a2.x;
}

bool cmp(int a1, int a2) {
    int x = a[a1][2].x - a[a1][1].x;
    int y = a[a2][2].x - a[a2][1].x;
    return x > y;
}

int func(int id) {
    int sum = 0;
    if (q[id].size() <= n/2) {
        for (auto i : q[id]) sum += a[i][2].x; 
        return sum;
    }

    sort(q[id].begin(), q[id].end(), cmp);
    for (int i=0; i<n/2; i++) {
        int idx = q[id][i];  
        sum += a[idx][2].x;
    }
    for (int i=n/2; i<q[id].size(); i++) {
        int idx = q[id][i];   
        sum += a[idx][1].x;
    }
    return sum;
}

void solve() {
    q[0].clear();
    q[1].clear();
    q[2].clear();
    cin >> n;
    for (int i=1; i<=n; i++) {
        cin >> a[i][0].x >> a[i][1].x >> a[i][2].x;  
        a[i][0].id = 0, a[i][1].id = 1, a[i][2].id = 2;
        sort(a[i], a[i]+3, pmc);  

        q[a[i][2].id].push_back(i); 
    }

    int res = 0;
    res += func(0) + func(1) + func(2);
    cout << res << '\n';
}

signed main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}

个人总结

这道题在考场上想到了一点点贪心思路(一点点点点点点......)

整场考试总结

这次考试第2题花的时间又多,又没做出来,以后要是像这次一样一个思路做不出来,一定要换个思路试试。

第3题虽然有了思路,但是代码没有总结出来。