【嚼之无味】toj1469 toj2867 toj3095

Wooden Sticks

是一个贪心。题意:给n组数(li, wi)代表n种神马的两个属性,生一只神马需要1个单位时间,但是如果生出了(l, w),那么可以迅速(不消耗时间)地生一只(l'>l, w' > w)。

开始理解错题意了(๑´ㅂ`๑)...

显然我们只需要拍一遍序然后扫描一遍即可:对没有标记的做标记,然后依次往后进行一次子扫描过程。

简单证明:排序后(假设我们按照l优先排序),l是不需要考虑的,因为后面的l一定大于等于前面的。

然后我们假设有w1 <= w2 <= w3.

那么对于w2来说,如果搞完w1后不搞它,那么如果它在搞完另一只w1'后搞,则对于w2以后的序列搞出的结果不会优于在搞完w1后直接搞w2得到的结果。其次,如果w2单独作为一个起点开始搞,解一定更差,和上述一样,搞完w2后,不管前面情况如何,后面都将面临相同的序列。

因此上述贪心过程是正确的,可以得到最优解。


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

struct stk {
	int x, y;
	bool operator<(const stk& b)const {
		return x == b.x ? y < b.y : x < b.x;
	}
} a[5001];
bool vis[5001];

int main() {
	int T, n;
	scanf(" %d", &T);
	while (T--) {
		scanf(" %d", &n);
		for (int i = 0; i < n; ++i) {
			scanf(" %d %d", &a[i].x, &a[i].y);
		}
		sort(a, a + n);
		memset(vis, false, sizeof(vis));
		int sum = 0, y;
		for (int i = 0; i < n; ++i) {
			if (!vis[i]) {
				vis[i] = true;
				++sum;
				y = a[i].y;
				for (int j = i + 1; j < n; ++j) {
					if (a[j].y >= y && !vis[j]) {
						y = a[j].y;
						vis[j] = true;
					}
				}
			}
		}
		printf("%d\n", sum);
	}
	return 0;
}

Picking Problem

题意:n件事情的时间区间[l,r)求问最多可以做多少件事。对于[l1,r1), [l2, r2),我们认为如果l2 >= r1那么这两件事是可以都被做掉的。

依然是贪心。对于任意两件事T1: [l1,r1), T2: [l2,r2),如果r1 <= r2,那么显然如果两件事都可以被做的话,做T1更有优势,因为做T2后可以做的事做完T1也都可以做。其次,有件事不能做和两件都不能做的情况我们不需要额外考虑,因为那是我们贪心的前提,即只有两件事都能做时才会有选择的区别,亦即解的区别。

我们按照r优先排序,扫描一遍即可。

#include <cstdio>
#include <cstring>
#include <algorithm>

struct node {
	int l, r;
	bool operator<(const node& b)const {
		return r == b.r ? l < b.l : r < b.r;
	}
} a[10007];

inline int read() {
	char ch;
	while ((ch = getchar()) < '0' || ch > '9');
	int x = ch - '0';
	while ((ch = getchar()) >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
	}
	return x;
}

int main() {
	int n;
	while (~scanf(" %d", &n) && n) {
		for (int i = 0; i < n; ++i) {
			a[i].l = read();
			a[i].r = read();
			//scanf(" %d %d", &a[i].l, &a[i].r);
			a[i].r += a[i].l;
		}
		std::sort(a, a + n);
		int p = a[0].r, c = 1;
		for (int i = 1; i < n; ++i) {
			if (a[i].l >= p) {
				p = a[i].r;
				++c;
			}
		}
		printf("%d\n", c);
	}
	return 0;
}


 Best Compression Ever

题意:问你b个bit(比特)位(即基于二进制的b位数)能不能表示出n来。

对于1 0这种数据有点无法理解,,,但是AC还是没有问题的。b位二进制可以表示的最大数就是11111...1111(b个1)=1*2^0 + 1*2^1 + ... + 1*2^b=∑2^i(i从0到b)=2^(b+1)-1

只需要判断是否n <= 2^(b+1)-1,即n < 2^(b+1)即可,哪来的这种问题...好无聊...

#include <cstdio>
int main() {
	long long n, b;
	scanf(" %lld %lld", &n, &b);
	puts(n < 1LL << (b + 1) ? "yes" : "no");
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值