刷题记录(NC13822 Keep In Line,NC16663 合并果子,NC16430 蚯蚓)

NC13822 Keep In Line

题目链接

关键点: 

1、建一个队列,每次出队时判断当前队列的头是否为要出队的人名,如果不是,有两种情况:

(1)该同学插队,提前出来

(2)可能该同学前面的同学均为插队的,那么该同学并没有插队

所以我们要在保持原队列的基础上,标记那么插队(提前出来的人),这样每次判断时,就可以找到前面是否有插队的

完整代码:

# include <bits/stdc++.h>
using namespace std;
int t, n;
int main()
{
    cin>>t;
    while (t--)
    {
        queue<string>q;
        set<string>se;
        int n, cnt=0;
        cin>>n;
        string s, name;
        for (int i=1; i<=n; i++)
        {
//             cout<<cnt<<endl;
            cin>>s>>name;
            if (s[0]=='i')
            q.push(name);
            else 
            {
                string chu = q.front();
//                 cout<<chu<<endl;
                if (chu == name)
                {
                    cnt++;
                    q.pop();
//                     cout<<i<<" "<<name<<endl;
                }
                else
                {
                    if (se.empty())
                    {
                        se.insert(name);
                        continue;
                    }
                    while (se.find(chu)!=se.end())
                    {
                        q.pop();
                        chu = q.front();
                    }
                    if (chu == name)
                    {
                        cnt++;
                        q.pop();
//                         cout<<i<<" "<<name<<endl;
                    }
                    else
                        se.insert(name);
                }
            }
        }
        cout<<cnt<<endl;
    }
    
    
    
    return 0;
}

 NC16663 合并果子

题目链接

关键点:

三种做法

一、双队列

1、创建两个队列,第一个队列记录第一次输入时从小到大的果子,从该队列取两个最小的果子合并,再将合并后的果子存在第二个队列,我们可以发现,这样的取法使得第二个队列(即只存两个果子合并后的数)也为从小到大的排列,那么每次从两个队列中取最小的两个合并,再存入第二个队列中,且最后花费为每次合并后果子的大小

代码:

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int a[200000+10];
long long sum;
queue<ll>q1, q2;
int main()
{
    cin>>n;
    for (int i=1; i<=n; i++)
        cin>>a[i];
    sort(a+1, a+1+n);
    for (int i=1; i<=n; i++)
        q1.push(a[i]);
    for (int j=1; j<n; j++)
    {
        int x[3];
        for (int i=1; i<=2; i++)
        {
            if ((q1.front()<q2.front()&&!q1.empty()) || q2.empty())
            {
                x[i] = q1.front();
                q1.pop();
            }
            else
            {
                x[i] = q2.front();
                q2.pop();
            }
        }
        ll ans = x[1]+x[2];
        q2.push(ans);
        sum+=ans;
    }
    cout<<sum;
    
    return 0;
}

二、优先队列priority_queue

我们直接用一个优先队列存所有果子(包括合并后的果子),每次从该队列中取两个,算出花费,并将其加入该队列,因为优先队列默认为大顶堆,即从大到小存,我们可以存其相反数,取出后再次相反即可

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int a[10000+10];
priority_queue<ll>q;
ll sum;
int main()
{
    cin>>n;
    for (int i=1; i<=n; i++)
    {
        int x;
        cin>>x;
        q.push(-x);
    }
    for (int i=1; i<n; i++)
    {
        ll x = -q.top();
        q.pop();
        ll y = -q.top();
        q.pop();
        ll ans = x+y;
        q.push(-ans);
        sum+=ans;
    }
    cout<<sum;
    
    
    
    return 0;
}

三、手动实现优先队列

首先建一个数组q,再记录一个数(last)记录数组最后一个下标。

push(x),先将x存在最后一个下标中(++last)中,之后每次跟父亲结点比较,如果比父亲结点小则交换,直到不满足为止

top直接输出p[1]

pop(),先将最后一个结点赋给第一个结点,再去和最小的孩子比较大小,再交换,直到不满足为止

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int a[100000+10];
ll sum;
ll q[100000+10];
int last = 0;
void push(int x)
{
    last++;
    q[last] = x;
    int parent = last/2, i = last;
    while (q[parent]>q[i] && parent!=0)
    {
        swap(q[i], q[parent]);
        i = parent;
        parent = parent/2;
    }
}
void pop()
{
    q[1] = q[last];
    int i = 1;
    last--;
    int c = i*2;
    if (q[c]>q[c+1]&&c+1<=last) c++;
    while (q[c]<q[i] && c<=last)
    {
        swap(q[c], q[i]);
        i = c;
        c = i*2;
        if (q[c]>q[c+1]&&c+1<=last) c++;
    }
}
int top()
{
    return q[1];
}
int main()
{
    cin>>n;
    for (int i=1; i<=n; i++)
    {
        int x;
        cin>>x;
        push(x);
    }
    for (int i=1; i<n; i++)
    {
        int a = top();
        pop();
        int b = top();
        pop();
        ll ans = a+b;
        push(ans);
        sum+=ans;
    }
    cout<<sum;
    
    
    
    return 0;
}

NC16430 蚯蚓

题目链接

关键点: 

1、首先用三个队列,第一个队列记录原始的蚯蚓,第二个记录被切掉的前段蚯蚓,第三个记录被切掉的后半段蚯蚓,每次从三个蚯蚓里找最大的数进行操作

2、如何处理每次蚯蚓增加的长度:我们可以只在数组里记录数组一开始的长度,最后输出的时候再根据时间直接加上增加长度即可

3、如何处理后出生的蚯蚓,可以将新出生的蚯蚓长度定义为当前长度-出生时间*q,这样就可以和直接就存在的蚯蚓计算时间方式相同

完整代码:

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, q, u, v, t;
int a[1000000+10];
queue<ll> qu[3];
int main()
{
    cin>>n>>m>>q>>u>>v>>t;
    for (int i=1; i<=n; i++)
        cin>>a[i];
    sort(a+1, a+1+n);
    for (int i=n; i>=1; i--)
        qu[0].push(a[i]);
    for (int i=1; i<=m; i++)
    {
        ll maxx = -1e18, pos = -1;
        for (int j=0; j<3; j++)
        {
            if (!qu[j].empty() && qu[j].front()>maxx)
            {
                maxx = qu[j].front();
                pos = j;
            }
        }
        qu[pos].pop();
        maxx += (i-1)*q;
        if (i%t==0) cout<<maxx<<" ";
        ll l = maxx*u/v, r = maxx - l;
        qu[1].push(l-i*q), qu[2].push(r-i*q);
    }
    cout<<endl;
    ll cnt = 1;
    while (!qu[0].empty()||!qu[1].empty()||!qu[2].empty())
    {
        ll maxx = -1e18, pos = -1;
        for (int i=0; i<3; i++)
        {
            if (!qu[i].empty()&&qu[i].front()>maxx)
            {
                pos = i;
                maxx = qu[i].front();
            }
        }
        qu[pos].pop();
        if (cnt%t == 0)
            cout<<maxx+m*q<<" ";
        cnt++;
    }
    cout<<endl;
    
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值