zoukankan      html  css  js  c++  java
  • [逆序数, 思维]牛客编程巅峰赛S1第5场

    A: 字符串模拟,简单题

    class Solution {
    public:
        /**
         * 解密密文
         * @param str string字符串 密文
         * @param d int整型 偏移量
         * @return string字符串
         */
        string decode(string str, int d) {
            // write code here
            string ret = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            string ans = "";
            for(auto x : str) {
                int i = 0;
                for (i = 0; ret[i] != x; ++i);
                i = (i + ret.length() - d) % ret.length();
                ans = ans + ret[i];
            }
            return ans;
        }
    };
    

     

    B:枚举,简单题,考虑取模循环,实质上只有最后几位会产生影响,枚举答案去重输出即可

    typedef long long ll;
    class Solution {
    public:
        /**
         * 
         * @param x int整型 
         * @return bool布尔型
         */
        bool solve(int x) {
            // write code here
            set <ll> ans;
            for(int i = 0; i < 50000; ++i) {
                ans.insert(i * i % 1000);
            }
            return ans.count(x);
        }
    };

     

    C:普通题,将窗口考虑为跑道并填补,可知道每个同学完成 + 等待花费的总时间和排名,取反后求逆序对即可,跑道用set维护,逆序对用归并,树状数组都可以维护

     

    constexpr int MAXN = 1e6 + 7;
    typedef long long ll;
    
    class Solution {
    public:
        /**
         * 求解合法的(i,j)对的数量
         * @param n int整型 n个人
         * @param m int整型 m个窗口
         * @param a int整型vector 长度为n的vector,顺序表示1-n号客人的办理业务所需时间
         * @return long长整型
         */
        struct bit
        {
            ll c[MAXN], N; //c树, N数组长
            bit() {}
            bit(int n) { N = n; fill(c, c + N + 1, 0);    } //初始化数组长
            int lowbit(int x) { return x & -x; }
            void update(int pos, ll val)
            {
                for( ;pos <= N; pos += lowbit(pos))
                    c[pos] += val;
            }
            ll ask(int pos)
            {
                ll ret = 0;
                for( ;pos; pos -= lowbit(pos))
                    ret += c[pos];
                return ret;
            }
        };
        
        long long getNumValidPairs(int n, int m, vector<int>& a) {
            // write code here
            multiset<pair<ll, ll>> mt;
            vector <ll> ans;
            bit B(a.size() + 1001);
            a.insert(a.begin(), 0);
            int pos = 1;
            for (int i = 0; i < m; ++i) {
                mt.insert(make_pair(0, 0));
            }
            while (!mt.empty()) {
                auto now = mt.begin();
                if(now->first != 0) {
                    ans.push_back(now->second);
                }
                mt.erase(mt.begin());
                if (pos < a.size()) {
                    mt.insert(make_pair(now->first + a[pos], pos++));
                }
            }
            ll ret = 0;
            reverse(ans.begin(), ans.end());
            for (int i = 0; i < ans.size(); ++i) {
                ret += B.ask(ans[i] - 1);
                B.update(ans[i], 1);
            }
            return ret;
        }
    };
  • 相关阅读:
    第四章 瞬时响应:网站的高性能架构(待续)
    第三章 大型网站核心架构要素(待续)
    Luogu P1140 相似基因 【dp】By cellur925
    矩阵快速幂/矩阵加速线性数列 By cellur925
    [POI2008]BLO-Blockade 【无向图tarjan/鸽点】By cellur925
    USACO Training3.3 A Game【区间Dp】 By cellur925
    Luogu P2858 [USACO06FEB]奶牛零食Treats for the Cows 【区间dp】By cellur925
    Luogu P2921 在农场万圣节 【tarjan in 有向图】 By cellur925
    浅谈扩展欧几里得[exgcd] By cellur925
    NOIp 2014 联合权值 By cellur925
  • 原文地址:https://www.cnblogs.com/zeolim/p/13373290.html
Copyright © 2011-2022 走看看