S + T

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描写叙述

给你一个长度为n的整数序列A1。A2,……,An,找出两个整数Ai和Aj(i<j),使得Ai-Aj尽量大。

输入
输入第一行为数据组数T(T<=100)。

每组数据的第一行为整数的个数n(2<=n<=100000);一下n行。每行为一个绝对值不超过150000的正整数。

(全部的数据都不超过2^64 - 1)

输出
对于每组数据,输出Ai –Aj的最大值、i值和j值。(假设有多个最大值,则输出先出现的最大值和i,j值)
例子输入
235 2 162 3 6 2 1 -1
例子输出
4 1 37 3 6
提示
假设你觉得数据太大,则本OJ推荐使用long long
来源
算法入门
上传者

userid=ACM_%E9%92%9F%E8%AF%97%E4%BF%8A" style="color:rgb(55,119,188)">ACM_钟诗俊

刚開始用的结构体优先队列,结果发现wa了几次才发现,i>j...也就是说前面的数减去后面数的最大值。

想想还是把wa的贴出来吧。AC贪心策略是,先把数据用数组保存起来。然后for循环从0-n筛选最大值,保存起来,从n-0筛选最小值,保存起来。

最后再用一个for循环寻找差的最大值。详细看以下ac代码。

wa:

#include <stdio.h>
#include <queue>
using namespace std;
struct node
{
long long t,num;
bool operator<(const node &a)const
{
if(num<a.num)
return true;
else if(num==a.num&&t<a.t)
return true;
else
return false;
}
};
priority_queue<node>s;
int main()
{
long long ncase,mark1,mark2,sum,n,mark;
node x,y1,y2,temp;
scanf("%lld",&ncase);
while(ncase--)
{
scanf("%lld",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&x.num);
x.t=i;
s.push(x);
}
y1=s.top();
while(s.size()>1)
{
temp=s.top();
if(temp.num!=y1.num)
break;
else
mark=temp.t,s.pop();
}
while(s.size()>1)
s.pop();
y2=s.top(),s.pop();
printf("%lld",y1.num-y2.num);
if(y1.num!=y2.num)
printf(" %lld %lld\n",mark,y2.t);
else
printf(" 1 2\n");
}
return 0;
}

ac:

#include <stdio.h>
#include <string.h>
#define N 100005
long long num[N];
struct node
{
long long max,t1,min,t2;
}c[N];
int main()
{
long long t,n,temp,mark1,mark2;
scanf("%lld",&t);
while(t--)
{
memset(num,0,sizeof(num));
memset(&c,0,sizeof(&c));
scanf("%lld",&n);
temp=-150005;
for(int i=0;i<n;i++)
{
scanf("%lld",&num[i]);
if(temp<num[i])
temp=num[i],mark1=i+1;
c[i].max=temp;
c[i].t1=mark1;
}
temp=150005;
for(int i=n-1;i>=0;i--)
{
if(num[i]<=temp)
temp=num[i],mark2=i+1;
c[i].min=temp;
c[i].t2=mark2;
}
temp=-150005*2;
for(int i=0;i<n-1;i++)
{
if(temp<c[i].max-c[i+1].min)
temp=c[i].max-c[i+1].min,mark1=c[i].t1,mark2=c[i+1].t2;
}
printf("%lld %lld %lld\n",temp,mark1,mark2);
}
return 0;
}

nyoj847 S + T(贪心)的更多相关文章

  1. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  2. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

  7. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

  8. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

  9. 【贪心】HDU 1257

    HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...

随机推荐

  1. 72.2801 LOL-盖伦的蹲草计划(广搜)

    时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 众所周知,LOL这款伟大的游戏,有个叫盖伦的英雄.他的伟大之处在 ...

  2. nginx hello模块代码

    // ngx_http_mytest_module.c #include "ngx_core.h" #include "ngx_string.h" #inclu ...

  3. VK Cup 2016 - Qualification Round 1 (Russian-Speaking Only, for VK Cup teams) C. Promocodes with Mistakes 水题

    C. Promocodes with Mistakes 题目连接: http://www.codeforces.com/contest/637/problem/C Description During ...

  4. Codeforces Round #245 (Div. 2) B. Balls Game 并查集

    B. Balls Game Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...

  5. Debounce 和 Throttle 的原理及实现---防止频繁触发某事件

    原文:http://blog.csdn.net/redtopic/article/details/69396722 在处理诸如 resize.scroll.mousemove 和 keydown/ke ...

  6. ASM相关视图

    oracle asm对于我们并非一个黑盒子 ,我们能够通过对应视图查看asm instance信息,和oracle系统视图一样. v$asm_diskgroup x$kfgrp  记录diskgrou ...

  7. Wifi模块的工作原理

    http://www.wifitop1.com/news/content-98.html 在无线网络领域里面,无线wifi是最火的名词.对于串口wifi模块的工作是什么呢?串口wifi模块又有什么功能 ...

  8. DotNet.Utilities工具类

    来源:http://git.oschina.net/kuiyu/dotnetcodes/tree/master/DotNet.Utilities

  9. redis主键失效机制

    Memcached删除主键的方式与Redis有何异同 首先,Memcached 在删除失效主键时也是采用的消极方法,即 Memcached 内部也不会监视主键是否失效,而是在通过 Get 访问主键时才 ...

  10. 数学图形(1.19)Doppler spiral螺线

    一种左右对称的螺线 相关软件参见:数学图形可视化工具,使用自己定义语法的脚本代码生成数学图形.该软件免费开源.QQ交流群: 367752815 vertices = t = *PI) to (*PI) ...