Finish your homework in lowest cost

The results:

inputs
4       
2 1 4 2 
3 4 6 8
outputs 
2 4 3 
3  
inputs   
3
1 3 1
6 2 3
outputs:
1 2 
3   
inputs:

0

The codes 

// how to finish the assignments in the lowest cost.
# include <iostream>
# include <iomanip>
# include <algorithm>
# include <list>
# include <vector>
using namespace std;
struct assignment_details{
    int No;
    int Deadline; // to illustrate the deadlines of every assignment
    int Cost;      // the cost of miss the deadline of every assignment
    bool operator< (const assignment_details& other) const { 
        if (Cost == other.Cost) 
            return Deadline < other.Deadline; // if the cost of the assignment are same, sort them by increaing   
        else 
            return Cost > other.Cost;     // if the cost of the assignment are different, sort them by decreaing
    }
   /* assignment_details(int no, int deadline, int cost){
        No = no; Deadline = deadline; Cost = cost;
    }*/
};
ostream& operator<< (ostream& COUT, assignment_details& assigment){
    COUT << "No. : " << assigment.No << endl;
    COUT << "Deadline : " << assigment.Deadline << endl;
    COUT << "Cost : " << assigment.Cost << endl;
    return COUT;
}
void sequence_least_cost(vector<assignment_details>& args_assignments, vector<int>& flag);
void initialize_flag(vector<int>& flag);
int main()
{
    vector<assignment_details> assignments;
    while(true){
        int n; // the number of assignments
        cin >> n;
        if(n == 0){
            break;
        }else{ 
            assignment_details assignment;
            for(int i = 0; i < n; i++){
                assignment.No = i + 1;
                cin >> assignment.Deadline;
                assignments.push_back(assignment);
            }
            for(int j = 0; j < n; j++){
                cin >> assignments[j].Cost;
            }
        }
        sort(assignments.begin(), assignments.end());
        /*for(int i = 0; i < assignments.size(); i++) {
            cout << assignments[i] << endl;
        }*/
        vector<int> flag(n + 10);
        initialize_flag(flag);
        sequence_least_cost(assignments,flag);
        assignments.clear();
        assignments.shrink_to_fit();
        flag.clear();
        flag.shrink_to_fit();
    }

    return 0;
}
void initialize_flag(vector<int>& flag)
{
    for(int i = 0; i < flag.size(); i++){
        flag[i] = 0;
    }
}
void sequence_least_cost(vector<assignment_details>& args_assignments, vector<int>& flag)
{
    int lowest_cost = 0;
    int i, j;
    for(i = 0; i < args_assignments.size(); i++){
        for(j = args_assignments[i].Deadline; j > 0; --j){
            if(flag[j] == 0){
                //cout << setw(3) <<args_assignments[i].No; 
                flag[j] = args_assignments[i].No;
                break;
            }
        }
        if(j == 0){
            lowest_cost += args_assignments[i].Cost;
        }
    }
    for(int i = 0; i < flag.size(); i++){
        if(flag[i] == 0){
        
        }else{
            cout  << flag[i] << " ";
        }
    }
    cout << endl;
    cout << lowest_cost;
}

A easier way to handle this problem, just using data structure of array:
The results:

PS C:\Users\Desktop\practices for python> .\homework3.exe
3
1 3 1 
6 2 3
1 2 
3

The codes:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

/* Assuming that the line segments will not repeat */
int main()
{
	int n;
	cin >> n;
	vector<int> d(n), s(n);
	for (int i = 0; i < n; i++)
		cin >> d[i];
	for (int j = 0; j < n; j++)
		cin >> s[j];

	vector<int> order;
	int deduction = 0;
	while (true) {
		int position = -1;
		/* Xiao v will not do assignment that has been deducted points */
		for (int i = 0; i < d.size(); i++)
			if (d[i] > 0)
				position = i;
		if (position == -1)
			break;

		/* Select a assignment to do */
		double maxdeduct = (double)s[position] / d[position];
		for (int i = 0; i < d.size(); i++)
			if (d[i] > 0 && maxdeduct < (double)s[i] / d[i]) {
				maxdeduct = (double)s[i] / d[i];
				position = i;
			}

		/* Do assignment */
		order.push_back(position+1);
		for (int i = 0; i < d.size(); i++)
			if (i == position)
				d[i] = -1;
			else if (d[i] - 1 == 0) {
				deduction += s[i];
				d[i] = -1;
			} else
				d[i] -= 1;

	}

	for (int i = 0; i < order.size(); i++)
		cout << order[i] << " ";
	cout << endl << deduction;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_38396940/article/details/120936865
今日推荐