暑假终章·8.30-河西校区

· · 个人记录

8.30-河西校区

::::::info[T1]{open}

U594958 ATM机

::::info[思路] 从头到尾模拟,若当前位置是0且后一位也是0,可以少按一个键 :::: ::::success[赛时代码&得分]

\color{green}{Accepted\not100tps}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int i, ans;
string s;
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> s;
    int n = s.size();
    for (; i < n; i ++) {
        if (s[i] == '0' && s[i + 1] == '0') 
            i ++;
        ans ++;
    }
    cout << ans;
    return 0;
}
::::

:::::info[T2]{open}

U595148 落落的去倒油

::::info[思路] 输入后排序,然后遍历一遍,与当前编号比大小,并打擂台求出最小值 :::: ::::error[赛时得分]

\color{red}{Unaccepted\not0tps}

:::: ::::success[正解]

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n;
double ans = 1;
int a[N];
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n; 
    for (int i = 1; i <= n; i ++) 
        cin >> a[i];
    sort(a + 1, a + 1 + n);
    for (int i = 1; i <= n; i ++) 
        if (a[i] > i) {
            cout << -1;
            return 0;
        } else 
            ans = min(ans, 1.0 * a[i] / i);
    cout << fixed << setprecision(8) << ans;
    return 0;
}
::::

::::::info[T3]{open}

U594778 制作任意门

::::info[思路] 二分答案,差分+前缀和找出是在哪一方案被打回时员工炸了 :::: ::::error[赛时得分]

\color{orange}{Unaccepted\not55tps}

:::: ::::success[正解]

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n, m, k, l, r, cnt;
int a[N], b[N], _l[N], _r[N];
void solve1() {
    for (int i = 1; i <= n; i ++) 
        cin >> a[i];
    for (int i = 1; i <= m; i ++) {
        cin >> l >> r;
        for (int j = l; j <= r; j ++) {
            b[j] ++;
            if (b[j] == a[j]) 
                cnt ++;
        }
        if (cnt >= k) {
            cout << i;
            return;
        }
    }
    cout << m;
}
bool check(int x) {
    memset(b, 0, sizeof(b));
    int cnt = 0;
    for (int i = 1; i <= x; i ++) {
        b[_l[i]] ++;
        b[_r[i] + 1] --;
    }
    for (int i = 1; i <= n; i ++) {
        b[i] = b[i - 1] + b[i];
        if (b[i] >= a[i]) 
            cnt ++;
    }
    return cnt >= k;
}
void solve2() {
    for (int i = 1; i <= n; i ++) 
        cin >> a[i];
    for (int i = 1; i <= m; i ++) 
        cin >> _l[i] >> _r[i];
    l = 0, r = m + 1;
    while (l + 1 < r) {
        int mid = (l + r) >> 1;
        if (check(mid)) 
            r = mid;
        else 
            l = mid;
    }
    if (check(r)) 
        cout << r;
    else 
        cout << m;
}
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m >> k;
    if (n <= 500) 
        solve1();
    else 
      solve2();
    return 0;
}
::::

::::::info[T4]{open}

U594963 没有洞的正方形

::::info[思路] 使用动态规划层级递推,记得取贡献 :::: ::::error[赛时得分]

\color{red}{Unaccepted\not10tps}

:::: ::::success[正解]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3025;
ll n, m, o, u, v, ans;
ll a[N][N], dp[N][N];
bool check(int x, int y, int len) {
    if (x - len + 1 < 1 || y - len + 1 < 1) 
        return 0;
    for (int i = x - len + 1; i <= x; i ++) 
        for (int j = y - len + 1; j <= y; j ++) 
            if (a[i][j] == 0) 
                return 0;
    return 1;
}
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m >> o;
    for (int i = 1; i <= n; i ++) 
        for (int j = 1; j <= m; j ++) 
            a[i][j] = 1;
    while (o --) {
        cin >> u >> v;
        if (u >= 1 && u <= n && v >= 1 && v <= m) 
            a[u][v] = 0;
    }
    for (int i = 1; i <= n; i ++) 
        for (int j = 1; j <= m; j ++) 
            if (a[i][j]) {
                dp[i][j] = min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}) + 1;
                ans += dp[i][j];
            }
    cout << ans;
    return 0;
}
::::

::::::info[T5]{open}

U595150 自动贩卖机

::::info[思路]

\color{red}{\texttt{超级无敌大模拟}}

:::: ::::error[赛时得分] 绿题模拟是半个小时能写完的吗!

\color{red}{Unaccepted\not0tps}

:::: ::::success[正解] 牢师使用AI写的,改不动
一改就炸

#include<bits/stdc++.h>

using namespace std;

const int N = 1e3 + 5;

struct User {
    string name;
    string pwd;
    bool isAdmin;
    int blance;
} user[N];

struct Product {
    string name;
    string category;
    int price;
    int stock;
    int cnt;
} product[N], tmp[N];

int usercnt, productCnt;
int checkUser = -1;

int coin[] = {10, 5, 2, 1};

void reg(string n, string p) {
    for (int i = 1; i <= usercnt; i++) {
        if (user[i].name == n) {
            cout << "user exists\n";
            return ;
        }
    }
    user[++usercnt] = {n, p, 0, 0};
    cout << "register ok\n";
}

void login(string n, string p) {
    if (checkUser != -1) {
        cout << "logged in\n";
        return ;
    }

    for (int i = 1; i <= usercnt; i++) {
        if (user[i].name == n && user[i].pwd == p) {
            checkUser = i;
            cout << "user in\n";
            return ;
        }
    }

    cout << "login failed\n";
}

void logout() {
    if (checkUser == -1) {
        cout << "user out\n";
        return ;
    }

    //退出时自动找零
    int bal = user[checkUser].blance;
    if (bal > 0) {
        cout << "change:";
        int res = bal;
        for (int i = 0; i < 4; i++) {
            int cnt = res / coin[i];
            res %= coin[i];
            if (cnt > 0) {
                cout << " " << coin[i] << "x" << cnt;
            }
        }
        cout << '\n';
        user[checkUser].blance = 0;
    }
    checkUser = -1;
    cout << "user out\n";
}

//充值
void recharge(int amt) {
    if (checkUser == -1) {
        cout << "please login\n";
        return ;
    }

    if (amt != 1 && amt != 2 && amt != 5 && amt != 10) {
        cout << "invalid coin\n";
        return ;
    }

    user[checkUser].blance += amt;
    cout << "balance: " << user[checkUser].blance << '\n';
}

void cmd_rank() {
    for (int i = 1; i <= productCnt; i++) {
        tmp[i] = product[i];
    }
    stable_sort(tmp + 1, tmp + 1 + productCnt, [](Product a, Product b) {
        if (a.cnt != b.cnt) return a.cnt > b.cnt;
        return a.name < b.name;
    });

    for (int i = 1; i <= productCnt; i++) {
        cout << tmp[i].name << ' ' << tmp[i].cnt << '\n';
    }
}

void buyproduct(string n) {
    if (checkUser == -1) {
        cout << "please login\n";
        return;
    }

    int idx = -1;

    for (int i = 1; i <= productCnt; i++) {
        if (product[i].name == n) {
            idx = i;
            break;
        }
    }

    if (idx == -1) {
        cout << "product not found\n";
        return ;
    }

    Product &p = product[idx];
    if (p.stock <= 0) {
        cout << "sold out\n";
        return;
    }

    if (user[checkUser].blance < p.price) {
        cout << "not enough money\n";
        return ;
    }

    user[checkUser].blance -= p.price;
    p.stock--;
    p.cnt++;

    cout << "buy " << p.name << '\n';
}

void restock() {
    if (checkUser == -1) {
        cout << "please login\n";
        return;
    }

    if (!user[checkUser].isAdmin) {
        cout << "not Admin\n";
        return ;
    }

    bool need = 0;
    //如果有小于3件商品就需要补货建议
    for (int i = 1; i <= productCnt; i++) {
        if (product[i].stock <= 3) {
            need = 1;
            break;
        }
    }
    cout << (need ? "yes" : "no") << '\n';
}

void cmt_list(string arg) {
    if (arg == "all") {
        //全部列出
        for (int i = 1; i <= productCnt; i++) {
            Product &p = product[i];
            cout << p.name << ' ' << p.category 
            << ' ' << p.price << ' ' << p.stock 
            << ' ' << p.cnt << '\n';
        }
    } else if (arg == "hot") {
        cmd_rank();
    } else {
        for (int i = 1; i <= productCnt; i++) {
            Product &p = product[i];
            if (p.category == arg) {
                cout << p.name << ' ' << p.category 
                << ' ' << p.price << ' ' << p.stock 
                << ' ' << p.cnt << '\n';
            }
        }
    }
}

int main(void)
{
    int n;
    cin >> n;
    productCnt = n;
    for (int i = 1; i <= n; i++) {
        cin >> product[i].name
        >> product[i].category
        >> product[i].price
        >> product[i].stock;
        product[i].cnt = 0;
    }

    user[++usercnt] = {"admin", "admin", 1, 0};

    int m;
    cin >> m;

    while (m--) {
        string op;
        cin >> op;
        string n, p;
        if (op == "register") {
            cin >> n >> p;
            reg(n, p);
        } else if (op == "login") {
            cin >> n >> p;
            login(n, p);
        } else if (op == "logout") {
            logout();
        } else if (op == "recharge") {
            int amt;
            cin >> amt;
            recharge(amt);
        } else if (op == "buy") {
            string name;
            cin >> name;
            buyproduct(name);
        } else if (op == "change") {
            if (checkUser == -1) {
                cout << "please login\n";
            } else {
                int bal = user[checkUser].blance;
                if (bal > 0) {
                    cout << "change:";
                    int res = bal;
                    for (int i = 0; i < 4; i++) {
                        int cnt = res / coin[i];
                        res %= coin[i];
                        if (cnt > 0) {
                            cout << " " << coin[i] << "x" << cnt;
                        }
                    }
                    cout << '\n';
                    user[checkUser].blance = 0;
                } else {
                    cout << "0\n";
                }
            }
        } else if (op == "restock") {
            restock();
        } else if (op == "list") {
            string arg;
            cin >> arg;
            cmt_list(arg);
        } else {
            cout << "unknown command\n";
        }
    }


    return 0;
}
::::

既然是最后一次暑假的最后一次写总结了,送个抽抽乐玩玩我抽到了91f5