• ICPC North Central NA Contest 2018


    A

    题解

    rmq

    代码

    #include <bits/stdc++.h>
    #define all(n) (n).begin(), (n).end()
    #define se second
    #define fi first
    #define pb push_back
    #define mp make_pair
    #define rep(i,a,b) for(int i=a;i<=b;++i)
    #define per(i,a,b) for(int i=a;i>=b;--i)
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> PII;
    typedef vector<int> VI;
    typedef double db;
    
    const int maxn = 2e5 + 5;
    
    int n, _, k, l, m;
    int f[maxn][30], a[maxn];
    PII rk[maxn];
    string s[maxn];
    
    void rmq_init()
    {
        memset(f, 0x3f, sizeof f);
        rep (i, 1, n - 1)
        {
            int len = 0;
            for (; len < s[rk[i].fi].size() && len < s[rk[i + 1].fi].size(); ++len)
                if (s[rk[i].fi][len] != s[rk[i + 1].fi][len]) break;
            f[i][0] = len;
        }
        for (int j = 1, mj = 2; mj <= n; ++j, mj <<= 1)
            for (int i = 1; i + mj <= n; ++i)
                f[i][j] = min(f[i][j - 1], f[i + (mj >> 1)][j - 1]);
    }
    
    int rmq_query(int l, int r)
    {
        l = rk[l].se, r = rk[r].se;
        if (l > r) swap(l, r);
        int k = r - l < 2 ? 0 : ceil(log2(r - l)) - 1;
        return min(f[l][k], f[r - (1 << k)][k]);
    }
    
    bool cmp(PII a, PII b)
    {
        return s[a.first] < s[b.first];
    }
    
    bool cmp2(int a, int b)
    {
        return rk[a].se < rk[b].se;
    }
    
    int main()
    {
        ios::sync_with_stdio(0); cin.tie(0);
        cin >> n >> m;
        rep (i, 1, n) cin >> s[i], rk[i].fi = i;
        sort(rk + 1, rk + 1 + n, cmp);
        rep (i, 1, n) rk[rk[i].fi].se = i;
        rmq_init();
        rep (_, 1, m)
        {
            cin >> k >> l; ll ans = 0;
            rep (i, 1, k) cin >> a[i];
            sort(a + 1, a + 1 + k, cmp2);
            rep (i, l, k)
            {
                int x = 0, y = 0, z = 0;
                if (l == 1) x = s[a[i]].size();
                else x = rmq_query(a[i], a[i - l + 1]);
                if (i > l) z = rmq_query(a[i], a[i - l]);
                if (i < k) y = rmq_query(a[i + 1], a[i - l + 1]);
                if (x > y && x > z) ans += min(x - y, x - z);
            }
            cout << ans << '
    ';
        }
        return 0;
    }

    B

    题解

    dp 我优化过了, 不优化就是

    dp[i][j] 到第j个数字分i段, dp[i][j] = max{dp[i][j - 1], dp[i - 1][k]} + num[j],  k 为 dp[i - 1][i - 1 ~ j - 1]最大值的下标

    代码

    #include <bits/stdc++.h>
    #define all(n) (n).begin(), (n).end()
    #define se second
    #define fi first
    #define pb push_back
    #define mp make_pair
    #define rep(i,a,b) for(int i=a;i<=b;++i)
    #define per(i,a,b) for(int i=a;i>=b;--i)
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> PII;
    typedef vector<int> VI;
    typedef double db;
    
    const int maxn = 5005;
    
    int n, m, _;
    ll dp[maxn], num[maxn]; 
    // dp[i][j] 到第j个数字分i段, dp[i][j] = max{dp[i][j - 1], dp[i - 1][k]} + num[j], 
    // k dp[i - 1][i - 1 ~ j - 1]最大值 
    
    ll solve()
    {
        rep (i, 1, m)
        {
            ll step = 0;
            rep (k, 1, i) step += num[k];
            dp[n] = step;
            rep (j, i + 1, n)
            {
                step = max(step, dp[j-1]) + num[j]; // dp[i][j], 现在dp[j -1] 存的是 dp[i - 1][k]
                dp[j - 1] = dp[n]; // 存dp[i][i ~ j - 1]的最大值, 即dp[i][k]
                dp[n] = max(step, dp[n]); // 答案
            }
        }
        return dp[n];
    }
    
    int main()
    {
        ios::sync_with_stdio(0); cin.tie(0);
        cin >> n >> m;
        rep (i, 1, n) dp[i]=0, cin >> num[i];
        cout << solve();
        return 0;
    }

    C

    题解

    小学的将混循环小数转换分数

    代码

    #include <bits/stdc++.h>
    #define all(n) (n).begin(), (n).end()
    #define se second
    #define fi first
    #define pb push_back
    #define mp make_pair
    #define rep(i,a,b) for(int i=a;i<=b;++i)
    #define per(i,a,b) for(int i=a;i>=b;--i)
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> PII;
    typedef vector<int> VI;
    typedef double db;
    
    const int maxn = 1e5;
    
    int n, m, _;
    
    ll pow(ll a, int b)
    {
        int c = 1;
        for (; b; b >>= 1, a = a * a)
            if (b & 1) c = c * a;
        return c;
    }
    
    int main()
    {
        ios::sync_with_stdio(0); cin.tie(0);
        string s; bool flag = 1;
        cin >> s >> s;
        ll x = 0, y; int w = 0;
        rep (i, 0, s.size() - n - 1)
            if (flag && s[i] != '.') x = x * 10 + s[i] - '0';
            else if (s[i] == '.') flag = 0;
            else x = x * 10 + s[i] - '0', ++w;
        y = x;
        rep (i, s.size() - n, s.size() - 1) y = y * 10 + s[i] - '0';
        ll a = y - x, b = pow(10, w + n) - pow(10, w), c = __gcd(a, b);
        cout << a / c << '/' << b / c;
        return 0;
    }

    E

    题解

    暴力

    代码

    #include <bits/stdc++.h>
    #define all(n) (n).begin(), (n).end()
    #define se second
    #define fi first
    #define pb push_back
    #define mp make_pair
    #define rep(i,a,b) for(int i=a;i<=b;++i)
    #define per(i,a,b) for(int i=a;i>=b;--i)
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> PII;
    typedef vector<int> VI;
    typedef double db;
    
    const int maxn = 1e5;
    
    int n, m, _;
    
    int main()
    {
        //ios::sync_with_stdio(0); cin.tie(0);
        cin >> n; double ans = 1, l = 1;
        if(!n) { printf("%.15lf", l); return 0; }
        rep (i, 1, n) { l /= i; ans += l; }
        printf("%.15lf
    ", ans);
        return 0;
    }

    F

    题解

    排序暴力

    代码

    #include <bits/stdc++.h>
    #define all(n) (n).begin(), (n).end()
    #define se second
    #define fi first
    #define pb push_back
    #define mp make_pair
    #define rep(i,a,b) for(int i=a;i<=b;++i)
    #define per(i,a,b) for(int i=a;i>=b;--i)
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> PII;
    typedef vector<int> VI;
    typedef double db;
    
    const int maxn = 2e5 + 5;
    
    int n, m, _;
    pair<double, double> a[maxn];
    
    int main()
    {
        //ios::sync_with_stdio(0); cin.tie(0);
        cin >> n; double l = -1e12;
        rep (i, 1, n) cin >> a[i].fi >> a[i].se;
        sort(a + 1, a + 1 + n);
        rep (i, 2, n) l = max(l, fabs(a[i].se - a[i - 1].se) / fabs(a[i].fi - a[i - 1].fi));
        printf("%.9lf
    ", l);
        return 0;
    }
    

      

    I

    题解

    思维题, 只要能让羊 or 狼和菜一直在船上 ,用余下的空间运送另一部分

    若羊 or 狼和菜一直在船上, 没有余下的部分, 就先把则部分运过去, 再回去运另一部分, 在把这部分运回来, 再把另的另一部分没运完的运过去, 再回来把这部分运过去

      eg 羊 a, 狼 + 菜 b 且 a < b,a == k,  k < b <= 2 * k (重点), 其他情况想想就有了

         a, b, 人   河流 

          b         河流     a, 人

          b - k    河流     a, k(狼和菜), 人

        a (b - k) 人 河流      k(狼和菜)

             a           河流      b, 人

                                    河流     a, b , 人

    代码

    #include <bits/stdc++.h>
    #define all(n) (n).begin(), (n).end()
    #define se second
    #define fi first
    #define pb push_back
    #define mp make_pair
    #define rep(i,a,b) for(int i=a;i<=b;++i)
    #define per(i,a,b) for(int i=a;i>=b;--i)
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> PII;
    typedef vector<int> VI;
    typedef double db;
    
    const int maxn = 1e5;
    
    int n, m, _;
    
    int main()
    {
        ios::sync_with_stdio(0); cin.tie(0);
        int w, s, c, k; cin >> w >> s >> c >> k;
        if (s > (w = w + c)) swap(s, w);
        if(k > s || k == s && w <= 2 * k) cout << "YES
    ";
        else cout << "NO
    ";
        return 0;
    }

    J

    题解

    暴力

    代码

    #include <bits/stdc++.h>
    #define all(n) (n).begin(), (n).end()
    #define se second
    #define fi first
    #define pb push_back
    #define mp make_pair
    #define rep(i,a,b) for(int i=a;i<=b;++i)
    #define per(i,a,b) for(int i=a;i>=b;--i)
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> PII;
    typedef vector<int> VI;
    typedef double db;
    
    const int maxn = 1e5;
    
    int n, m, _;
    
    bool check(int a, int b)
    {
        stack <int> st; VI v;
        while(a) { int c = a%b; st.push(c); a /= b; }
        while(!st.empty()) { v.pb(st.top()); st.pop(); }
        for (int i = 0, j = v.size() - 1; i < j; ++i, --j)
            if (v[i] != v[j]) return false;
        return true;
    }
    
    int main()
    {
        ios::sync_with_stdio(0); cin.tie(0);
        int a, b, k; cin >> a >> b >> k;
        rep (i, a, b)
        {
            bool flag = 1;
            for (int j = 2; j <= k && flag; flag = check(i, j++));
            m += flag;
        }
        cout << m;
        return 0;
    }
    

      

  • 相关阅读:
    2016年第9本:系统之美
    2016年第8本:不可思议的心理控制实验
    2016年第7本:非暴力沟通
    用SDWebImage渐变加载图片
    iOS相册、相机、通讯录权限获取
    屏蔽iOS10模拟器海量的垃圾debug信息
    Swift
    Swift
    PlaceholderImageView
    Swift
  • 原文地址:https://www.cnblogs.com/2aptx4869/p/12808393.html
Copyright © 2020-2023  润新知