牛客网暑期ACM多校训练营(第二场)-D-money(贪心,物品买卖)

题目链接:https://www.nowcoder.com/acm/contest/140/D
 

White Cloud has built n stores numbered from 1 to n.
White Rabbit wants to visit these stores in the order from 1 to n.
The store numbered i has a price a[i] representing that White Rabbit can spend a[i] dollars to buy a product or sell a product to get a[i] dollars when it is in the i-th store.
The product is too heavy so that White Rabbit can only take one product at the same time.
White Rabbit wants to know the maximum profit after visiting all stores.
Also, White Rabbit wants to know the minimum number of transactions while geting the maximum profit.
Notice that White Rabbit has infinite money initially.

 

 

输入描述:

The first line contains an integer T(0<T<=5), denoting the number of test cases.
In each test case, there is one integer n(0<n<=100000) in the first line,denoting the number of stores.
For the next line, There are n integers in range [0,2147483648), denoting a[1..n].

输出描述:

For each test case, print a single line containing 2 integers, denoting the maximum profit and the minimum number of transactions.

 

示例1

输入

复制

1
5
9 10 7 6 8

输出

复制

3 4

题目大意:T组测试样例,对于每组样例,一个n代表n个数,然后是n个数,表示第i天的商品的价值;只能往前走不能后退,问走完一遍的能够获得的最大收益,同时的最少交易次数;

注意,这里的物品只能拿一个(想要那新的必须放弃(卖掉)手中的)

物品买卖的题,贪心似乎很好写;

ac:

#include<stdio.h>
#include<string.h> 
#include<math.h> 
   
#include<map>  
//#include<set>
#include<deque> 
#include<queue> 
#include<stack> 
#include<bitset>
#include<string> 
#include<fstream>
#include<iostream> 
#include<algorithm> 
using namespace std; 
 
#define ll long long 
#define INF 0x3f3f3f3f 
//#define mod 1e9+7
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b)
#define clean(a,b) memset(a,b,sizeof(a))// 水印
//std::ios::sync_with_stdio(false);
 
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        ll cnt=0,ans=0,have;
        bool f=0;
        for(int i=1;i<=n;++i)
        {
            ll x;
            scanf("%lld",&x);
            if(i==1)
                have=x;
            if(have<x)//有收益
            {
                ans=ans+x-have;//收益
                if(f==0)//如果手中没东西
                    cnt++;//使用have的交换
                have=x;//换成x
                f=1;//手中有东西了
            }
            else if(have>x)//如果没有收益(现在这个比手中的这个还要小)
            {
                f=0;
                have=x;//原来的那个都不用拿,选择那这个
            }
            //cout<<ans<<" "<<cnt<<endl;
        }
        printf("%lld %lld\n",ans,cnt*2);
    }
}

 

全部评论

相关推荐

09-22 22:22
中山大学 Java
乌鱼子萨奇:羡慕你啊,直接转正了,都不用经历秋招的炼狱,但是你少经历了很多痛苦的事情啊
点赞 评论 收藏
分享
08-28 11:37
已编辑
华东师范大学 Java
Sigma777:本来想说师弟怎么把我这个老东西卷没了,仔细一看是师兄 简历不错,但是得准备好选型话术,比如我举个例子你为什么要用caffeine,一般我们的小项目不会有这么hot的key需要本地缓存,你要说明你是如何发现有这么hot的key连redis都兜不住的,引入后优化了多少时间,然后还有本地缓存大小设置为多少,这个大小能保证热点key不会因为太小而淘汰也不会因为太大影响服务吗,为什么不用guava,引入本地缓存同步问题怎么解决。 然后分库分表,为什么你觉得要分表,数据量多少,分多少张表几个库,分片键选择依据,你的所有查询能不能准确定位到某一张避免全库扫描,有没有数据倾斜问题就是分的每张表数据量差距特别大,你是一开始分库分表还是后期发现瓶颈才分,如果后期才分你如何把旧表的数据搬过去同时还能确保业务正常运行。 然后是消息队列,你说缓存高并发请求,却选择了吞吐量较小的rabbit,有什么原因吗,为什么不选Kafka。 然后你说分布式锁解决集群环境并发安全,也就是说你是集群部署的,请问是怎么部署的,docker还是k8s,部署几台,配置是多少,jvm参数设置是多少,有监控吗,线上遇到故障吗,怎么解决的,有做负载均衡吗,数据是怎么压测的等等。 zset缓存本月实时排行数据具体怎么做的,会有大key问题吗。 其他本小渣暂时想不到了,留给其他大神点评
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务