zoukankan      html  css  js  c++  java
  • Codeforces Round #352(Div 2)

    又一次半夜打CF。(队友太强直接秒题解,然而我座位比较远耳朵不好(雾)听不见)总之,最后结果400多名。估计还能重新上蓝。

    A题:

    *题目描述:
    请你求这样的序列“1234567891011……”的第n位数字是多少。
    *题解:
    直接模拟。
    *代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    
    #ifdef WIN32
        #define LL "%I64d"
    #else
        #define LL "%lld"
    #endif
    
    #ifdef CT
        #define debug(...) printf(__VA_ARGS__)
        #define setfile() 
    #else
        #define debug(...)
        #define filename ""
        #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
    #endif
    
    #define R register
    #define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
    #define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
    #define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
    #define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
    #define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
    char B[1 << 15], *S = B, *T = B;
    inline int FastIn()
    {
        R char ch; R int cnt = 0; R bool minus = 0;
        while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
        ch == '-' ? minus = 1 : cnt = ch - '0';
        while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
        return minus ? -cnt : cnt;
    }
    int a[2010], stack[1000];
    int main()
    {
    //  setfile();
        R int n = FastIn(), tot = 0;
        for (R int i = 1; tot <= 1000; ++i)
        {
            R int tmp = i, top = 0;
            while (tmp)
            {
                stack[++top] = tmp % 10;
                tmp /= 10;
            }
            for (R int i = top; i; --i)
                a[++tot] = stack[i];
        }
        printf("%d
    ",a[n] );
        return 0;
    }

    B题:

    *题目描述:
    给定一个字符串,求最少需要修改多少位使得每一位都不同。
    *题解:
    统计一下每种字母出现了多少次。然后用长度减一下。
    *代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    
    #ifdef WIN32
        #define LL "%I64d"
    #else
        #define LL "%lld"
    #endif
    
    #ifdef CT
        #define debug(...) printf(__VA_ARGS__)
        #define setfile() 
    #else
        #define debug(...)
        #define filename ""
        #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
    #endif
    
    #define R register
    #define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
    #define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
    #define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
    #define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
    #define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
    char B[1 << 15], *S = B, *T = B;
    inline int FastIn()
    {
        R char ch; R int cnt = 0; R bool minus = 0;
        while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
        ch == '-' ? minus = 1 : cnt = ch - '0';
        while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
        return minus ? -cnt : cnt;
    }
    #define maxn 100010
    int cnt[26];
    char str[maxn];
    int main()
    {
    //  setfile();
        R int n, ans = 0;
        scanf("%d
    ", &n);
        gets(str);
        for (R int i = 0; str[i]; ++i) ++cnt[str[i] - 'a'];
        for (R int i = 0; i < 26; ++i) if (cnt[i]) ans++;
        if (n <= 26) printf("%d
    ",n - ans );
        else puts("-1");
        return 0;
    }

    C题:

    *题目描述:
    二维平面上有n个点,有两个人的坐标和一个终点的坐标,每一次每个人可以走到点上后到终点。问回收完所有的点的最短距离。
    *题解:
    按到两个起点的距离排序。然后分情况讨论。
    *代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    
    #ifdef WIN32
        #define LL "%I64d"
    #else
        #define LL "%lld"
    #endif
    
    #ifdef CT
        #define debug(...) printf(__VA_ARGS__)
        #define setfile() 
    #else
        #define debug(...)
        #define filename ""
        #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
    #endif
    
    #define R register
    #define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
    #define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
    #define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
    #define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
    #define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
    char B[1 << 15], *S = B, *T = B;
    inline int FastIn()
    {
        R char ch; R int cnt = 0; R bool minus = 0;
        while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
        ch == '-' ? minus = 1 : cnt = ch - '0';
        while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
        return minus ? -cnt : cnt;
    }
    #define maxn 100010
    struct Poi
    {
        double x, y;
    }p[maxn], a, b, t;
    #define dist(_i, _j) sqrt((_i.x - _j.x) * (_i.x - _j.x) + (_i.y - _j.y) * (_i.y - _j.y))
    bool vis[maxn];
    struct Dist
    {
        double d;
        int r;
        inline bool operator < (const Dist &that) const {return d < that.d; }
    }d1[maxn], d2[maxn];
    double d3[maxn];
    #define inf 1e18
    int main()
    {
    //  setfile();
        a = (Poi) {FastIn(), FastIn()};
        b = (Poi) {FastIn(), FastIn()};
        t = (Poi) {FastIn(), FastIn()};
        R double ans = 0;
        R int n = FastIn();
        for (R int i = 1; i <= n; ++i) p[i] = (Poi) {FastIn(), FastIn()};
        for (R int i = 1; i <= n; ++i)
        {
            d3[i] = dist(p[i], t);
            d1[i].d = dist(p[i], a) - d3[i];
            d2[i].d = dist(p[i], b) - d3[i];
            ans += d3[i] * 2;
            d1[i].r = i;
            d2[i].r = i;
        }
        std::sort(d1 + 1, d1 + n + 1);
        std::sort(d2 + 1, d2 + n + 1);
        if (d1[1].d < 0 && d2[1].d < 0)
        {
            R double tmp = inf;
            if (d1[1].r != d2[1].r) ans += d1[1].d + d2[1].d;
            else
            {
                cmin(tmp, d2[1].d);
                cmin(tmp, d2[1].d + d1[2].d);
                cmin(tmp, d1[1].d);
                cmin(tmp, d1[1].d + d2[2].d);
                ans += tmp;
            }
        }
        else
            ans += dmin(d1[1].d, d2[1].d);
        printf("%.10lf
    ",ans );
        return 0;
    }

    D题:

    *题目描述:
    有n个人,每个人的资产为ai,每一天鲁滨孙会让最富的那个人给最穷的人一块钱,问k天以后最富的人和最穷的人的差距是多少。
    *题解:
    第一反应是堆强行模拟,然而写完交上去TLE了,然后才看见k<=10^9这个条件,伤心。。。队长Lightning巨强,当场就a了,我比赛后想了一种二分最大值和最小值的方法,然后队长说他的复杂度是线性的(我感觉过去细节巨多)。。。
    我们发现答案实际上是最小的最大值减去最大的最小值,于是乎我果断想到两次二分。求出割掉至多k个的最小的最大值和至多补上k个的最大的最小值。
    *代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    using namespace std; 
    #ifdef WIN32
        #define LL "%I64d"
    #else
        #define LL "%lld"
    #endif
    
    #ifdef CT
        #define debug(...) printf(__VA_ARGS__)
        #define setfile() 
    #else
        #define debug(...)
        #define filename ""
        #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
    #endif
    
    #define R register
    #define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
    #define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
    #define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
    #define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
    #define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
    char B[1 << 15], *S = B, *T = B;
    inline int FastIn()
    {
        R char ch; R int cnt = 0; R bool minus = 0;
        while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
        ch == '-' ? minus = 1 : cnt = ch - '0';
        while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
        return minus ? -cnt : cnt;
    }
    #define maxn 500010
    const int oo = 1e9;
    int v[maxn], n, k;
    inline bool check(R int x, R int opt)
    {
        R long long tmp = 0;
        if (!opt)
        {
            for (R int i = n; i && v[i] > x; --i)
                tmp += v[i] - x;
            return tmp <= k;
        }
        else
        {
            for (R int i = 1; i <= n && v[i] < x; ++i)
                tmp += x - v[i];
            return tmp <= k;
        }
    }
    int main()
    {
    //  setfile();
        n = FastIn(), k = FastIn();
        R long long sum = 0;
        for (R int i = 1; i <= n; ++i)
            v[i] = FastIn(), sum += v[i];
        std::sort(v + 1, v + n + 1);
        if (v[1] == v[n]) return !printf("0
    ");
        R int l = v[1], r = v[n], maxx, minn;
        while (l < r)
        {
            R int mid = l + r >> 1;
            if (check(mid, 0)) r = mid;
            else l = mid + 1;
        }
        maxx = l;
        l = v[1]; r = v[n];
        while (l < r)
        {
            R int mid = l + r + 1 >> 1;
            if (check(mid, 1)) l = mid;
            else r = mid - 1;
        }
        minn = l;
        if (maxx <= minn)
            if (sum % n) puts("1");
            else puts("0");
        else printf("%d
    ",maxx - minn );
        return 0;
    }
  • 相关阅读:
    股市行情指标计算原理和趋势反映--量化交易1-基础
    建立ssh反向隧道
    Linux学习截图保存
    Java内存泄漏
    Java如何正确比较浮点数
    Github使用
    矩阵向量求导
    深度神经网络反向传播算法
    深度神经网络模型与前向传播
    Unity(八)脚本生命周期
  • 原文地址:https://www.cnblogs.com/cocottt/p/5550962.html
Copyright © 2011-2022 走看看