is the order a rabbit (模拟+预处理+一点点贪心)

In a remote place, there is a rabbits market where rabbits can be bought and sold in every day. The trading rules in this market are as follows:

1.The market is available only in morning and afternoon in one day. And the rabbit transaction price may be different every day even different between morning and afternoon in a day.

2.It is prohibited that purchasing more than once in a day. And only one rabbit can be purchased at a time.

3. You can only sell once in a day, but you can sell rabbits unlimited quantities (provided that you have so many rabbits)

Mr.Cocktail has traded in this market for N days, suppose he has unlimited money to buy rabbits. Before the first day and after the last day, he has no rabbits. Now, in these N days, how much money did he earn the most?

Input
The first line contains a positive integer N, which means there are N days in total (1 \leq n \leq 10^5)(1≤n≤10 
5
 )

Next, there are N lines, each line contains two positive integers, representing the rabbit price in the morning and afternoon of the day a_i,b_i(1 \leq a_i ,b_i \leq 10^9)a 
i
​
 ,b 
i
​
 (1≤a 
i
​
 ,b 
i
​
 ≤10 
9
 ).

Output
Output an integer on a line to indicate the answer.

Sample 1
Inputcopy    Outputcopy
3
1 6
2 3
7 1
11
Sample 2
Inputcopy    Outputcopy
2
5 4
3 2
0
Note
In example 1, a rabbit was purchased for 1 in the morning of the first day, and a rabbit was purchased for1inthemorningofthefirstday,andarabbitwaspurchasedfor2 in the afternoon of the second day. On the morning of the third day, the two rabbits were sold, a total profit of $11.

The price of the rabbit in sample 2 continues to decrease, and it is impossible to make money through buying and selling, so choose not to make any buying and selling, and output the answer 0.
View problem

swjtu—春季集训 - Virtual Judge (vjudge.net)

思路:

  • 按照时间顺序模拟,当后面的值没有比当前大,就全部卖出(贪心
  • 通过排序+树状数组解决是否有后面的值比自己大的情况
#include <bits/stdc++.h>
using namespace std;
#define ri register int
#define M 100005

template <class G> void read(G &x)
{
    x=0;int f=0;char ch=getchar();
    while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=f?-x:x;
    return ;
}

int n,m;

struct dian{
    int val,id;
    bool operator <(const dian &t)const
    {
        return val>t.val;
    }
}p[M*2];
int val[M*2];
int flag[M*2];
long long num[M*2];
int cur=0;
void add(int a)
{
    while(a<=cur)
    {
        val[a]++;
        a+=a&(-a);
    }
}
int qu(int a)
{
    int ans=0;
    while(a>0)
    {
       ans+=val[a];
       a-=a&(-a);
    }
    return ans;
}
int main(){
    
    
    read(n);

    for(ri i=1;i<=n;i++)
    {
        read(p[++cur].val);p[cur].id=cur;
        num[cur]=p[cur].val;
        read(p[++cur].val);p[cur].id=cur;
        num[cur]=p[cur].val;
    }
    sort(p+1,p+1+cur);
    for(ri i=1;i<=cur;i++)
    {
        if(qu(cur)-qu(p[i].id-1)) 
        {
            flag[p[i].id]=1;
        }
        add(p[i].id);
    }
    long long ans=0;
    long long tmp=0;
    for(ri i=1;i<=cur;i+=2)
    {
        if(!flag[i])
        {
            ans+=tmp*num[i];tmp=0;
            if(flag[i+1]) ans-=num[i+1],tmp++;
            continue;
        }
        if(!flag[i+1])
        {
            ans-=num[i];tmp++;
            ans+=tmp*num[i+1];
            tmp=0;
            continue;
        }
        ans-=min(num[i],num[i+1]);
        tmp++;
    }
    printf("%lld",ans);
    return 0;
    
    
}
View Code

后记:

  • 注意数组范围是2倍,循环的时候也是 2n,不要习惯写 成n了
posted @ 2022-05-06 15:34  VxiaohuanV  阅读(46)  评论(0编辑  收藏  举报