zoukankan      html  css  js  c++  java
  • codeforces 459 B.Pashmak and Flowers 解题报告

    题目链接:http://codeforces.com/problemset/problem/459/B

    题目意思:有 n 朵 flowers,每朵flower有相应的 beauty,求出最大的beauty 差 和 要达到这个最大的差 的取法有多少种。

       一下子wa,是因为没考虑到整个序列都是相同的beauty 时的情况,以为取法是一种= =。注意,beauty 差为0都是合法的。还有注意这句话:Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way。 就是说,两种flower 只要有一种(当然两种也可以)和之前的取法不相同,就是一个新的取法。

        傻了,相同beauty > 2 时,答案应该是 n * (n-1) / 2!!!竟然写成 n * (n-1)了,这样会有重复取法数啦!!!!排列组合都还给老师了- -

         

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 typedef long long LL;
     9 const int maxn = 2e5 + 5;
    10 int b[maxn];
    11 
    12 int main()
    13 {
    14     int n;
    15     while (scanf("%d", &n) != EOF)
    16     {
    17         for (int i = 0; i < n; i++)
    18             scanf("%d", &b[i]);
    19         sort(b, b+n);
    20         int minn = b[0];
    21         int maxx = b[n-1];
    22         if (minn == maxx)
    23         {
    24             if (n == 2)
    25                 printf("0 1
    ");
    26             else
    27                 printf("0 %lld
    ", (LL)n * (LL)(n-1) / 2);
    28         }
    29         else
    30         {
    31             int cnt1 = 1;
    32             int cnt2 = 1;
    33             for (int i = 1; i < n; i++)
    34             {
    35                 if (minn == b[i])
    36                     cnt1++;
    37                 else
    38                     break;
    39             }
    40             for (int i = n-2; i >= 0; i--)
    41             {
    42                 if (maxx == b[i])
    43                     cnt2++;
    44                 else
    45                     break;
    46             }
    47             printf("%d %lld
    ", maxx-minn, (LL)cnt1*cnt2);
    48         }
    49     }
    50     return 0;
    51 }

         

  • 相关阅读:
    微信打开网址添加在浏览器中打开提示遮罩
    Java内存分配之堆、栈和常量池
    腾讯面试题04.进程和线程的区别?
    cookie 和 session的区别
    jvm内存模型-回收算法-和内存分配以及jdk、jre、jvm是什么关系(阿里,美团,京东面试题)
    HTTP中GET与POST的区别
    Socket send函数和recv函数详解
    JSP九大内置对象及四个作用域
    JavaScript
    网页布局(html+css基础)
  • 原文地址:https://www.cnblogs.com/windysai/p/3916023.html
Copyright © 2011-2022 走看看