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
  • 相关阅读:
    张小龙:微信十周年总结
    天呐!!!竟还有人不知道如何将Python程序打包成exe
    Making Games with Python & Pygame 中文翻译
    turtle怎么引入背景图片
    Python少儿编程全集(一):一只小海龟(turtle库的使用)
    C/C++基础编程
    少儿编程论坛,汇集所有少儿编程资源!!!
    少儿编程:认识python中的turtle库(一)
    博客导航栏,衣渐衫衣终不悔,沉淀技术这十年(持续更新...)
    SpringBoot学习笔记【尚硅谷】
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5496084.html
Copyright © 2011-2022 走看看