zoukankan      html  css  js  c++  java
  • 【codeforces 534C】Polycarpus' Dice

    【题目链接】:http://codeforces.com/contest/534/problem/C

    【题意】

    给你n个奇怪的骰子,第i个骰子有di个面;
    然后给你n个骰子的点数之和;
    问你每一个骰子有哪一些面是不可能出现的;

    【题解】

    对于第i个骰子
    设它的点数为x
    设其他n-1个骰子的最大点数之和(即∑di)为restmax
    如果
    x+restmax< A
    且x是最大的满足这个条件的x;
    则1..x这些点数都不能出现;
    同时还有
    设其他n-1个骰子的最小点数之和(即n-1)为restmin
    如果
    x+restmin >A
    且x是最小的满足这个条件的x
    则x..d[i]这些点数都不能出现;
    这里的两个x其实都能直接算出来。。
    我写了个二分求。。
    所以复杂度应该可以做到O(N)的;不用加上那个对数的;

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%lld",&x)
    #define ref(x) scanf("%lf",&x)
    
    typedef pair<int, int> pii;
    typedef pair<LL, LL> pll;
    
    const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
    const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
    const double pi = acos(-1.0);
    const int N = 2e5+100;
    
    LL n, A;
    LL d[N],restmax,restmin;
    
    int main()
    {
        //freopen("F:\rush.txt", "r", stdin);
        rel(n), rel(A);
        rep1(i, 1, n)
        {
            rel(d[i]);
            restmax += d[i];
        }
        restmin = n;
        rep1(i, 1, n)
        {
            restmax -= d[i], restmin--;
            LL x1 = 0,x2 = d[i]+1;
            LL l = 0, r = d[i] + 1;
            while (l <= r)
            {
                LL m = (l + r) >> 1;
                if (m + restmax < A)
                {
                    x1 = m;
                    l = m + 1;
                }
                else
                    r = m - 1;
            }
            l = 0, r = d[i] + 1;
            while (l <= r)
            {
                LL m = (l + r) >> 1;
                if (m + restmin > A)
                {
                    x2 = m;
                    r = m - 1;
                }
                else
                    l = m + 1;
            }
            //1..x1都不行
            //x2..d[i]都不行
            if (x1 >= x2)
                printf("%lld", d[i]);
            else
            {
                printf("%lld", x1 + d[i] - x2 + 1);
            }
            if (i == n)
                puts("");
            else
                putchar(' ');
            restmax += d[i], restmin++;
        }
        //printf("
    %.2lf sec 
    ", (double)clock() / CLOCKS_PER_SEC);
        return 0;
    }
  • 相关阅读:
    Worker Threads in C#
    Opera和各种下载工具的右键整合
    两种不用的电动车刹车装置价格竟差了一倍
    SQL Server中CONVERT  将日期格式化
    javascript获得当前文档的相对路径
    arcgis sever9.3 for flex API
    常用ArcGIS Server for java网址
    点线面查询闪烁
    arcmap 中建鹰眼
    将一个包含有exe运行文件的文件夹压缩成exe文件
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626511.html
Copyright © 2011-2022 走看看