Program design thinking Week4- jobs

Detailed programming Week4- jobs

A-DDL fear

Description

ZJM there are n jobs, each job has its own DDL, if ZJM not done the job before the DDL, then the teacher will deduct this job all the usual points. So ZJM want to know how to arrange the order of homework to buckle a bit less points as possible.
Input of T test. Each test amount of work has N (1 <= N <= 1000 ), and are input DDL deduction of fractional-N jobs.
The output of each test case at least buckle score.

Sample

input:
3
3
3 3 3
10 5 1
3
1 3 1
6 2 3
7
1 4 6 4 2 4 3
3 2 1 7 6 5 4

output:
0
3
5

Idea

First, it contains the ddl and deducted points for each job custom structure. N job for each test case, the first order of decreasing the DDL, and then from back to front enumerate every day, one day schedule for each task, starting from the first element of the array, to enumerate the x day, all DDL = x maximum heap job was added (score maximum heap sort buckle according to each job), and then out the top of the stack from the largest element stack, to arrange this day, added or deducted points until complete enumeration 1 day, if the maximum heap is not empty then these jobs are overdue, adding buckle score. The final output total score buckle

Summary

The basic idea of solving the problem is a greedy algorithm, greedy criterion is forward by one day from the end, for the day to arrange DDL> = the day job buckle fraction biggest job, leaving fewer points deducted.
From the back of the enumerated added only at day x = x DDL ensure the job for the first x day, the maximum stack DDL operation are greater than or equal x, DDL does not erroneously small job leads out from the heap the job timeout.

Codes

#include <iostream>
#include <algorithm>
#include<cstdio>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std;
int T;
struct task {
	int DDL, s;
	//bool operator()(const task &t) {
	//	return s  t.s;
	//}
}P[1100];
struct cmp {
	bool operator()(task a, task b) {
		return a.s < b.s;
	}
};
bool compare(const task &a, const task &b) {
	return a.DDL > b.DDL;
}

int main()
{
	cin.sync_with_stdio(false);
	cin >> T;
	while (T--) {
		int N;
		cin >> N;
		//memset(P, 0, sizeof(task));
		//memset(s, 0, sizeof(int));
		int score = 0;

		for (int i = 0; i < N; i++)
			cin >> P[i].DDL;
		for (int i = 0; i < N; i++)
			cin >> P[i].s;
		sort(P, P + N,compare);
		int index = 0;
		priority_queue<task,vector<task>,cmp > Q;
		for (int t = 1010; t >= 1; t--) {
			//if (index >= N)break;
			for (; index <N; index++) {
				
				if (P[index].DDL == t) { Q.push(P[index]); }
				else if (P[index].DDL < t)break;
			}
			if (!Q.empty()) {
				task temp = Q.top();
				
				Q.pop();
				//if (temp.DDL < t) { score += temp.s;  }

			}

		}
		while (!Q.empty()) {
			task temp = Q.top();
			Q.pop();
			score += temp.s;
			/*cout << temp.s << endl;*/
		}

		cout << score << endl;

	}

}


B- four columns

Description

ZJM has four columns A, B, C, D, each column has the number of digits n. ZJM a number taken from each respective column number, he would like to know how many kinds of programs such that the number of 4 and 0. When a number of columns has the same plurality of numbers, to treat them as a different number.
N to the number of columns of four elements, the number of different output combinations.

Sample

input:
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45

output:
5

Idea

First, each of A and enumeration of number B, A + B was added to each a number of columns, the number of columns in ascending order, and then enumerate the number of each of C and D, for each of C + D, in search of its opposite number the number appearing in the column number, i.e., the ordered sequence to locate the first and last - (C + D), where the need to use a binary search, respectively, to find the first and last, the sum of the two numbers subtract added the final sum is the number of different combinations.

Summary

In one aspect, to reduce complexity, and AB respectively shall enumerate CD, and then each enumerator number ABCD, the complexity will reach the fourth power, equal to the sum of four numbers 0, corresponds to A + B = - (C + D). On the other hand, the key to this question is to find the location of a number of first occurrence and the last occurrence in a ordered sequence, where the use of binary search, the complexity is reduced to logn.

Codes

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
int n;
int A[4010];
int B[4010];
int C[4010];
int D[4010];
vector<int> AB;
int sum = 0;

int find1(int x) {
	int l = 0, r = AB.size() - 1, ans = -1;
	while (l <= r) {
		int mid = l + r >> 1;
		if (AB[mid] == x) {
			ans = mid;
			r = mid - 1;
		}
		else if (AB[mid] > x)r = mid - 1;
		else l = mid + 1;
	}
	return ans;
}

int find2(int x) {
	int l = 0, r = AB.size() - 1, ans = -1;
	while (l <= r) {
		int mid=(l + r) >> 1;
		if (AB[mid] == x) {
			ans = mid;
			l = mid + 1;
		}
		else if (AB[mid] > x)r = mid - 1;
		else l = mid + 1;
	}
	return ans;
}

int main()
{
	cin.sync_with_stdio(false);
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> A[i] >> B[i] >> C[i] >> D[i];
	for(int i=0;i<n;i++)
		for (int j = 0;j < n; j++)
		{
			AB.push_back(A[i] + B[j]);
		}
	sort(AB.begin(), AB.end());
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++) {
			int temp = -(C[i] + D[j]);
			int first = find1(temp);
			int last = find2(temp);

			if (first != -1 && last != -1) { sum += last - first + 1; }
			

		}

	cout << sum << endl;

}


Mysterious gift of C-TT

Description

Given array cat [i] a number N, and generate a new array ans [i] with the array. It is defined as the new array for any i, j and i = j, are ans [] = abs (cat [i] - cat [j])!, 1 <= i <j <= N. This new array. Determine the median, then the median is the sort (len + 1) / 2 corresponding to the position number, '/' is a rounding.

Plural sets of inputs, each input one N, the number N expressed, after the input of a sequence of length N cat, cat [i] <= 1e9, 3 <= n <= 1e5. Output new array ans median

Sample

input:
4
1 3 2 4
3
1 10 2

output:
1
8

Idea

Calculation range of the original array are sorted first new array element values, in this range find a binary answer P, P is the number of bits for each P in P calculates new ranking sequence.
Because the number of columns of the original has been sorted, the absolute value can go to give Xj <= Xi + P, subscript i is then calculated enumerate the number of the index j satisfying the condition calculated here is also possible to use a binary search to find the first is greater than or equal to Xi + P number of positions, and the final sum of all j satisfying the condition, i.e. the qualifying tuples of numbers, and comparing the median number of ranking, the ranking is less than the median is the median right side, if the median is greater than median rank on the left, the new array element corresponding update range lookup value and then, if the two numbers are equal, P is the median.

Suppose the number 4, respectively, 1,3,2,4
-half answer as follows:
Here Insert Picture Description

Summary

P binary answer, enumeration calculation of P i + two ranking
since the storage elements after beginning from 0, the median is the sort code (len + 1) / 2-1 corresponding to the position numbers. This question for a given key is how to determine a number which is not the median, instead of the new number of columns for the calculated median of its elements must belong to a new series of [0, cat_max-cat_min]. How to calculate the number of tuples in thinking in the relatively long.

Codes

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

int N;
int cat[100010];


int find(int x) {
	int l = 0, r = N - 1, ans = N;
	while (l <= r) {
		int mid = l + r >> 1;
		if (cat[mid] >= x) {
			ans = mid;
			r = mid - 1;
		}
		else if (cat[mid] > x)r = mid - 1;
		else l = mid + 1;
	}
	return ans;
}


int main()
{
	while (scanf("%d",&N)!=EOF) {
		for (int i = 0; i < N; i++)
			 scanf("%d",&cat[i]);
		sort(cat, cat + N);

		int min = 0, max = cat[N - 1] - cat[0],P;
		int index = (N*(N - 1) / 2 + 1) / 2-1;
		//二分答案
		while (min < max) {
			P = (min + max) >> 1;
			int pos = 0;		
			for (int i = 0; i < N; i++)
				pos += N - find(cat[i] + P);		//枚举i,计算满足条件的j的个数
			if (pos >= N*(N - 1) / 2 - index)min = P + 1;
			else max = P;
		}
		printf("%d\n", min - 1);
	}
}


Published 21 original articles · won praise 5 · Views 784

Guess you like

Origin blog.csdn.net/weixin_44578615/article/details/104846940