华农校赛--G,用set比较大小,缩短时间复杂度
Array C
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 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≤Ci≤Ai(1≤i≤n)
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\n",res); } return 0; }