牛客网暑期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);
    }
}

 

全部评论

相关推荐

机智的豹子有点心碎:UU我还在找工作还没找到,一直在搜简历怎么改,总结了这些: 1.SEO:简历根据每一个岗位定制化:使用这个岗位中所描述的工作的词,它要求什么技能就把自己的技能描述成什么样子,把SEO用在自己身上(把我的简历和个人特质,当成一个热门产品来做 “搜索引擎优化”),让HR能用最低的门槛看到我 2."顺序:把岗位要求的技能跟经历放在简历的最开头、最显眼的位置" 3.包装:简历是一个最终交付说明书,只要最终学习成长做得到就可以,在合适的范围内自我吹捧(我这个人怎么能够在HR的角度被迅速的看懂和看到,减轻HR的工作压力) 4.每点加小标题​:用6~10字概括该段内容,便于面试官快速抓取信息。 5.避免空泛描述​:拒绝“培养了组织能力”等泛泛而谈,替换为具体行动和成果。 6."使用“三段式结构”​​:每段经历按“为什么做-做了什么-结果如何”展开: ​a) 为什么做​:痛点或目标(例如“品牌声量不足”) ​b) 做​了什么:方法论(例如“趋势洞察+竞品对标+人群细分”) ​c) 结果如何​:量化成果或影响(例如“推动客户投放20万预算”)" 7.量化成果​:用数字体现工作成效(如“整理500+份资料”“撰写2万字报告”)。 这些有的是我想去的岗的,如果对你有用的话按需修改就好~加油,早日上岸!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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