• 牛客多校第一场补题


    难度升序



    F:Infinite String Comparision

    上手就写,将ab都延长2倍,然后比较到max(a.length,b.length)即可

    证法:假设a.length<b.length,然后举例子就行了

    
    #pragma GCC optimize(2)
    #include<bits/stdc++.h>
    #define ll long long
    #define fastio {ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);};
    using namespace std;
    double pi = acos(-1);
    const double eps = 1e-9;
    const int inf = 1e9 + 7;
    const int maxn = 2e3 + 10;
     
    int judge(string a,string b)
    {
        int lena = a.length(), lenb = b.length();
        int len = max(lena, lenb);
        for (int i = 0; i < len; i++)
        {
            int l = i % lena, r = i % lenb;
            if (a[l] < b[r])return -1;
            else if (a[l] > b[r])return 1;
        }
        return 0;
    }
     
    int main()
    {
        fastio;
        string a, b;
        while (cin >> a >> b)
        {
            a += a, b += b;
            if (judge(a, b) == 1)cout << ">" << endl;
            else if(judge(a, b) == 0)cout << "=" << endl;
            else cout << "<" << endl;
        }
        return 0;
    }
    

    J-Easy Integration


    求这个积分,如果是分数就输出分子乘分母的逆元再模998244353

    手残导致输出溢出WA了,我只能爪巴

    #pragma GCC optimize(2)
    #include<bits/stdc++.h>
    #define ll long long
    #define fastio {ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);};
    using namespace std;
    double pi = acos(-1);
    const double eps = 1e-9;
    const int inf = 1e9 + 7;
    const int maxn = 1e6 + 10;
    const ll mod = 998244353;
    ll f[maxn], fac[maxn], tmp[maxn];
     
    long long qpow(long long x, long long n) {
        long long res = 1;
        while (n) {
            if (n & 1) res = (res * x) % mod;
            x = (x * x) % mod;
            n /= 2;
        }
        return res%mod;
    }
     
    int main()
    {
        fastio;
        ll n;
        f[0] = 1;
        fac[0] = 1;
        tmp[1] = 2;
        for (ll i = 1; i < maxn; i++)
            fac[i] = (fac[i - 1] * (2 * i + 1)) % mod;
        for (ll i = 2; i < maxn; i++)
            tmp[i] = (tmp[i - 1] * 2 * i) % mod;
        while (cin >> n)
        {
            cout << (((qpow(qpow(4, n), mod - 2) % mod) * (tmp[n] % mod))%mod * (qpow(fac[n], mod - 2) % mod)) % mod << endl;
        }
    }
    

    I-1 or 2

    很明显是个网络流,但需要一点建图的技巧

    构造源点流量为inf,连接输入点1~n,流量为x;然后将1+N~n+N与输入点按照题目要求相连,每条边的流量为1;再将1+N~n+N进行拆点限流,最后汇总到汇点跑dinic检查是否能跑到最大流即可
    (代码有点不同,思路一样,板子备注没删)

    #pragma GCC optimize(2)
    #include<bits/stdc++.h>
    #define ll long long
    #define fastio {ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);};
    using namespace std;
    double pi = acos(-1);
    const double eps = 1e-9;
    const int inf = 1e9 + 7;
    const int maxn = 1e6 + 10;
    int cnt = 1, head[maxn], now[maxn], deep[maxn];
    queue<int>q;
     
    struct edge {
        int to;
        int cost;
        int next;
    }e[maxn * 2];
    void add(int from, int to, int cost)//前向星
    {
        e[++cnt].to = to;
        e[cnt].cost = cost;
        e[cnt].next = head[from];
        head[from] = cnt;
    }
     
    int n, m, s, t;//n个点,m条边,s为源点,t为汇点
     
    bool bfs()
    {
        memset(deep, 0, sizeof(deep));//初始化深度
        while (!q.empty())q.pop();//初始化队列
        q.push(s);
        deep[s] = 1;//源点深度赋值1
        now[s] = head[s];//将拥有深度的点复制到另一个head数组
        while (!q.empty())
        {
            int from = q.front(), to, cost; q.pop();
            for (int i = head[from]; i; i = e[i].next)
            {
                to = e[i].to;
                cost = e[i].cost;
                if (cost && !deep[to])//容量>0且未遍历过
                {
                    q.push(to);
                    deep[to] = deep[from] + 1;
                    now[to] = head[to];//将拥有深度的点复制
                    if (to == t)return 1;//找到汇点就return,在deep[t]-1的点早已被复制,所以不用担心直接返回会丢失增广路
                }
            }
        }
        return 0;
    }
     
    int dfs(int from, int flow)
    {
        if (from == t)return flow;
        int rest = flow, tmp, i;//rest存当前剩余容量,tmp存当前增广路可增广流量,i前向星
        //cout << 1 << endl;
        for (i = now[from]; i && rest; i = e[i].next)//尝试增广与from相连的所有容量>0且deep[from]+1==deep[to]的点所存在的路径
            if (e[i].cost && deep[e[i].to] == deep[from] + 1)
            {
                tmp = dfs(e[i].to, min(flow, e[i].cost));//取当前路径上最小容量作为flow
                if (!tmp)deep[e[i].to] = -2;//to所有边都无法增广,那就炸点,直接把深度赋一个不可能值
                e[i].cost -= tmp;//增广完成后正向边减去tmp,反向边加上tmp,剩余容量减去tmp
                e[i ^ 1].cost += tmp;//由于存图是从2开始存并且正边和反边相邻,所以正边^1是反边,反之亦然
                rest -= tmp;
                //cout << rest << endl;
            }
        now[from] = i;//当前弧优化,下次遍历到from时会直接从i(flow完了i就不一定是0了)开始
        return flow - rest;//返回一个增广消耗的值
    }
     
    ll dinic()
    {
        long long maxflow = 0;
        long long flow = 0;
        while (bfs())
        {
            //for (int i = 1; i <= n; i++)
            //  cout << deep[i] << " ";
            while (flow = dfs(s, inf))//dfs没能找到增广路就重新进行分层
                maxflow += flow;
        }
        return maxflow;
    }
     
    int main()
    {
        fastio;
        int n, m;
        s = 200, t = 301;
        while (cin >> n >> m)
        {
            cnt = 1;
            memset(head, 0, sizeof(head));
            int sum = 0;
            for (int i = 1; i <= n; i++)
            {
                int x;
                cin >> x;
                sum += x;
                add(i, i + 100, 0);
                add(i + 100, i, x);//限流器
                add(i, t, inf);
                add(t, i, 0);
                add(s, i + 200, x);
                add(i + 200, s, x);
            }
            for (int i = 1; i <= m; i++)
            {
                int a, b;
                cin >> a >> b;
                add(a + 200, b + 100, 1);
                add(b + 100, a + 200, 0);
                add(b + 200, a + 100, 1);
                add(a + 100, b + 200, 0);
            }
            if (dinic() == sum)cout << "Yes" << endl;
            else cout << "No" << endl;
     
        }
        return 0;
    }
    

    H-Minimum-cost Flow

    最小费用流,题目直接告诉做法了,直接跑就完事了(我板子好像常数有点问题,cin直接原地爆炸)

    还是有丶做法:首先跑流量无限,所有边容量为1的最小费用流,记录每一条增广路的花费(每一条都是满流);

    然后对于每一对uv,让每条边的容量都变成u,那么每一条增广路的花费也会变成u倍;

    由于源点是单位流,边权原本是(u/v),现在变成了(u),那么源点的流量就变成了(v),因此最大流(或者说答案需求流)必然只能是v

    设当前已增广的流量为flownow

    flownow=0

    这样遍历每一条增广路(已知必然满流),若(flownow+u<=v),则(flownow += u, cost += tracost[i] * u);

    如果(flownow+u>v),但(flownow<v),这一条就跑不满,(cost += tracost[i] * (v - tmp), flownow = v);

    如果跑完之后(flownow<v),则跑不满单位流,输出(NaN);

    如果跑满了,就把(cost)(v)求个(gcd),然后输出就完事了

    #pragma GCC optimize(2)
    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    double pi = acos(-1);
    const double eps = 1e-9;
    const int inf = 1e9 + 7;
    const int maxn = 60;
     
    int cnt = 1, head[maxn], pre[maxn], last[maxn];
    int dis[maxn], flow[maxn], maxflow;
    int vis[maxn];
    vector<ll>tracost;
    queue<int>q;
    int n, m, S, T;
     
    struct edge {
        int to;
        int cost;
        int vue;
        int next;
    }e[maxn * 4];
     
    inline void add(int from, int to, int cost, int vue)//前向星
    {
        e[++cnt].to = to;
        e[cnt].cost = cost;
        e[cnt].vue = vue;
        e[cnt].next = head[from];
        head[from] = cnt;
    }
     
    ll __gcd(ll a, ll b)
    {
        while (b)
        {
            ll tmp = b;
            b = a % b;
            a = tmp;
        }
        return a;
    }
     
     
    ll SPFA(int s, int t) {
        memset(dis, 0x3f, sizeof(dis));
        memset(vis, 0, sizeof(vis));
        q.push(s);
        dis[s] = 0, vis[s] = 1, pre[t] = -1, flow[s] = inf;
        while (!q.empty()) {
            int now = q.front();
            q.pop();
            vis[now] = 0;
            for (int i = head[now]; i != -1; i = e[i].next) {
                if (e[i].cost > 0) {
                    int po = e[i].to;
                    long long lo = e[i].vue;
                    if (dis[po] > dis[now] + lo) {
                        dis[po] = dis[now] + lo;
                        flow[po] = min(flow[now], e[i].cost);
                        last[po] = i;
                        pre[po] = now;
                        if (!vis[po]) {
                            vis[po] = 1;
                            q.push(po);
                        }
                    }
                }
            }
        }
        return pre[t] != -1;
    }
    void MCMF(int s, int t) {
        maxflow = 0;
        while (SPFA(s, t)) {
            maxflow += flow[t];
            tracost.push_back(dis[t] * flow[t]);
            for (int i = t; i != s; i = pre[i]) {
                e[last[i]].cost -= flow[t],
                e[last[i] ^ 1].cost += flow[t];
            }
        }
    }
     
    int main()
    {
        S = 1;
        while (~scanf("%d %d", &n, &m))
        {
            memset(head, -1, sizeof(head));
            tracost.clear();
            cnt = 1;
            T = n;
            for (int i = 1; i <= m; ++i)
            {
                ll a, b, c;
                scanf("%d%d%d", &a, &b, &c);
                add(a, b, 1, c);
                add(b, a, 0, -c);
            }
            MCMF(S, T);
            int q;
            scanf("%d", &q);
            int u, v;
            while (q--)
            {
                scanf("%d%d", &u, &v);
                ll tmp = 0, cost = 0;
                    int size = tracost.size();
                    for (int i = 0; i < size; i++)
                    {
                        if (tmp + u <= v)
                            tmp += u, cost += tracost[i] * u;
                        else if (tmp < v)
                            cost += tracost[i] * (v - tmp), tmp = v;
                        else
                            break;
                    }
                    if (tmp == v)
                    {
                        ll gcd = __gcd(cost, v);
                        printf("%lld/%lld
    ", cost / gcd, v / gcd);
                    }
                    else printf("NaN
    ");
            }
        }
        return 0;
     
    }
    
  • 相关阅读:
    CSS---@import
    C语言中float,double类型,在内存中的结构(存储方式)
    科普:字,字长,字节,位
    mysql的字段类型范围必须重视起来
    print,print_r,echo,var_dump,var_export比较
    常见编码格式
    php截取字符串,无乱码
    MYSQL配置详解
    Mysql主从复制,读写分离
    17173php招聘
  • 原文地址:https://www.cnblogs.com/ruanbaiQAQ/p/13337178.html
Copyright © 2020-2023  润新知