zoukankan      html  css  js  c++  java
  • 华农校赛--G,用set比较大小,缩短时间复杂度

    Array C

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 581  Solved: 101
    [Submit][Status][Web Board]

    Description

       Giving two integers  and  and two arrays  and  both with length , you should construct an array  also with length  which satisfied:

    1.0≤CiAi(1≤in)

    2.

    and make the value S be minimum. The value S is defined as:

    Input

       There are multiple test cases. In each test case, the first line contains two integers n(1≤n≤1000) andm(1≤m≤100000). Then two lines followed, each line contains n integers separated by spaces, indicating the array Aand B in order. You can assume that 1≤Ai≤100 and 1≤Bi≤10000 for each i from 1 to n, and there must be at least one solution for array C. The input will end by EOF.

    Output

        For each test case, output the minimum value S as the answer in one line.

    Sample Input

    3 4 
    2 3 4
    1 1 1

    Sample Output

    6

    HINT

        In the sample, you can construct an array [1,1,2](of course [1,2,1] and [2,1,1] are also correct), and  is 6.

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<stdlib.h>
    #include<cmath>
    #include<algorithm>
    #include<set>
    using namespace std;
    struct Node
    {
        long long a;
        long long b;
        long long c;
        long long num;
        int i;
        bool operator < (const Node& t)const
        {
            return ((num>t.num)|| (num==t.num&&a<t.a)|| (num==t.num&&a==t.a&&b<t.b)||(num==t.num&&a==t.a&&b==t.b&&c<t.c)||(num==t.num&&a==t.a&&b==t.b&&c==t.c&&i<t.i));
        }
    
    } node[1005];
    set<Node>s;
    
    
    int main()
    {
        int n,m;
    
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            long long res=0;
            long long sum=0;
            s.clear();
            for(int i=0; i<n; i++)
                scanf("%I64d",&node[i].a);
            for(int i=0; i<n; i++)
                scanf("%I64d",&node[i].b);
            for(int i=0; i<n; i++)
            {
                node[i].i=i;
                node[i].c=node[i].a;
                node[i].num=(2*node[i].c-1)*node[i].b;
                res+=node[i].c*node[i].c*node[i].b;
                sum+=node[i].a;
                s.insert(node[i]);
            }
            // cout<<res<<endl;
            Node tmp;
            set<Node>::iterator iter;
            for(int i=sum; i>m; i--)
            {
               // for(iter=s.begin(); iter!=s.end(); iter++)
                   // cout<<iter->num<<"  ";
                tmp=(*s.begin());
                //cout<<tmp.num<<"***"<<res<<endl;
    
                s.erase(tmp);
                res-=tmp.num;
                tmp.c-=1;
                //out<<tmp.a<<endl;
                tmp.num=(2*tmp.c-1)*tmp.b;
                s.insert(tmp);
    
                //cout<<endl;
    
            }
            printf("%lld
    ",res);
    
        }
        return 0;
    }
    View Code
  • 相关阅读:
    进度条简单实现
    bootstrap学习(二)-----Modal模态框
    PL/SQL Developer登入时候报ORA-12638: 身份证明检索失败的解决办法
    pdf.js在IIS中配置使用笔记
    JSON数据查询方法
    Visual Studio 2013 错误提示“未找到与约束匹配”的修正
    WebStorm 11激活方法
    Xamarin开发Android笔记:使用ZXing进行连续扫描
    Xamarin开发IOS笔记:切换输入法时输入框被遮住
    Xamarin开发Android笔记:拍照或相册选取图片角度问题
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5496084.html
Copyright © 2011-2022 走看看