An exhilarating sale.

Source: Internet
Author: User
Tags value of pi

First, the smooth sale
"Problem description"
Mrkill abroad to buy back a lot of things, look on the annoying, really annoying.
On his way home, he suddenly received a notice, not a lot of overseas goods, this is a good reason to
Stall his friends, this pile of things can be sold to overseas cattle to make a difference. A total of n items were counted,
Each item has the weight of WI, the value of pi, but his suitcase is only a load of M, that is, he only
I can sell some of the items, and of course he wants the most money, so he can buy the most games.
Mrkill looking at this pile of products that are about to become steam wallets:
"Hey, that smells good."
"Input Format"
The input file name is sell.in.
Input data first behavior N,m next n lines are WI and pi, respectively
"Output Format"
The input file name is sell.out.
An integer representing the maximum value
"Input and output Example 1"
Sell.in Sell.out
2 2 3
1 3
2 2


4 3 10
3 10
2 7
2 8
1 1
Example 1 Explanation: Only the first product is the best
Example 2 Explanation: Only the first product is the best
"Data size and conventions"
For 20% of data n≤20,
Another 20% of the data n≤10^5, 1≤wi≤2
Another 20% of the data n≤5*10^2, 1≤pi≤20;
For 100% data n≤10^5,m≤3*10^5, 1≤wi≤3, 1≤pi≤10^10

Because of the particularity of WI, the idea is DP, we consider the three kinds of weight of items to consider separately, then the aftereffect is that each commodity in the cost of how much weight has been used several.

So it is easy to think of the value of each product in order to be able to carry out DP.

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include < Algorithm> #define LL Long long#define il inline#define db double#define max (a) > (b)? (  A):(B)) using namespace Std;il int gi () {int x=0,y=1;  Char Ch=getchar (); while (ch< ' 0 ' | |      Ch> ' 9 ') {if (ch== '-') y=-1;    Ch=getchar ();      } while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 ';    Ch=getchar (); } return x*y;}  IL ll GL () {ll x=0,y=1;  Char Ch=getchar (); while (ch< ' 0 ' | |      Ch> ' 9 ') {if (ch== '-') y=-1;    Ch=getchar ();      } while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 ';    Ch=getchar (); } return x*y;}  struct thing{ll p; int W;} t1[100045],t2[100045],t3[100045];il BOOL CMP (thing a,thing b) {return A.P&GT;B.P;}  ll F[300045][4];int main () {int n=gi (), M=gi (), Wei;  int w1=0,w2=0,w3=0;      for (int i=1;i<=n;i++) {wei=gi ();  if (wei==1) {T1[++w1].w=wei;      T1[W1].P=GL ();}  if (wei==2) {T2[++w2].w=wei; T2[W2].P=GL ();}  if (wei==3) {T3[++w3].w=wei;    T3[W3].P=GL ();}  } sort (t1+1,t1+1+w1,cmp);  Sort (t2+1,t2+1+w2,cmp);  Sort (t3+1,t3+1+w3,cmp);  for (int i=1;i<=m;i++) {if (f[i-1][0]+t1[f[i-1][1]+1].p>=f[i][0]) {F[I][0]=F[I-1][0]+T1[F[I-1][1]+1].P;  f[i][1]=f[i-1][1]+1;  F[I][2]=F[I-1][2];      F[I][3]=F[I-1][3];}  if (f[i-2][0]+t2[f[i-2][2]+1].p>=f[i][0]&&i>1) {f[i][0]=f[i-2][0]+t2[f[i-2][2]+1].p;  F[I][1]=F[I-2][1];  f[i][2]=f[i-2][2]+1;      F[I][3]=F[I-2][3];}  if (f[i-3][0]+t3[f[i-3][3]+1].p>=f[i][0]&&i>2) {f[i][0]=f[i-3][0]+t3[f[i-3][3]+1].p;  F[I][1]=F[I-3][1];  F[I][2]=F[I-3][2];    f[i][3]=f[i-3][3]+1;}  } printf ("%lld\n", f[m][0]); return 0;}

An exhilarating sale.

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.