「程序設計思維與實踐」Week4 作業:A - DDL 的恐懼、B - 四個數列、C - TT 的神祕禮物。貪心+二分+答案二分總結

A - DDL 的恐懼

題目描述

ZJM 有 n 個作業,每個作業都有自己的 DDL,如果 ZJM 沒有在 DDL 前做完這個作業,那麼老師會扣掉這個作業的全部平時分。
所以 ZJM 想知道如何安排做作業的順序,才能儘可能少扣一點分。
請你幫幫他吧!

input

輸入包含T個測試用例。輸入的第一行是單個整數T,爲測試用例的數量。
每個測試用例以一個正整數N開頭(1<=N<=1000),表示作業的數量。
然後兩行。第一行包含N個整數,表示DDL,下一行包含N個整數,表示扣的分。

Output

對於每個測試用例,您應該輸出最小的總降低分數,每個測試用例一行。

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

Sample Output

0
3
5

Hint

上方有三組樣例。
對於第一組樣例,有三個作業它們的DDL均爲第三天,ZJM每天做一個正好在DDL前全部做完,所以沒有扣分,輸出0。
對於第二組樣例,有三個作業,它們的DDL分別爲第一天,第三天、第一天。ZJM在第一天做了第一個作業,第二天做了第二個作業,共扣了3分,輸出3。

題解

  • easy 版
    • 使用貪心策略:1.儘量選價值高的任務;2.安排任務時儘量安排的靠後。於是對於n^2的做法,我們先按照大小順序給任務排序,然後從大到小依次取出任務,對於該任務可以安排的時間,從ddl開始往前遍歷,如果可以安排,那就放在這個地方,保證合法的情況下最晚。
  • hard 版
    • 貪心策略:不再根據任務找時間,而是根據時間找合適的最有價值的任務。對於nlogn的做法,我們從最後一個時間開始一次往前遍歷,對於遍歷到時間t,將所有滿足ddl=t的任務按照價值排序加到優先隊列中,維護一個最大值,這樣每次取出價值最大的放到當前時間t,直到最後時間遍歷完成。

代碼 easy版

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 1010;
struct DDL {
	int d;
	int a;
}ddls[MAXN];

bool myday[MAXN];

bool cmp(const DDL x, const DDL y) {
	if(x.a != y.a) return x.a < y.a;
	return x.d < y.d;
}

int main() {
	int T;
	cin >> T;
	while (T --) {
		int N;
		cin >> N;
		for(int i = 0; i <= N; ++i) ddls[i].a = ddls[i].d = 0;
		memset(myday, 0, sizeof(myday));
		for (int i = 1; i <= N; ++i) {
			scanf("%d", &ddls[i].d);
		}
		for (int i = 1; i <= N; ++i) {
			scanf("%d", &ddls[i].a);
			ddls[0].a += ddls[i].a;
		}
		sort(ddls+1,ddls+N+1,cmp);
		int sum = 0;
		for (int i = N; i >= 1; i--) {
			for (int j = ddls[i].d; j >= 1; j--) {
				if (!myday[j]) {
					myday[j] = true;
					sum += ddls[i].a;
					break;
				}
			}
		}
		cout << ddls[0].a-sum << endl;

	}
	return 0;
}

代碼 hard版

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int MAXN = 1010;
struct DDL {
	int d;
	int a;
}ddls[MAXN];

bool cmp(const DDL x, const DDL y) {
	return x.d > y.d;
}

priority_queue<int> q;

int main() {
	int T;
	cin >> T;
	while (T --) {
		int N;
		cin >> N;
		for(int i = 0; i <= N; ++i) ddls[i].a = ddls[i].d = 0;
		while(q.size()) q.pop();
		for (int i = 1; i <= N; ++i) {
			scanf("%d", &ddls[i].d);
			ddls[0].d = max(ddls[0].d,ddls[i].d);
		}
		for (int i = 1; i <= N; ++i) {
			scanf("%d", &ddls[i].a);
			ddls[0].a += ddls[i].a;
		}
		sort(ddls+1,ddls+N+1,cmp);
		int sum = 0, nowid = 1;
		for(int t = ddls[0].d; t >= 1; t--) {
			while (ddls[nowid].d == t && nowid <= N) {
				q.push(ddls[nowid].a);
				nowid++;
			}
			if (q.size()) {
				sum += q.top();
				q.pop();
			}
		}
		cout << ddls[0].a-sum << endl;

	}
	return 0;
}

B - 四個數列

題目描述

ZJM 有四個數列 A,B,C,D,每個數列都有 n 個數字。ZJM 從每個數列中各取出一個數,他想知道有多少種方案使得 4 個數的和爲 0。
當一個數列中有多個相同的數字的時候,把它們當做不同的數對待。

input

第一行:n(代表數列中數字的個數) (1≤n≤4000)
接下來的 n 行中,第 i 行有四個數字,分別表示數列 A,B,C,D 中的第 i 個數字(數字不超過 2 的 28 次方)

Output

輸出不同組合的個數。

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

Sample Output

5

題解

  • 對於 a[i]+b[i]+c[i]+d[i] = 0,變換形式得到a[i]+b[i] = -(c[i]+d[i]),即其中兩組數的和等於另外兩組數的相反數,我們可以分別預處理出abcd的和,然後給ab組排序,這樣在對於每一個cd[j],我們都能在ab組中二分的查找符合條件的值的個數。時間複雜度爲n^2logn.
  • 在二分查找時,先找到符合條件的最左端和最右端,然後r-l+1便是個數。

代碼

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int MAXN = 4000+10;
int tot = 0;
int a[MAXN], b[MAXN], c[MAXN], d[MAXN];
int ab[MAXN*MAXN];
int cd[MAXN*MAXN];

int find_l(int x) {
	x = -x;
	int l = 1, r = tot; 
	int mid = (l+r)>>1;
	int res = -1;
	while (l <= r) {
		mid = (l+r)>>1;
		if (x == ab[mid]) {
			res = mid;
			r = mid-1;
		} else if (x < ab[mid]) {
			r = mid-1;
		} else if (x > ab[mid]) {
			l = mid+1;
		}
	}
	return res;
}

int find_r(int x) {
	x = -x;
	int l = 1, r = tot; 
	int mid = (l+r)>>1;
	int res = -1;
	while (l <= r) {
		mid = (l+r)>>1;
		if (x == ab[mid]) {
			res = mid;
			l = mid+1;
		} else if (x < ab[mid]) {
			r = mid-1;
		} else if (x > ab[mid]) {
			l = mid+1;
		}
	}
	return res;
}

int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
	}
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= n; ++j) {
			ab[++tot] = a[i]+b[j];
			cd[tot] = c[i]+d[j];
		}
	}
	int ans = 0;
	sort(ab+1,ab+tot+1);
	for(int i = 1; i <= tot; ++i) {
		// cout << ab[i] << "      " << cd[i] << endl;
		int l = find_l(cd[i]);
		int r = find_r(cd[i]);
		// cout << l << " and " << r << endl;
		if(l == -1 || r == -1) continue;
		ans += r-l+1;
	}
	cout << ans << endl;

	return 0;
}

C - TT 的神祕禮物

題目描述

TT 是一位重度愛貓人士,每日沉溺於 B 站上的貓咪頻道。
有一天,TT 的好友 ZJM 決定交給 TT 一個難題,如果 TT 能夠解決這個難題,ZJM 就會買一隻可愛貓咪送給 TT。
任務內容是,給定一個 N 個數的數組 cat[i],並用這個數組生成一個新數組 ans[i]。新數組定義爲對於任意的 i, j 且 i != j,均有 ans[] = abs(cat[i] - cat[j]),1 <= i < j <= N。試求出這個新數組的中位數,中位數即爲排序之後 (len+1)/2 位置對應的數字,’/’ 爲下取整。

TT 非常想得到那隻可愛的貓咪,你能幫幫他嗎?

input

多組輸入,每次輸入一個 N,表示有 N 個數,之後輸入一個長度爲 N 的序列 cat, cat[i] <= 1e9 , 3 <= n <= 1e5

Output

輸出新數組 ans 的中位數

Sample Input

4
1 3 2 4
3
1 10 2

Sample Output

1
8

題解

  • 思路:先考慮暴力求解的複雜度,互相求差的絕對值,時間複雜度是 O(n),會超時不可取。令兩兩相減生成的數組叫ans。我們觀察中位數的特性,發現只要我們將原數組從小到大排序,然後按照順序加減,我們就能得到對於某個數x來說ans數組裏小於x的有多少個元素,而又因爲ans數組有序,所以可以二分解決,複雜度可以爲O(log(1e6)*n*logn)
  • 實現細節:先二分答案,下屆爲0,上屆爲1e6,因爲最大的數才1e5肯定夠用。然後對於每一個mid進行check:計算這個在ans數組裏,找出嚴格小於 mid的的數字個數有k個,令m爲我們ans數組中的中位數個數,如果k < m,即嚴格小於 mid 的的數字小於中位數應有的位數,表示mid剛好在中位數位置或者在中位數位置之前,偏小,那麼返回1,在外層二分中令 左區間等於mid+1,朝右擴展。反之,朝左擴展。
  • 二分邊界問題:寫的時候發現這個地方是自己知識的一個漏洞,之前寫的時候非常麻煩的判斷邊界問題,這次參照了市面上的所有二分寫法,最終選擇如 https://www.zhihu.com/question/36132386 所示的二分問題寫法:即l,r分別用來逼近答案和縮小區間。在這個題中,如果不滿足條件,那麼就l=mid+1縮小區間,滿足的話r=mid來逼近。最終符合條件的是mid 或 r特別注意的一點:這時計算mid的時候要l + (r-l+1)/2否則會因爲mid一直==l,而又check==1所以死循環。
  • 類似的二分還可以有另外一種寫法:用l逼近區間,r來縮小區間,但是這時候注意是左閉右開的,就是那mid正常寫就可了。然後結果l-1的原因:觀察最後小區間的狀態可以得知:當我們檢查到midmid是中位數時,因爲mid前面的嚴格小於的元素個數是小於mid所在的位置的,所以會執行l = mid+1,這一句話,所以最終的狀態是 l-1 爲最後我們檢查合法的元素。
  • 參考
    • https://www.zhihu.com/question/36132386
    • https://blog.csdn.net/OCEANtroye/article/details/82811432
    • https://blog.csdn.net/yefengzhichen/article/details/52372407?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

代碼1

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;

const int MAXN = 1e5+10;
int n, nums;
int cat[MAXN];
// 0 1 2 3 4 5 6 7 8 9
// 2 3 6 7 9 9 9 9 9 9
// // // / / / / // / /
// 1 2 3 4 5 6 7 8 9 10
// 2 3 6 7 9 9 9 9 9 9

bool check(int x) {
	// 目的:找到在cat數組裏,兩數之差小於x的個數,即找滿足 a[j]-a[i] < x 的個數,個數多說明x過大,個數少說明x小
	// 也就是對於每一個i來說,要找到剛好滿足條件的位置,j~n全是可以的。 a[j] < a[i]+x;
	// cout << " - - - - - - - - - x = " << x << endl;
	int sum = 0;
	for (int i = 0; i < n; ++i) {
		// cout << "t = " << cat[i]+x << endl;
		// cout << "juedui:" << lower_bound(cat+i+1, cat+n, cat[i]+x)-(cat) << ",posi = " << lower_bound(cat+i+1, cat+n, cat[i]+x)-(cat+i+1) << endl;
		int posi = lower_bound(cat+i+1, cat+n, cat[i]+x)-(cat+i+1); //在他前面的數的個數
		sum += posi;
		// cout << posi << ",";
	}
	// cout << endl;
	// cout << "sum = " << sum << endl; 
	return sum < nums;
}

int main() {
	while (cin >> n) {
	 	nums = n*(n-1)/2;
	 	nums = (nums+1)/2; // 中位數的位置
	 	// cout << "mid:" << nums << endl;
		memset(cat, 0, sizeof(cat));
		for (int i = 0; i < n; ++i) {
			scanf("%d", &cat[i]);
		}
		sort(cat,cat+n);	
		int l = 0, r = 1e6;
		int mid = l+r >> 1;
		int ans = 0;
		while (l < r) {
			mid = l + (r-l+1)/2;
			// cout << l << ", " << mid << ", " << r << endl;
			// cout << "now mid = " << mid << endl;
			// 嚴格小於的個數小於真正中位數的位置
			if (check(mid)) { // 如果這個數前面數字達不到中位數個數的情況// 前面如果有小於等於 nums 個數。這個數一定不行
				l = mid;
			} else {// 這個數字可能彳亍
				r = mid-1;
			}
		}
		// l-1的原因:觀察最後小區間的狀態可以得知:當我們檢查到mid,mid是中位數時,因爲mid前面的嚴格小於的元素個數是小於mid所在的位置的,
		// 所以會執行 l = mid+1,這一句話,所以最終的狀態是 l-1 爲最後我們檢查合法的元素。
		printf("%d\n",l);
	} 

	return 0;
}

代碼2

		while (l < r) {
			mid = l + (r-l)/2;
			// cout << l << ", " << r << "mid = " << mid << endl;
			// cout << "now mid = " << mid << endl;
			if (check(mid)) { // 如果這個數前面數字達不到中位數個數的情況// 前面如果有小於等於 nums 個數。這個數一定不行
				l = mid+1;
			} else {// 這個數字可能彳亍
				r = mid;
			}
		}
		printf("%d\n",l-1);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章