Atcoder 056 D 贪心+二分

D - No Need


Time limit : 2sec / Memory limit : 256MB

Score : 600 points

Problem Statement

AtCoDeer the deer has N cards with positive integers written on them. The number on the i-th card (1iN) is ai. Because he loves big numbers, he calls a subset of the cards good when the sum of the numbers written on the cards in the subset, is K or greater.

Then, for each card i, he judges whether it is unnecessary or not, as follows:

  • If, for any good subset of the cards containing card i, the set that can be obtained by eliminating card i from the subset is also good, card i is unnecessary.
  • Otherwise, card i is NOT unnecessary.

Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary.

Constraints

  • All input values are integers.
  • 1N5000
  • 1K5000
  • 1ai109(1iN)

Partial Score

  • 300 points will be awarded for passing the test set satisfying N,K400.

Input

The input is given from Standard Input in the following format:

N K
a1 a2 ... aN

Output

Print the number of the unnecessary cards.


Sample Input 1

Copy
3 6
1 4 3

Sample Output 1

Copy
1

There are two good sets: {2,3} and {1,2,3}.

Card 1 is only contained in {1,2,3}, and this set without card 1, {2,3}, is also good. Thus, card 1 is unnecessary.

For card 2, a good set {2,3} without card 2, {3}, is not good. Thus, card 2 is NOT unnecessary.

Neither is card 3 for a similar reason, hence the answer is 1.


Sample Input 2

Copy
5 400
3 1 4 1 5

Sample Output 2

Copy
5

In this case, there is no good set. Therefore, all the cards are unnecessary.


Sample Input 3

Copy
6 20
10 4 3 10 25 2

Sample Output 3

Copy
3
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>

using namespace std;

int s[5050];

bool cmp(int a,int b)
{
	return a<b;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n,k;
	scanf("%d%d",&n,&k);
	int num=n,temp,ans=1;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&temp);
		if(temp>=k) num--;
		else s[ans++]=temp;
	}
	ans--;
	sort(s+1,s+num+1,cmp);
	temp=0;
	while(num>=2)
	{
		temp+=s[num];
		if(s[num-1]+temp<k)
		{
			num--;
			continue;
		}
		if(s[1]+temp>=k)
		{
			ans=0;
			break;
		}
		int l=1,r=num-2;
		while(l+1<r)
		{
			int mid=(l+r)/2;
			if(temp+s[mid]>=k) r=mid;
			else l=mid;
		}
		ans=ans-(num-r);
		num=r;
	}
	printf("%d\n",ans);
	return 0;
}






<think>我们使用贪心策略:希望上升子序列尽可能长,那么就要让序列上升得尽可能慢,即每次在子序列末尾添加的元素尽可能小。 维护一个数组 `d`,其中 `d[i]` 表示所有长度为 `i` 的上升子序列中末尾元素的最小值。注意:`d` 数组是严格递增的(因为如果长度为 `i` 的上升子序列的最小末尾比长度为 `i+1` 的还大,那么长度为 `i+1` 的那个子序列的前 `i` 个元素构成的子序列的末尾就会小于 `d[i]`,与定义矛盾)。 算法步骤: 1. 初始化:令 `d[1] = nums[0]`,长度 `len = 1`。 2. 遍历数组 `nums`(从第二个元素开始): - 如果当前元素 `nums[i]` 大于 `d[len]`,则直接加入 `d` 数组末尾,并且 `len++`。 - 否则,在 `d[1..len]` 中二分查找第一个大于等于 `nums[i]` 的位置 `pos`,并更新 `d[pos] = nums[i]`(因为我们要让长度为 `pos` 的上升子序列的末尾元素尽可能小,所以用更小的 `nums[i]` 替换掉原来的 `d[pos]`)。 注意:这里二分查找的是第一个大于等于 `nums[i]` 的位置(因为我们希望替换掉第一个比 `nums[i]` 大的元素,这样替换后序列仍然有序,且我们让这个位置的末尾元素变小了,为后续提供更多可能)。 最终 `len` 就是最长上升子序列的长度。 注意:这个算法只能得到最长上升子序列的长度,如果要还原出具体的序列,需要另外记录路径(但这里我们只求长度)。 为什么正确? 我们维护的 `d` 数组并不一定是真实存在的子序列,但是它的长度就是最长上升子序列的长度。因为每次我们扩展长度时(即遇到比末尾大的元素),就相当于在原来的某个上升子序列后面添加了一个元素,使得长度加一。而当我们替换某个位置的值时,我们并没有改变当前的长度,但是让这个位置的末尾元素更小了,这样在后续的遍历中,我们就有可能构造出更长的子序列。 时间复杂度:O(n log n) 下面给出C++实现:</think>### LIS贪心算法与二分查找实现详解 贪心+二分查找是求解最长上升子序列(LIS)问题的最优解法,时间复杂度为 $O(n \log n)$,空间复杂度为 $O(n)$。其核心思想是**维护一个最小末尾元素序列**,使序列尽可能缓慢增长。 #### 算法核心思想 1. **贪心策略**:让上升子序列增长尽可能慢,即每次添加的元素尽可能小 2. **状态定义**:`d[i]` 表示长度为 `i` 的上升子序列的最小末尾元素 3. **关键性质**:`d` 数组严格递增(证明:若 $d[i] \geq d[i+1]$,则 $d[i+1]$ 对应的序列前 `i` 个元素构成的子序列末尾元素 $< d[i]$,矛盾) 4. **二分优化**:利用 `d` 的有序性,通过二分查找确定插入位置 #### 算法流程 1. 初始化 `d[1] = nums[0]`, `len = 1` 2. 遍历数组元素: - 若 `nums[i] > d[len]`:扩展序列,`d[++len] = nums[i]` - 否则:二分查找 `d[1..len]` 中第一个 $\geq$ `nums[i]` 的位置 `pos`,更新 `d[pos] = nums[i]` 3. 最终 `len` 即为 LIS 长度 #### C++ 实现代码 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int lengthOfLIS(vector<int>& nums) { if (nums.empty()) return 0; int n = nums.size(); vector<int> d(n + 1, 0); // d数组,d[0]不使用 int len = 1; d[1] = nums[0]; for (int i = 1; i < n; i++) { if (nums[i] > d[len]) { // 扩展序列 d[++len] = nums[i]; } else { // 二分查找插入位置 int left = 1, right = len; while (left <= right) { int mid = left + (right - left) / 2; if (d[mid] < nums[i]) { left = mid + 1; } else { right = mid - 1; } } // 更新d数组 d[left] = nums[i]; } } return len; } int main() { vector<int> nums = {10, 9, 2, 5, 3, 7, 101, 18}; cout << "最长上升子序列长度: " << lengthOfLIS(nums) << endl; // 输出: 4 return 0; } ``` #### 关键步骤解析 1. **初始化**:`d[1] = nums[0]`,初始长度为1 2. **遍历处理**: - 当前元素 `7`:大于 `d[3]=5`,扩展序列:`d[4]=7`,`len=4` - 当前元素 `3`:小于 `d[3]=5`,二分查找位置2(`d[2]=3`),更新 `d[2]=3` 3. **二分查找**:在有序数组 `d[1..len]` 中定位第一个 $\geq$ 当前元素的位置 #### 复杂度分析 - **时间复杂度**:$O(n \log n)$ - 遍历 $n$ 个元素,每次二分查找 $O(\log n)$ - **空间复杂度**:$O(n)$(存储 `d` 数组) #### 算法正确性证明 - **维护最小末尾**:每次更新确保 `d[i]` 是长度为 `i` 的 LIS 的最小可能末尾 - **单调性保证**:$d[1] < d[2] < \cdots < d[len]$ 严格成立 - **长度最优**:最终 `len` 对应真实 LIS 长度(可通过数学归纳法证明) #### 扩展应用 1. **输出具体序列**:额外记录路径数组 `path`,回溯时从 `d[len]` 开始反向追踪[^1] 2. **非严格递增**:修改比较条件为 `nums[i] >= d[len]` 和 `d[mid] <= nums[i]` 3. **二维LIS问题**:如俄罗斯套娃问题(先按宽度排序,高度求LIS)[^5]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值