头像

xianhai




离线:1小时前


最近来访(18)
用户头像
_wyp_
用户头像
L_H_R
用户头像
21KINGDMG
用户头像
邂逅
用户头像
1号用户
用户头像
土狗
用户头像
sssz
用户头像
指的就是你
用户头像
lyktes
用户头像
QQ家族族长
用户头像
123go
用户头像
长小圆
用户头像
leekey
用户头像
爷nm乱冲
用户头像
俄罗斯刺沙蓬
用户头像
Darren_4
用户头像
joykiller
用户头像
星星藏于星野

活动打卡代码 AcWing 429. 奖学金

xianhai
1小时前
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 305;

struct Stu {
    int id, c, m, e, sum;
    bool operator < (const Stu &p) const {
        if (sum != p.sum) {
            return sum > p.sum;
        }
        if (c != p.c) {
            return c > p.c;
        }
        return id < p.id;
    }
} stu[N];

int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int c, m, e;
        cin >> c >> m >> e;
        stu[i] = {i, c, m, e, c + m + e};
    }
    sort(stu + 1, stu + n + 1);
    for (int i = 1; i <= 5; i++) {
        cout << stu[i].id << ' ' << stu[i].sum << endl;
    }
    return 0;
}


活动打卡代码 AcWing 438. 分数线划定

xianhai
1小时前
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 5e3 + 5;
struct Person {
    int id, score;
    bool operator < (const Person &p) const {
        if (score != p.score) {
            return score > p.score;
        }
        return id < p.id;
    }
} p[N];
int n, m;

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &p[i].id, &p[i].score);
    }
    sort(p, p + n);
    int k = m * 1.5;
    while(k < n && p[k - 1].score == p[k].score) {
        k++;
    }
    printf("%d %d\n", p[k - 1].score, k);
    for (int i = 0; i < k; i++) {
        printf("%d %d\n", p[i].id, p[i].score);
    }
    return 0;
}


活动打卡代码 AcWing 421. 陶陶摘苹果

xianhai
1小时前
#include <iostream>
using namespace std;
const int N = 11;
int a[N];

int main() {
    for (int i = 0; i < 10; i++) {
        cin >> a[i];
    }
    int h;
    cin >> h;
    int ans = 0;
    for (int i =0; i < 10; i++) {
        if (h + 30 >= a[i]) {
            ans++;
        }
    }
    cout << ans << endl;
    return 0;
}


活动打卡代码 AcWing 442. 接水问题

xianhai
1小时前
#include <iostream>
using namespace std;
int n, m, maxx;
int w[10001]; // 每个人打水需要时间
int s[101]; // 每个水龙头目前的时间

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> w[i];
    }
    for (int i = 1; i <= n; i++) {
        maxx = 1;
        for (int j = 1; j <= m; j++) {
            if (s[maxx] > s[j]) {
                maxx = j; // 找到最小的
            }
        }
        s[maxx] += w[i]; // 加上他的时间
    }
    maxx = 1;
    for (int i = 1; i <= m; i++) {
        if (s[i] > maxx) {
            maxx = s[i];
        }
    }
    cout << maxx << endl;
    return 0;
}



xianhai
2小时前

实现获取 下一个排列 的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列(即,组合出下一个更大的整数)。

如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。

链接: https://leetcode-cn.com/problems/next-permutation

31.gif

class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        int i = nums.size() - 2;
        while (i >= 0 && nums[i] >= nums[i + 1]) {
            i--;
        }
        if (i >= 0) {
            int j = nums.size() - 1;
            while (j >= 0 && nums[i] >= nums[j]) {
                j--;
            }
            swap(nums[i], nums[j]);
        }
        reverse(nums.begin() + i + 1, nums.end());
    }
};
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void print(vector<int> nums) {
    for (auto i : nums) {
        cout << i << ' ';
    }
    cout << endl;
}

int main() {
    vector<int> nums = {4, 5, 2, 6, 3, 1};
    //vector<int> nums = {4, 3, 2, 1};
    print(nums);
    // 从后往前找生序对
    int i = nums.size() - 2;
    while (i >= 0 && nums[i] >= nums[i + 1]) {
        cout << "i=" << i << " " << nums[i] << endl;
        i--;
    }
    cout << "i=" << i << " " << nums[i] << endl;
    // 找比nums[i]大的
    if (i >= 0) {
        int j = nums.size() - 1;
        while (j >= 0 && nums[i] >= nums[j]) {
            cout << "j=" << j << " " << nums[j] << endl;
            j--;
        }
        cout << "j=" << j << " " << nums[j] << endl;
        swap(nums[i], nums[j]);
    }
    print(nums);
    //
    reverse(nums.begin() + i + 1, nums.end());
    print(nums);
    return 0;
}


活动打卡代码 AcWing 179. 最大数

xianhai
11小时前
class Solution {
public:
    string largestNumber(vector<int>& nums) {
        sort(nums.begin(), nums.end(), [](int a, int b) {
            string s1 = to_string(a), s2 = to_string(b);
            return s1 + s2 > s2 + s1;
        });
        string ans;
        for (auto num : nums) {
            ans += to_string(num);
        }
        int k = 0;
        while (k + 1 < ans.size() && ans[k] == '0') {
            k++;
        }
        return ans.substr(k);
    }
};
class Solution {
public:
    string largestNumber(vector<int>& nums) {
        vector<string> str;
        for(auto i : nums) {
            str.push_back(to_string(i));
        }
        // 使用 lambda 比较 elements.
        auto cmp = [](string left, string right) {
            return left + right > right + left;
        };
        sort(str.begin(),str.end(), cmp);
        stringstream ss;
        for(auto c : str) {
            ss << c;
        }
        string ans = ss.str();
        if(ans[0] == '0'){
            return "0";
        }
        return ans;
    }
};


活动打卡代码 AcWing 28. 实现 strStr()

xianhai
12小时前
class Solution {
public:
    int strStr(string haystack, string needle) {
        if (needle.empty()) {
            return 0;
        }
        int n = haystack.size(), m = needle.size();
        haystack = ' ' + haystack;
        needle = ' ' + needle;
        vector<int> next(m + 1);

        for (int i = 2, j = 0; i <= m; i ++ ) {
            while (j && needle[i] != needle[j + 1]) {
                j = next[j];
            }
            if (needle[i] == needle[j + 1]) {
                j++ ;
            }
            next[i] = j;
        }

        for (int i = 1, j = 0; i <= n; i ++ ) {
            while (j && haystack[i] != needle[j + 1]) {
                j = next[j];
            }
            if (haystack[i] == needle[j + 1]) {
                j ++ ;
            }
            if (j == m) {
                return i - m;
            }
        }
        return -1;
    }
};
class Solution {
public:
    int strStr(string haystack, string needle) {
        if (needle.empty()) {
            return 0;
        }
        int n = haystack.size(), m = needle.size();
        for (int i = 0; i < n - m + 1; i++) {
            bool flag = true;
            for (int j = 0; j < m; j++) {
                if (haystack[i + j] != needle[j]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                return i;
            }
        }
        return -1;
    }
};



xianhai
12小时前


活动打卡代码 AcWing 422. 校门外的树

xianhai
1天前

区间合并

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, int> pii;
vector<pii> segs;
int L, M;

int main() {
    cin >> L >> M;
    int a, b;
    for (int i = 0; i < M; i++) {
        cin >> a >> b;
        segs.push_back({a, b});
    }
    sort(segs.begin(), segs.end());
    int res = L + 1, st = segs[0].first, ed = segs[0].second;
    for (auto seg : segs) {
        if (seg.first <= ed) {
            ed = max(ed, seg.second);
        } else {
            res -= ed - st + 1;
            st = seg.first, ed = seg.second;
        }
    }
    res -= ed - st + 1;
    cout << res << endl;
    return 0;
}

直接模拟

#include <iostream>
using namespace std;
const int N = 1e4 + 5;
bool st[N];
int L, M;

int main() {
    cin >> L >> M;
    while (M--) {
        int l, r;
        cin >> l >> r;
        for (int i = l; i <= r; i++) {
            st[i] = true;
        }
    }
    int ans = 0;
    for (int i = 0; i <= L; i++) {
        if (st[i] == false) {
            ans++;
        }
    }
    cout << ans << endl;
    return 0;
}


活动打卡代码 AcWing 143. 最大异或对

xianhai
1天前
#include <iostream>
using namespace std;
const int N = 1e5;
int a[N + 5], son[31 * N + 5][2], n, idx;

void insert(int x) {
    int p = 0;
    for (int i = 30; i >= 0; i--) {
        int &s = son[p][x >> i & 1];
        if (!s) {
            s = ++idx;
        }
        p = s;
    }
}

int query(int x) {
    int p = 0, res = 0;
    for (int i =30; i >= 0; i--) {
        int u = x >> i & 1;
        if (son[p][!u]) {
            res += 1 << i;
            p = son[p][!u];
        } else {
            p = son[p][u];
        }
    }
    return res;
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        insert(a[i]);
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans = max(ans, query(a[i]));
    }
    cout << ans << endl;
    return 0;
}