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

    C. Ilya and Sticks
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length li.

    Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximizes their total area. Each stick is used in making at most one rectangle, it is possible that some of sticks remain unused. Bending sticks is not allowed.

    Sticks with lengths a1a2a3 and a4 can make a rectangle if the following properties are observed:

    • a1 ≤ a2 ≤ a3 ≤ a4
    • a1 = a2
    • a3 = a4

    A rectangle can be made of sticks with lengths of, for example, 3 3 3 3 or 2 2 4 4. A rectangle cannot be made of, for example, sticks5 5 5 7.

    Ilya also has an instrument which can reduce the length of the sticks. The sticks are made of a special material, so the length of each stick can be reduced by at most one. For example, a stick with length 5 can either stay at this length or be transformed into a stick of length 4.

    You have to answer the question — what maximum total area of the rectangles can Ilya get with a file if makes rectangles from the available sticks?

    Input

    The first line of the input contains a positive integer n (1 ≤ n ≤ 105) — the number of the available sticks.

    The second line of the input contains n positive integers li (2 ≤ li ≤ 106) — the lengths of the sticks.

    Output

    The first line of the output must contain a single non-negative integer — the maximum total area of the rectangles that Ilya can make from the available sticks.

    Sample test(s)
    input
    4
    2 4 4 2
    
    output
    8
    
    input
    4
    2 2 3 5
    
    output
    0
    
    input
    4
    100003 100004 100005 100006
    
    output
    10000800015
    



    题目链接:点击打开链接

    给出n个棒子的长度, 每一个棒子能够变为自己的len - 1, 问能够组成题目中要求的矩形的面子最大是多少.

    将棒子排序, 由大到小遍历棒子, 因为每一个棒子能够变成自己的len - 1, 若相邻棒子差值为1或0, 则四边构成一个矩形, 累加得到答案.

    AC代码:

    #include "iostream"
    #include "cstdio"
    #include "cstring"
    #include "algorithm"
    #include "queue"
    #include "stack"
    #include "cmath"
    #include "utility"
    #include "map"
    #include "set"
    #include "vector"
    #include "list"
    #include "string"
    #include "cstdlib"
    using namespace std;
    typedef long long ll;
    const int MOD = 1e9 + 7;
    const int INF = 0x3f3f3f3f;
    const int MAXN = 1e6 + 5;
    int n;
    ll x, ans;
    int main(int argc, char const *argv[])
    {
    	cin >> n;
    	vector<int> v(n);
    	for(int i = 0; i < n; ++i)
    		cin >> v[i];
    	sort(v.begin(), v.end());
    	for(int i = n - 2; i >= 0; --i)
    		if(v[i + 1] - v[i] < 2) {
    			if(x) {
    				ans += x * v[i];
    				x = 0;
    			}
    			else x = v[i];
    			i--;
    		}
    	cout << ans << endl;
    	return 0;
    }


  • 相关阅读:
    中国大学排名爬虫
    基于bs4库的HTML内容查找方法和HTML格式化和编码
    自动化提取51啦数据的信息
    简单目录扫描工具
    一个简单音乐播放器
    【原创】C++中对象的序列化
    [android]android开发中的运行错误之:adb.exe
    [转载]十大编程算法助程序员走上高手之路
    [原创]二叉树相关笔试题代码
    [原创]VS2010中创建动态链接库及其调用
  • 原文地址:https://www.cnblogs.com/llguanli/p/8666524.html
Copyright © 2011-2022 走看看