5.15周末总结

1 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G - 洛谷

#include<bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int> > a;
int m, n;
int ans=0;
int main() {    
    cin >> n;
    for (int i = 0; i < n;i++) {
        int x;
        cin >> x;
        a.push(x);
    }
    while (a.size()>=2) {
        int i = a.top(); a.pop();
        int j = a.top(); a.pop();
        ans += i + j;
        a.push(i+j);
    }
    cout << ans << endl;
    
    return 0;
}

用优先队列储存数据,每次取出累加并放回,得到最大值

2、第k小

#include<bits/stdc++.h>
using namespace std;
priority_queue<int >q;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, m, k;
	cin >> n >> m >> k;
	for (int i = 0; i < n; i++)
	{
		int tmp;
		cin >> tmp;
		if (q.empty())
		{
			q.push(tmp);
		}
		else
		{
			if (q.size() < k)
				q.push(tmp);
			else
			{
				if (tmp < q.top())
				{
					q.push(tmp);
					q.pop();
				}
				
			}
		}
	}
	for (int i = 0; i < m; i++)
	{
		int input;
		int data;
		cin >> input;
		switch (input)
		{
		case 1:
			cin >> data;
			if (q.empty())
			{
				q.push(data);
			}
			else
			{
				if (q.size() < k)
					q.push(data);
				else
				{
					if (data < q.top())
					{
						q.push(data);
						q.pop();
					}

				}
			}
			break;
		case 2:
			if (q.size() < k)
				cout << -1 << endl;
			else
				cout << q.top() << endl;
			break;

		}
	}

	return 0;
}

这是一个动态的过程,需要不断地执行插入数字和查询的操作。所以可以选择使用优先队列,插入时自动排序,这是一个动态的过程,我们需要不断地执行插入数字和查询的操作。所以可以选择使用优先队列,插入时自动排序。

[JSOI2007] 建筑抢修 - 洛谷

#include<bits/stdc++.h>
#define MAXN 200200
using namespace std;

int n;
int T;
int ans;
struct node
{
    int w;
    int t;
}a[MAXN];
priority_queue<int> Q;//优先队列

bool cmp (node x, node y)
{
    return x.t < y.t;//按t从小到大排序
}
int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        scanf("%d%d", &a[i].w, &a[i].t);
    sort(a + 1, a + n + 1, cmp);
    for(int i = 1; i <= n; i++)
    {
        if(T + a[i].w > a[i].t)
        {
            if(a[i].w < Q.top())//ai < aj
            {
                T -= Q.top();
                Q.pop();
                Q.push(a[i].w);
                T += a[i].w;
            }
        }
        else 
        {
            Q.push(a[i].w);
            ans++;
            T += a[i].w;
        }
    }
    printf("%d\n", ans);
    return 0; 
}

 按 t从小到大排序之后,开始轮流遍历每个建筑。如果中途某个建筑 i 无法在 t_i 的时间内修复,那么在先前选择修复的建筑中拿出 a_j​ 最大的 j号建筑。若 a_i < a_j,则放弃 j 转而修 i

4、CUT

#include<bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int> > quej;//升序队列
//priority_queue <int, vector<int>, less<int> >quej;//降序队列
int m, n,x,q[10010],c;
int  a, b;
long long ans=0,v[10010],s[10010],vm,sm,num;
int main() {	
	//memset(q, 0, sizeof(q));
	int t = 1;
	cin >> n;
	while (n--) {
		cin >> a;
		quej.push(a);
		num += a;
	}
	while (quej.size()>1) {
		b = quej.top();
		//cout << b << endl;
		quej.pop();
		ans += num;
		num -= b;
	}
	cout << ans << endl;
	return 0;
}

 降序优先队列储存数据,每次取出当前最小值,使得每次删去最小数并保证剩余数大,时代最终总代价最大

5、新建 Microsoft Office Word 文档

#include<bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int> > que;//升序队列
int m, n,x,q[100100],c;
string a, b;
int ans=0;
int main() {	
	memset(q, 0, sizeof(q));
	int t = 1;
	cin >> n;
	while (n--) {
		cin >> a;
		if (a == "New") {
			if (que.empty()) {
				cout << t << endl;
				q[t] = 1;
				t++;
			}
			else {
				int p = que.top();
				que.pop();
				cout << p << endl;
				q[p] = 1;
			}
		}
		else
		{
			cin >> c;
			if (q[c]) {
				que.push(c);
				q[c] = 0;
				cout << "Successful" << endl;
			}
			else
				cout << "Failed" << endl;
		}
	}
	return 0;
}

刚开始用数组存储,删去的文档在数组中标记,这样会导致每次添加新文档时都需要从头遍历,时间复杂度大,易超时;后来改成优先队列存储,由于其能够自行排序,每次操作只需在队列队尾进行增删操作,时间复杂度为O(N)。

6、加边无向图

#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int p[N];

int find(int x) {
	if (x != p[x])p[x] = find(p[x]);
	return p[x];
}

int main() {
	set<int>s;
	int n, m, i, j;
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		p[i] = i;
	for (int k = 1; k <= n; k++) {
		cin >> i >> j;
		int ii = find(i), jj = find(j);
		if (ii != jj) {
			p[ii] = jj;
		}
	}
	for (int i = 1; i <= n; i++) {
		s.insert(find(i));
	}
	cout << s.size() - 1 << endl;
}

这个周主要复习了并查集以及拓补排序的知识,牛客网的队列并查集题目巩固了一遍知识,问题还是做题量不够,平时的学习积极性还有很大提升空间

这个学期以来acm课程对我最深的影响就是对我平时学习习惯的改变,让我有了主动学习的意识,自己在空余时间也会主动去做题,去练习,去钻研自己的疑难杂题,去主动探索未知的知识;自己的程序编写能力和算法能力明显的到了提升,自己的学习效率也有改善;不仅在本学科上,在其他的理科类学科又有一定的进步;不过自己平时的懒惰习惯依旧需要改善,学习积极性还有进步空间,还需要再努力。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值