zoukankan      html  css  js  c++  java
  • 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门

     1 /*
     2     题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1
     3     贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了
     4 */
     5 #include <cstdio>
     6 #include <algorithm>
     7 #include <cstring>
     8 #include <cmath>
     9 using namespace std;
    10 
    11 typedef long long ll;
    12 
    13 const int MAXN = 1e5 + 10;
    14 const int INF = 0x3f3f3f3f;
    15 int a[MAXN];
    16 ll x[MAXN];
    17 int vis[MAXN];
    18 
    19 int main(void)        //Codeforces Round #297 (Div. 2) C. Ilya and Sticks
    20 {
    21     int n;
    22     while (scanf ("%d", &n) == 1)
    23     {
    24         memset (x, 0, sizeof (x));
    25         for (int i=1; i<=n; ++i)    scanf ("%d", &a[i]);
    26         sort (a+1, a+1+n);
    27         int p = 0;
    28         for (int i=n; i>=2; --i)
    29         {
    30             if (a[i] == a[i-1])    {x[p++] = a[i];    i--;}
    31             else if (a[i] == a[i-1] + 1)    {x[p++] = a[i-1];    i--;}
    32         }
    33 
    34         if (p < 2)    puts ("0");
    35         else
    36         {
    37             ll sum = 0;    x[p] = 1;
    38             for (int i=0; i<p; i+=2)
    39             {
    40                 sum += (x[i] * x[i+1]);
    41             }
    42             if (p & 1)    sum -= x[p-1];
    43             printf ("%I64d
    ", sum);
    44         }
    45     }
    46 
    47     return 0;
    48 }
    49 
    50 
    51 /*
    52 4
    53 2 4 4 2
    54 4
    55 2 2 3 5
    56 4
    57 100003 100004 100005 100006
    58 5
    59 1 1 1 1 1
    60 10
    61 3 3 5 4 2 2 5 6 7 5
    62 */
    编译人生,运行世界!
  • 相关阅读:
    第八周学习进度
    个人NABCD
    软件需求模式阅读笔记一
    问题账户需求分析
    2017年秋季个人阅读计划
    软件需求与分析——读后感
    第十六周周总结
    第十五周周总结
    第十四周周总结
    第十三周周总结
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4531642.html
Copyright © 2011-2022 走看看