贪心 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 */
时间: 2024-08-02 15:14:14

贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks的相关文章

Codeforces Round #297 (Div. 2)C. Ilya and Sticks

题意:给你n 个木头的长度   ,  问你组成矩形的最大总面积为多少,  且木头长度L   可以当作L-1 来使用. 题解:很显然   组成矩形要保证总面积最大   只有大的和大的边组成才能保证面积最大,并且如果当前大的为偶数条那么不需要变成-1  否则变成减1   从头到尾扫一遍  没了 代码: #include<stdio.h> #include<iostream> #include<algorithm> using namespace std; __int64 v

模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie

题目传送门 1 /* 2 模拟:这就是一道模拟水题,看到标签是贪心,还以为错了呢 3 题目倒是很长:) 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 using namespace std; 11 12 const int MAXN = 2e5 + 10; 13

BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

题目传送门 1 /* 2 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 3 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 4 在2*2的方格里,若只有一个是'*',那么它一定要被替换掉 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #include <queue> 1

递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries

题目传送门 1 /* 2 递推:用cnt记录前缀值,查询区间时,两个区间相减 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cmath> 7 #include <cstring> 8 using namespace std; 9 10 const int MAXN = 1e5 + 10; 11 const int INF = 0x3f3f3f3f; 12 char s[MAXN]; 1

Codeforces Round #258 (Div. 2) A. Game With Sticks(数学题)

题目链接:http://codeforces.com/contest/451/problem/A ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma

Codeforces Round #258 (Div. 2/A)/Codeforces451A_Game With Sticks

解题报告 http://blog.csdn.net/juncoder/article/details/38102263 n和m跟木棍相交,问一人取一交点(必须是交点,且取完后去掉交点的两根木棍),最后谁赢 思路: 取最大正方形,以对角线上的交点个数判断输赢. #include <iostream> #include <cstdio> using namespace std; int main() { int m,n; while(cin>>n>>m) { i

贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

题目传送门 1 /* 2 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 3 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 #include <iostream> 10 using namespace std; 11 12 const int MAX

贪心 Codeforces Round #191 (Div. 2) A. Flipping Game

题目传送门 1 /* 2 贪心:暴力贪心水水 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 using namespace std; 8 9 const int MAXN = 1e2 + 10; 10 const int INF = 0x3f3f3f3f; 11 int a[MAXN]; 12 13 int main(void) //Codeforces Round #191

贪心 Codeforces Round #301 (Div. 2) B. School Marks

题目传送门 1 /* 2 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 3 num1是输出的1的个数,numy是除此之外的数都为y,此时的numy是最少需要的,这样才可能中位数大于等于y 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 using na