跳到正文

个人记录

3.27-甜菜训练课-401

cxhtieyc约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年4月2日
更新
2026年4月2日
篇幅
约 7 分钟

版本

  1. v1当前

P1190 [NOIP 2010 普及组] 接水问题

1.错误原因

想太复杂了并且没有想清楚

2.正确思路

在所有水龙头都在用是接水时间会最少,就是一开始要加入m个元素到堆里面 保证接水时间尽可能小,就是每次用最小接水时间的水龙头给下一个人接水 这里有两种解法: 解法1:暴力模拟 0(n*m) 这里会超时,不能用 解法2:堆 n log n

3.代码

C++
#include<bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,greater<int>> q;
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x;
        cin>>x;
        q.push(x);
	}
	for(int i=m+1;i<=n;i++){
        int x;
        cin>>x;
		int y=q.top();
        q.pop();
        q.push(x+y);
	}
    int ans=0;
    while(!q.empty()){
        int y=q.top();
        q.pop();
        ans=max(ans,y);
    }
	cout<<ans;
	return 0;
}

T577644 字符串大师

1.错误原因

暴力还爆错了…

2.正确思路

题意并不难理解,就直接上思路 写法有两种: 暴力写法: 操作1:循环翻转 reverse(s.begin(),s.end());时间复杂度:0(n) 操作2:时间复杂度:0(n) I.在前面加入元素:s=s+a; II.在后面加入元素:s=s+a; NO!NO!NO!这样子肯定拿不到满分

C++
正确写法:deque<char>q;
先解决操作2:
I.q.push_front(c);
II.q.push_back(c);
操作1:只需要维护是否需要翻转  int cnt=0;  出现1操作:cnt^=1;  判断出现1操作是否为偶数次
if(cnt==0){  为偶数次
    f==1 => q.push_front(c);
    f==2 => q.push_back(c);
}else{  不为偶数次
    f==2 => q.push_front(c);
    f==1 => q.push_back(c);
}
输出:
cnt==0
从前往后输出
cnt==1
从后往前输出

3.代码

C++
#include<bits/stdc++.h>
using namespace std;
int n,t,f,cnt;
char c;
string s;
deque<char> q;
int main(){
	cin>>s;
	cin>>n;
	for(int i=0;i<s.size();i++){
		q.push_back(s[i]);
	}
	for(int i=1;i<=n;i++){
        cin>>t;
        if(t==1){
            cnt^=1;
        }else{
            cin>>f>>c;
            if(f==1){
                if(cnt==0){
                    q.push_front(c);
                }else{
                    q.push_back(c);
                }
            }else{
                if(cnt==0){
                    q.push_back(c);
                }else{
                    q.push_front(c);
                }
            }
        }
    }
    if(cnt==1){
       while(q.size()>0){
           cout<<q.back();
           q.pop_back();
       }
    }else{
        while(q.size()>0){
           cout<<q.front();
           q.pop_front();
       }
    }
	return 0;
}

T671141 Large Queue

1.错误原因

暴力超

2.正确思路

题面已经说得很清楚了,上思路 同样也是一个拥有两种解法的题:

C++
解法1:queue模拟
    操作1:for(int i=1;i<=c;i++) q.push(x);
    long long ans=0;
    操作2:for(int i=1;i<=k;i++) ans+=q.front(),q.pop();
    暴力只有50分...
    解法2:结构体模拟
    struct st{
        int x,c;
    };
    queue<st> q;
    操作1:q.push({x,c});
    操作2:
    I.删除k个,先取出队首看够不够删
    1.够删(q.front().c>=k) : q.front().c-=k; cout<<q.front().x*k;
    II.不够删 => 循环取出队首一次删除
    while(q.size()>0){
        if(q.front().c>=k){ans+=k*q.front().x;
            q.front().c-=k;
            break;
        }else{
            k-=q.front().c;ans+=q.front().c*q.front().x;
            q.pop();
        }
    }
    cout<<ans;

3.代码

C++
#include<bits/stdc++.h>
using namespace std;
long long n,f,c,x,k;
struct st{
    long long c,x;
};
queue<st> q;
int main(){
	cin>>n;
	while(n--){
		cin>>f;
		if(f==1){
            cin>>c>>x;
			q.push({c,x});
		}else{
			cin>>k;
            long long ans=0;
            while(q.size()>0){
                if(q.front().c>=k){
                    ans+=k*q.front().x;
                    q.front().c-=k;
                    break;
                }else{
                    k-=q.front().c;
                    ans+=q.front().c*q.front().x;
                    q.pop();
                }
            }
            cout<<ans<<endl;
        }
	}
	return 0;
}
原文