zoukankan      html  css  js  c++  java
  • codeforces#297div2_c 贪心

    codeforces#297div2_c 贪心

    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 a1, a2, a3 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, sticks 5 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

     题意:如题

    思路:贪心

    hack点:个数为1时用STL的vector判断size时负数会溢出成大整数

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<string>
    #include<vector>
    
    using namespace std;
    
    const int maxn=1000100;
    const int INF=(1<<29);
    const double EPS=0.0000001;
    
    int n;
    int L[maxn];
    vector<int> v;
    
    bool cmp(int a,int b)
    {
        return a>b;
    }
    
    int main()
    {
        cin>>n;
        memset(L,0,sizeof(L));
        for(int i=0;i<n;i++) scanf("%d",&L[i]);
        sort(L,L+n,cmp);
        int tmp=L[0];
        for(int i=1;i<n;){
            if(abs(tmp-L[i])<=1){
                v.push_back(L[i]);
                tmp=L[i+1];
                i+=2;
            }
            else tmp=L[i++];
        }
        long long ans=0;
        for(int i=0;i<(int)v.size()-1;i+=2){///这里size是无符号整数,要转为int,否则当size为0时size-1溢出成大整数,这是一个hack点
            ans+=v[i]*(long long)v[i+1];
        }
        cout<<ans<<endl;
        return 0;
    }
    View Code
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    20199319 2019-2020-2 《网络攻防实践》第七周作业
    《访问控制策略描述和应用》阅读总结
    密码学基础学习-序列密码
    密码学基础学习-1
    《一种普适计算下的访问控制策略》阅读总结
    20199319 2019-2020-2 《网络攻防实践》第五周作业
    《访问控制策略描述语言与策略冲突研究》阅读总结
    20199319 2019-2020-2 《网络攻防实践》第四周作业
    20199319 2019-2020-2 《网络攻防实践》第三周作业
    记录旧手机(米6)安装Linux(Ubuntu16.04)踩过的坑
  • 原文地址:https://www.cnblogs.com/--560/p/4417350.html
Copyright © 2011-2022 走看看