TOJ 4283 Gifts with different style/Line segment tree-shaped array backpack

Source: Internet
Author: User
Tags ming first row time limit
Gifts with different style time limit (normal/java): 1000ms/3000ms Run memory limit: 65536KByte

Description

A few days ago GBQC country of Xiaoming in Phoenix City to play for two days, before leaving hope to give a girlfriend back two different kinds of souvenirs, and Xiao Ming's backpack can only accommodate the total volume of items not exceeding V, each souvenir so that his girlfriend's happy degree of increase value is not all the same.

Now xiaoming want to know, for Xiao Ming has the money to buy n a souvenir, he needs to buy back which two to make a girlfriend's happiness increase value of the maximum.

input

The input contains multiple sets of test data.

For each set of test data, the first row contains three integers n (1<=n<=10^5), C (1<=c<=10^4), V (1<=v<=10^4), where n means a total of n souvenirs that Xiaoming has the financial resources to buy, C said that the market for souvenirs can be divided into a total of C categories, V said Xiao Ming's backpack can only accommodate the volume of items not exceeding v. Next n rows, each row with three positive integers I (1<=i<=c), J

Output

For each set of test data, an integer line is used to indicate how much Bob can increase his girlfriend's happiness. If Xiaoming has no way to bring back two different kinds of souvenirs, then output "1" (not including quotation marks).

Sample Input

2 3 5
1 2 3
2 4 1
3 3 5
1 2 4
1 2 5
3 3 2

Sample Output

-1
7

Tree Array function Find maximum value

#include <stdio.h> #include <algorithm> #include <string.h> using namespace std;
	struct node {int c;
	int V;
int p;
}A[100010];
int n,c,v;

int map[10010]; 
BOOL CMP (node A,node b) {return A.C < B.C;} int lowbit (int t) {return T & (t ^ (t-1)); 
    } int sum (int end) {int sum = 0; 
        while (end > 0) {if (sum < map[end]) sum = Map[end]; 
    End-= lowbit (end); 
} return sum; 
		} void Update (int pos, int num) {while (pos <= 10000) {if (Map[pos] < num) Map[pos] = num; 
    pos + = Lowbit (POS);
	}} int main () {int i,j,k,max;
		while (scanf ("%d%d%d", &n,&c,&v)!=eof) {memset (map,0,sizeof (map));
		max = 0;
		for (i = 0;i < n; i++) scanf ("%d%d%d", &AMP;A[I].C,&AMP;A[I].V,&AMP;A[I].P);
		Sort (a,a+n,cmp); for (i = 0,k = 0;i < n; i++) {if (i && a[i].c! = a[i-1].c) {for (j = k; J < I; J + +) {up
				Date (A[J].V,A[J].P);
	}			K = i;
			} int t = Sum (V-A[I].V);
		if (t && a[i].p + T > max) max = a[i].p + t;
		} if (max) printf ("%d\n", Max);			
	Else puts ("-1");
} return 0; }

Below is the line tree
 

#include <stdio.h> #include <algorithm> #include <string.h> using namespace std;
	struct node {int c;
	int V;
int p;
}B[100010];
	struct NOD {int l;
	int R;
int num;
}A[40010]; BOOL CMP (node A,node b) {return A.C < B.C;} int max (int x,int y) {return x > y? x:y;} void build (int l,int
	R,int rt) {a[rt].l = l;
	A[RT].R = R;
	A[rt].num = 0;
	if (L = = r) return;
	int m = (L + r) >> 1;
	Build (l,m,rt<<1);
Build (m+1,r,rt<<1|1);
		} void Update (int v,int P,int RT) {if (a[rt].l = = A[RT].R) {if (A[rt].num < p) A[rt].num = p;
	Return
	} int m = (a[rt].l + a[rt].r) >> 1;
	if (v <= m) update (V,P,RT&LT;&LT;1);
	else update (V,P,RT&LT;&LT;1|1);
A[rt].num = max (a[rt<<1].num,a[rt<<1|1].num);
	} int query (int lv,int rv,int RT) {if (a[rt].l >= LV && RV >= a[rt].r) {return a[rt].num;
	} int ret = 0;
	int m = (a[rt].l + a[rt].r) >> 1; if (LV <= m) ret = max (Ret,query (lv,rv,rt<<1));
	if (rv > m) ret = max (Ret,query (lv,rv,rt<<1|1));
return ret;
	} int main () {int n,c,v;
	int i,j,k,ma;
		while (scanf ("%d%d", &n,&c,&v)!=eof) {build (1,v,1);
		Ma = 0;
		for (i = 0;i < n; i++) scanf ("%d%d%d", &AMP;B[I].C,&AMP;B[I].V,&AMP;B[I].P);
		Sort (b,b+n,cmp); for (i = 0,k = 0;i < n; i++) {if (i && b[i].c! = b[i-1].c) {for (j = k; J < I; J + +) {up
				Date (b[j].v,b[j].p,1);
			} k = i;
			} int t = query (1,v-b[i].v,1);
		if (t && b[i].p + T > Ma) ma = b[i].p + t;
		} if (MA) printf ("%d\n", MA);			
	Else puts ("-1");
} return 0; }


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.