SRM 588 D2 L2:GUMIAndSongsDiv2,冷静思考,好的算法简洁明了

题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12707


算法决定一切,这道题目有很多方法解,个人认为这里 vexorian 给出的解法最简便,编码也最容易。而使用brute force 和 DP都比较复杂。


代码如下:

 

#include <algorithm>

#include <iostream>

#include <sstream>



#include <string>

#include <vector>

#include <stack>

#include <deque>

#include <queue>

#include <set>

#include <map>



#include <cstdio>

#include <cstdlib>

#include <cctype>

#include <cmath>

#include <cstring>



using namespace std;





/*************** Program Begin **********************/



class GUMIAndSongsDiv2 {

public:

    int maxSongs(vector <int> duration, vector <int> tone, int T) {

	int res = 0;

	int N = duration.size();



	vector <int> songs;

	for (int i = 0; i < N; i++) {

		for (int j = i; j < N; j++) {

			int maxTone = max(tone[i], tone[j]);

			int minTone = min(tone[i], tone[j]);

			songs.clear();

			for (int k = 0; k < N; k++) {

				if (tone[k] <= maxTone && tone[k] >= minTone) {

					songs.push_back(duration[k]);

				}

			}

			sort(songs.begin(), songs.end());

			int sum = 0;

			int c = 0;

			for (int k = 0; k < songs.size(); k++) {

				sum += songs[k];

				if (sum <= T - maxTone + minTone) {

					++c;

				}

			}

			res = max(res, c);

		}

	}



	return res;

    }

};



/************** Program End ************************/


 

 

你可能感兴趣的:(div)