【算法1-5】贪心

P2240 【深基12.例1】部分背包问题

题目链接:P2240 【深基12.例1】部分背包问题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 

#include 
#include 
#include 
#include 
using namespace std;

class Thing {
	public:
		double avg;//平均价格
		int num;//总数
		int sum;//总价
};

class Com {
	public:
		bool operator()(Thing t1, Thing t2) {
			return t1.avg > t2.avg;
		}
};

int main() {
	int n, t, x = 0;
	double ans = 0;
	cin >> n >> t;
	vectorv(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i].num >> v[i].sum;
		v[i].avg = (double)v[i].sum / v[i]. num;
	}
	sort(v.begin(), v.end(), Com());
	int cnt = 0;
	while (x != t) {
		if (t - x >= v[cnt].num) {
			x += v[cnt].num;
			ans += v[cnt].sum;
		} else {
			ans += (t - x) * v[cnt].avg;
			x = t;
		}
		cnt++;
	}
	cout << setprecision(2) << fixed << ans;
	return 0;
}

 P1223 排队接水

题目链接:P1223 排队接水 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include 
#include 
#include 
#include 
using namespace std;

class Person {
	public:
		int num;
		int time;
};

class Com {
	public:
		bool operator()(Person p1, Person p2) {
			return p1.time < p2.time;
		}
};

int main() {
	int n;
	cin >> n;
	vectorv(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i].time;
		v[i].num = i + 1;
	}
	sort(v.begin(), v.end(), Com());
	double cnt = 0, sum = 0;
	for (int i = 0; i < n - 1; i++) {
		cnt += v[i].time;
		sum += cnt;
	}
	for (int i = 0; i < n; i++) {
		cout << v[i].num << ' ';
	}
	cout << endl;
	cout << setprecision(2) << fixed << sum / n;
	return 0;
}

P1803 凌乱的yyy / 线段覆盖

题目链接: P1803 凌乱的yyy / 线段覆盖 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include 
#include 
#include 
#include 
using namespace std;
int flag[1000010];

class Contest {
	public:
		int start;
		int endd;
		int time;
};

class Com {
	public:
		bool operator()(Contest c1, Contest c2) {
			return c1.endd == c2.endd ? (c1.time < c2.time) : c1.endd < c2.endd;
		}
};

int main() {
	int n, cnt = 0;
	cin >> n;
	vectorv(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i].start >> v[i].endd;
		v[i].time = v[i].endd - v[i].start;
	}
	sort(v.begin(), v.end(), Com());
	int now = 0;
	for (int i = 0; i < n; i++) {
		bool f = 0;
		for (int j = v[i].start; j < v[i].endd; j++) {
			if (flag[j] == 0) {
				flag[j] = 1;
			} else {
				for (int k = v[i].start; k < j; k++) {
					flag[k] = 0;
				}
				f = 1;
				break;
			}
		}
		if (f == 1) {
			continue;
		} else {
			cnt++;
			int j = i + 1;
			while (v[j].start == v[i].start) {
				j++;
			}
			i = j - 1;
		}
	}
	cout << cnt;
}

P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G

题目链接:P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include 
#include 
#include 
using namespace std;
priority_queue, greater>q;

int main() {
	int n, x, ans = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> x;
		q.push(x);
	}
	while (q.size() > 1) {
		int a = q.top();
		q.pop();
		int b = q.top();
		q.pop();
		ans += a + b;
		q.push(a + b);
	}
	cout << ans;
	return 0;
}

 优先队列:【原创】堆排序+合并果子+优先队列_致上-CSDN博客

【原创】优先队列 priority_queue 详解_致上-CSDN博客_优先队列

P3817 小A的糖果 

题目链接:P3817 小A的糖果 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include 
using namespace std;
long long a[100010];

int main() {
	long long n, x, cnt = 0;
	cin >> n >> x;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	if (a[0] > x) {
		cnt += a[0] - x;
		a[0] = x;
	}
	for (int i = 1; i < n; i++) {
		if (a[i] + a[i - 1] > x) {
			cnt += a[i] + a[i - 1] - x;
			a[i] = x - a[i - 1];
		}
	}
	cout << cnt;
	return 0;
}

 P1106 删数问题

题目链接:P1106 删数问题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include 
#include 
using namespace std;

int main() {
	int k, len;
	string s;
	cin >> s >> k;
	len = s.size();
	while (k--) {
		for (int i = 1; i < s.size(); i++) {
			if (s[i - 1] > s[i]) {
				s.erase(i - 1, 1);
				break;
			}
		}
		len--;
	}
	bool flag = 0;
	int index = 0;
	for (int i = 0; i < len; i++) {
		if (s[i] != '0') {
			flag = 1;
			index = i;
			break;
		}
	}
	if (flag == 0) {
		cout << 0;
	} else {
		for (int i = index; i < len; i++) {
			cout << s[i];
		}
	}
	return 0;
}

 参考:C++删除string最后一个字符的几种方法_沉默的时光-CSDN博客_c++ string删除最后一个字符

P1478 陶陶摘苹果(升级版) 

题目链接:P1478 陶陶摘苹果(升级版) - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include 
#include 
#include 
using namespace std;

class Apple {
	public:
		int x;
		int y;
};

class Com {
	public:
		bool operator()(Apple a1, Apple a2) {
			return a1.y < a2.y;
		}
};

int main() {
	int n, s, a, b;
	cin >> n >> s >> a
	    >> b;
	vectorv;
	for (int i = 0; i < n; i++) {
		Apple p;
		cin >> p.x >> p.y ;
		if (p.x <= a + b) {
			v.push_back(p);
		}
	}
	sort(v.begin(), v.end(), Com());
	int cnt = 0, sum = 0;
	for (int i = 0; i < v.size(); i++) {
		if (sum + v[i].y <= s) {
			cnt++;
			sum += v[i].y;
		} else {
			break;
		}
	}
	cout << cnt;
	return 0;
}

 P5019 [NOIP2018 提高组] 铺设道路

题目链接:P5019 [NOIP2018 提高组] 铺设道路 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 

#include 
using namespace std;
int a[100010];

int main() {
	int n, ans = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 1; i < n; i++) {
		if (a[i] > a[i - 1]) {
			ans += a[i] - a[i - 1];
		}
	}
	cout << a[0] + ans;
	return 0;
}

P1208 [USACO1.3]混合牛奶 Mixing Milk

题目链接:P1208 [USACO1.3]混合牛奶 Mixing Milk - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 

#include 
#include 
#include 
using namespace std;

class Milk {
	public:
		int sum;
		int avg;
};

class Com {
	public:
		bool operator()(Milk m1, Milk m2) {
			return m1.avg < m2.avg;
		}
};

int main() {
	int n, m, k = 0, ans = 0;
	cin >> n >> m;
	vectorv(m);
	for (int i = 0; i < m; i++) {
		cin >> v[i].avg >> v[i].sum;
	}
	sort(v.begin(), v.end(), Com());
	for (int i = 0; i < m; i++) {
		if (k < n) {
			if (n - k >= v[i].sum) {
				k += v[i].sum;
				ans += v[i].sum * v[i].avg;
			} else {
				ans += v[i].avg * (n - k);
				k = n;
			}
		} else {
			break;
		}
	}
	cout << ans;
	return 0;
}

P1094 [NOIP2007 普及组] 纪念品分组

题目链接:P1094 [NOIP2007 普及组] 纪念品分组 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 

#include 
#include 
using namespace std;
int a[30010];

int main() {
	int n, w, cnt = 0;
	cin >> w >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort(a, a + n);
	int i = 0, j = n - 1;
	while (i < j) {
		if (a[i] + a[j] <= w) {
			cnt++;
			i++;
			j--;
		} else {
			cnt++;
			j--;
		}
		if (i == j) {
			cnt++;
		}
	}
	cout << cnt;
	return 0;
}

P4995 跳跳!

题目链接:P4995 跳跳! - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 

#include 
#include 
using namespace std;
long long a[310];

int main() {
	long long n, ans = 0, t = 0;
	cin >> n;
	for (long long i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort(a, a + n);
	if (n == 1) {
		ans = a[0] * a[0];
	} else {
		long long i = 0, j = n - 1;
		while (i < j) {
			ans += (a[j] - t) * (a[j] - t);
			t = a[j];
			j--;
			ans += (a[i] - t) * (a[i] - t);
			t = a[i];
			i++;
			if (i == j) {
				ans += (a[i] - t) * (a[i] - t);
			}
		}
	}
	cout << ans;
	return 0;
}

 P4447 [AHOI2018初中组]分组

题目链接:P4447 [AHOI2018初中组]分组 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include 
#include 
using namespace std;
int a[100010];

int sum[100010];//每组元素个数
int last[100010];//每组最大值
int cnt;//已经分好的组数
int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort(a, a + n);
	//初始化
	cnt = 1;
	sum[0] = 1;
	last[0] = a[0];
	for (int i = 1; i < n; i++) {
		bool flag = 0;
		int sh = n + 1; //用来寻找最短的组
		int index;
		for (int j = 0; j < cnt; j++) {
			if (a[i] == last[j] + 1 && sh > sum[j]) {
				sh = sum[j];
				index = j;
				flag = 1;
			}
		}
		if (flag == 0) {
			last[cnt] = a[i];
			sum[cnt] = 1;
			cnt++;
		} else {
			sum[index]++;
			last[index] = a[i];
		}
	}
	int minn = n + 1;
	for (int i = 0; i < cnt; i++) {
		if (minn > sum[i]) {
			minn = sum[i];
		}
	}
	cout << minn;
	return 0;
}

P1080 [NOIP2012 提高组] 国王游戏

题目链接:P1080 [NOIP2012 提高组] 国王游戏 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include 
#include 
#include 
#include 
using namespace std;


int maxx[10010] = {1}, sum[10010] = {1}, ans[10010];

int s = 1, m = 1, an = 1; //记录位数
class Minister {
	public:
		int R;
		int L;
};

class Com {
	public:
		bool operator()(Minister m1, Minister m2) {
			return m1.L * m1.R < m2.L * m2.R;
		}
};

//低精度乘高精度
void mult(long long x) {
	int t = 0;
	for (int i = 0; i < s; i++) {
		sum[i] *= x;
	}
	for (int i = 0; i < s; i++) {
		t += sum[i];
		sum[i] = t % 10;
		t /= 10;
	}
	while (t) {
		sum[s] = t % 10;
		t /= 10;
		s++;
	}
}

void divi(long long x) {
	memset(ans, 0, sizeof(ans));
	an = s;
	int t = 0;
	for (int i = an - 1; i >= 0; i--) {
		t *= 10;
		t += sum[i];
		if (t >= x) {
			ans[i] = t / x;
			t %= x;
		}
	}
	while (ans[an - 1] == 0) { //去除前导零
		if (an == 1) { //特判0
			break;
		}
		an--;
	}
}

void max() {
	if (an > m) {
		for (int i = 0; i < an; i++) {
			maxx[i] = ans[i];
		}
		m = an;
	} else if (an == m) {
		for (int i = an - 1; i >= 0; i--) {
			if (maxx[i] > ans[i]) {
				break;
			}
			if (maxx[i] < ans[i]) {
				for (int j = 0; j < an; j++) {
					maxx[j] = ans[j];
				}
				break;
			}
		}
	}
}

int main() {
	int n, a, b;
	cin >> n >> a >> b;
	vectorv(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i].L >> v[i].R;
	}
	sort(v.begin(), v.end(), Com());
	mult(a);
	divi(v[0].R);
	max();
	for (int i = 1; i < n; i++) {
		mult(v[i - 1].L);
		divi(v[i].R);
		max();
	}
	for (int i = m - 1; i >= 0; i--) {
		cout << maxx[i];
	}
	return 0;
}

 

你可能感兴趣的:(洛谷,c++,算法)