贪心 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\n", 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 */

 

posted @ 2015-05-26 20:12  Running_Time  阅读(125)  评论(0编辑  收藏  举报