Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

类似背包问题,数据很弱,剪纸就过(附代妈)

Posted by KatrineYang at 2016-06-14 16:30:19 on Problem 1040
//============================================================================
// Name        : main1040.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

int maxi = 0;
int cur = 0;
int state[10] = {0};
int n;

void clear(){
	maxi = 0;
	cur = 0;
	for(int i = 0; i < 10; i++) state[i] = 0;
}

int Max(int a, int b){
	if(a < b) return b;
	return a;
}

class order{
public:
	int start;
	int end;
	int num;
	int price;
	order(int s, int e, int n): start(s), end(e), num(n), price(0){}
	order():start(0), end(0), num(0), price(0){}
};

int partion(order *array, int p, int r) {
		order x = array[r];
		int i = p - 1;//注意这点,把i设成负值,然后作为移动的标志
		int j;
		for (j = p; j < r; j++) {
			if (array[j].price > x.price) {
				i++;
				order temp = array[j];
				array[j] = array[i];
				array[i] = temp;
			}
		}
		order temp = array[j];
		array[j] = array[i + 1];
		array[i + 1] = temp;
		return i+1;//返回的应该是交换后的哨兵的位置
}
		//递归解决每个划分后的小
void quickSort(order* array, int p, int r) {
		if (p < r) {
			int q = partion(array, p, r);
			quickSort(array, p, q - 1);
			quickSort(array, q + 1, r);
		}
}

void findMax(order* orders, int *psum, int ord, int plc){
	//最初plc为orders的长度
	if(plc == 0) return;
	//首先判断当前的order是否可以被接受
	int idx = ord - plc;
	order temp = orders[idx];
	bool can = true;
	for(int i = temp.start; i < temp.end; i++){
		if(state[i] + temp.num > n){
			can = false;
			break;
		}
	}
	if(can){
		cur += temp.price;
		for(int i = temp.start; i < temp.end; i++) state[i] += temp.num;
		maxi = Max(maxi, cur);
		findMax(orders, psum, ord, plc-1);
		cur -= temp.price;
		for(int i = temp.start; i < temp.end; i++) state[i] -= temp.num;
	}
	//如果不选这个,看看剩下的是否能达到max,不能达到就不理
	if(cur + psum[idx+1] <= maxi) return;
	findMax(orders, psum, ord, plc-1);

}

int main() {
	while(1){
		int  m, ord;
		cin >> n >> m >> ord;
		if(n == 0 && m == 0 && ord == 0) return 0;
		order *orders = new order[ord];
		for(int i = 0; i < ord; i++){
			int s, e, N;
			cin >> s >> e >> N;
			orders[i].start = s;
			orders[i].end = e;
			orders[i].num = N;
			orders[i].price = (e-s)*N;
		}
		quickSort(orders, 0, ord-1);
		clear();
		int psum[30] = {0};
		for(int i = ord-1; i >= 0; i--) psum[i] = psum[i+1] + orders[i].price;
		findMax(orders, psum, ord, ord);
		cout << maxi << endl;
	}
	//cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator