Codeforces Round #521 D Shop Heap

Source: Internet
Author: User

Title: Given the number of K, n operations, there are three types of operations:

1.a[i]=b

2.a[i]+=b

3.a[i]*=b

Where I and B are the operations specified

You can now perform up to M operations, with each operation being performed at most once, requiring the maximum number of products after the end of the operation

Output the number of each operation in the order in which they are used


In a muddle, I wrote a pitch d ... The result was not transferred out Qaq B also did not write to the end of the written A and C platoon 100+ ...

First, for the same number, all of the operations we perform must be in the order of

Operation 1 perform up to 1 times so we might as well take the operation 1 of each number to the maximum and then treat it as Operation 2.

There's only two operations left, 2 and 3.

3 The effect of the operation is the answer *=b

2 effect of the operation is the answer *= (a[i]+b)/a[i]

But A[i] can change, so you can't just be greedy.

We found that 2 operations are independent of each other but obviously the order of 2 operations per number is better than the size of the operand m.

So we might as well order each of the 2 operations, adding the largest heap

And then every time you take out the top of the heap, if the top of the heap is Operation 2, take the next operation of this number 2 and calculate the current (a[i]+b)/a[i]

Finally, all operations are ordered in the order of type

Time complexity O (MLOGN)

Note A[i] may explode

#include <queue> #include <cmath> #include <vector> #include <cstdio> #include <cstring># Include <iostream> #include <algorithm> #define M 100100#define EPS 1e-9using namespace Std;int k,n,m;int A[m] , To_modify[m];long long _a[m];struct abcd{int type,i,b;} b[m];vector<pair<double,int> > Modifictions[m];p riority_queue<pair<double,int> > Heap;int Stack[m],top;bool Compare (int x,int y) {return b[x].type < B[y].type;}    int main () {//freopen ("Fuck.txt", "R", stdin);    int i;    cin>>k>>n>>m;    for (i=1;i<=k;i++) scanf ("%d", &a[i]), _a[i]=a[i];        for (i=1;i<=n;i++) {scanf ("%d%d%d", &b[i].type,&b[i].i,&b[i].b); if (b[i].type==1) {if (!to_modify[b[i].i]| |        B[TO_MODIFY[B[I].I]].B&LT;B[I].B) to_modify[b[i].i]=i;        } else if (b[i].type==2) Modifictions[b[i].i].push_back (Make_pair (b[i].b,i)); else HEAP.PUsh (pair<double,int> (log (b[i].b), i));     }for (i=1;i<=k;i++) if (To_modify[i]&&b[to_modify[i]].b>a[i]) Modifictions[i].push_back (    Pair<double,int> (B[to_modify[i]].b-a[i],to_modify[i]));    for (i=1;i<=k;i++) if (Modifictions[i].size ()) {sort (Modifictions[i].begin (), Modifictions[i].end ());    Pair<double,int> Temp=modifictions[i].back (); Modifictions[i].pop_back ();    Temp.first=log (A[b[temp.second].i]+temp.first)-log (a[b[temp.second].i]); Heap.push (temp);        } for (I=1;i<=m&&heap.size (); i++) {pair<double,int> temp=heap.top (); Heap.pop ();        Stack[++top]=temp.second;        if (b[temp.second].type==1) _a[b[temp.second].i]+=b[temp.second].b-a[b[temp.second].i];        else if (b[temp.second].type==2) _a[b[temp.second].i]+=b[temp.second].b;        else continue; if (Modifictions[b[temp.second].i].size ()) {pair<double,int> _temp=modificTions[b[temp.second].i].back (); Modifictions[b[temp.second].i].pop_back (); _temp.first=log (_a[b[temp.second].i]+    _temp.first)-log (_a[b[temp.second].i]); Heap.push (_temp);}    } sort (Stack+1,stack+top+1,compare);    cout<<top<<endl; for (i=1;i<=top;i++) printf ("%d%c", Stack[i],i==top? ' \ n ': ');}


Codeforces Round #521 D Shop Heap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.