1. 程式人生 > >uva-10670-貪心

uva-10670-貪心

題意:從N開始,目標是把數字變成M,每個代理有倆個操作,讓數字減一或者變成一半,求最小的花費

能減半就減半.

 
#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
namespace cc
{
	using std::cout;
	using std::endl;
	using std::cin;
	using std::map;
	using std::vector;
	using std::string;
	using std::sort;
	using std::priority_queue;
	using std::greater;
	using std::vector;
	using std::swap;
	using std::stack;
	using std::bitset;



	class Input
	{
	public:
		char* name;
		int a=0;
		int b=0;
		int cost=0;
		Input() {
			name = new char[50];
		};
	};

	constexpr int L = 110;

	Input ins[L];

	bool cmp(Input& a, Input& b)
	{
		if (a.cost < b.cost)
			return true;
		if (a.cost > b.cost)
			return false;
		return strcmp(a.name, b.name) < 0;
	}
	void solve2(int LL, int N, int M)
	{
		for (int i = 0;i < LL;i++)
		{
			//計算花費
			int h = N;
			while (h/2 >= M && (h - h/2) * ins[i].a >= ins[i].b)
			{
				ins[i].cost += ins[i].b;
				h = h / 2;
				
			}
			if (h > M)
				ins[i].cost += (h - M) * ins[i].a;

		}

		sort(ins, ins + LL, cmp);
		for (int i = 0;i < LL;i++)
			cout << ins[i].name << " " << ins[i].cost << endl;
	}

	void solve()
	{
		int cases;
		cin >> cases;
		int t = 1;
		while (cases--)
		{
			int N, M, LL;
			cin >> N >> M >> LL;
			for (int j = 0;j < LL;j++)
			{
				char* strs = new char[100];
				cin >> strs;
				Input inss;
				sscanf(strs, "%[^:]:%d,%d", inss.name, &inss.a, &inss.b);
				ins[j] = inss;
			}
			cout << "Case " << t << endl;
			t++;
			solve2(LL, N, M);

		}


	}

};


int main()
{

#ifndef ONLINE_JUDGE
	freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
	cc::solve();

	return 0;
}