hdu 4163 Stock Prices 花式排序

Stock Prices

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 987    Accepted Submission(s): 397

Problem Description

Buy
low, sell high. That is what one should do to make profit in the stock
market (we will ignore short selling here). Of course, no one can tell
the price of a stock in the future, so it is difficult to know exactly
when to buy and sell and how much profit one can make by repeatedly
buying and selling a stock.

But if you do have the history of
price of a stock for the last n days, it is certainly possible to
determine the maximum profit that could have been made. Instead, we are
interested in finding the k1 lowest prices and k2 highest prices in the
history.

Input

The
input consists of a number of cases. The first line of each case starts
with positive integers n, k1, and k2 on a line (n <= 1,000,000, k1 +
k2 <= n, k1, k2 <= 100). The next line contains integers giving
the prices of a stock in the last n days: the i-th integer (1 <= i
<= n) gives the stock price on day i. The stock prices are
non-negative. The input is terminated by n = k1 = k2 = 0, and that case
should not be processed.

Output

For
each case, produce three lines of output. The first line contains the
case number (starting from 1) on one line. The second line specifies the
days on which the k1 lowest stock prices occur. The days are sorted in
ascending order. The third line specifies the days on which the k2
highest stock prices occur, and the days sorted in descending order. The
entries in each list should be separated by a single space. If there
are multiple correct lists for the lowest prices, choose the
lexicographically smallest list. If there are multiple correct lists for
the highest prices, choose the lexicographically largest list.

Sample Input

10 3 2
1 2 3 4 5 6 7 8 9 10
10 3 2
10 9 8 7 6 5 4 3 2 1
0 0 0

Sample Output

Case 1
1 2 3
10 9
Case 2
8 9 10
2 1

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1000001
const int inf=0x7fffffff;   //无限大
struct node
{
    int x;
    int y;
};
node a[maxn];
node b[maxn];
bool cmp(node c,node d)
{
    if(c.x==d.x)
        return c.y<d.y;
    return c.x<d.x;
}
bool cmp1(node c,node d)
{
    return c.y>d.y;
}
bool cmp2(node c,node d)
{
    return c.y<d.y;
}
int main()
{
    int n,k1,k2;
    int cas=1;
    while(scanf("%d%d%d",&n,&k1,&k2)!=EOF)
    {
        if(n==0)
            break;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i].x);
            a[i].y=i+1;
        }
        sort(a,a+n,cmp);
        sort(a,a+k1,cmp2);
        sort(a+n-k2,a+n,cmp1);
        printf("Case %d\n",cas++);
        int first=1;
        for(int i=0;i<k1;i++)
        {
            if(first)
            {
                printf("%d",a[i].y);
                first=0;
            }
            else
                printf(" %d",a[i].y);
        }
        printf("\n");
        first=1;
        for(int i=n-k2;i<n;i++)
        {
            if(first)
            {
                printf("%d",a[i].y);
                first=0;
            }
            else
                printf(" %d",a[i].y);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-09-27 01:20:15

hdu 4163 Stock Prices 花式排序的相关文章

hdu 4163 Stock Prices 水

#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 const int N=1e6+100,M=4e6+10,inf=1e9+10,mod=1e9+7; const ll INF=1e18+10; struct is { int x; int pos; }a[N]; int cmp1(is a,is b) { if(a.x!=b.

hdu 4857 逃生 (拓扑排序+保证最小在前面)

逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 74    Accepted Submission(s): 13 Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前. 同时,社会是不平

HDU 4324:Triangle LOVE( 拓扑排序 )

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2271    Accepted Submission(s): 946 Problem Description Recently, scientists find that there is love between any of two people. For

HDU 4857 (反向拓扑排序 + 优先队列)

题意:有N个人,M个优先级a,b表示a优先于b,并且每个人有个编号的优先级,输出顺序. 思路来自:与PKU3687一样 在基本的拓扑排序的基础上又增加了一个要求:编号最小的节点要尽量排在前面:在满足上一个条件的基础上,编号第二小的节点要尽量排在前面:在满足前两个条件的基础上,编号第三小的节点要尽量排在前面--依此类推.(注意,这和字典序是两回事,不可以混淆.) 如图 1 所示,满足要求的拓扑序应该是:6 4 1 3 9 2 5 7 8 0. 图 1 一个拓扑排序的例子 一般来说,在一个有向无环图

hdu 4857 逃生(拓扑排序逆序 + 优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 题意:有编号 1 - n 的 n 个人逃生,编号越小的人越有钱, 在满足 m 个前提的条件下要尽可能早的逃脱 .m个前提,对于每个前提 a , b,代表 a 要早于 b逃脱. 思路: (1)这题可以理解为有钱的人优先级越高,所以可以用优先队列. (2)但是要注意这道题和字典序升序的区别. eg:5 1 5 1 按照字典序的答案:2 3 4 5 1,    本题答案: 5 1 2 3 4. 因为

HDU 5438 Ponds (拓扑排序+DFS)2015 ACM/ICPC Asia Regional Changchun Online

[题目链接]:click here~~ [题目大意]: 题意:在一个无向图中有 p 个点, m 条边,每个点有一个值 vi .不断的删去度数小于2的点直到不能删为止.求新图中所有点个数为奇数的连通分量的点值的和. 1<p<10^4,1<m<10^5 [思路]删边考虑类似拓扑排序的写法,不过topsort是循环一遍1到n结点入度为0的结点,然后加入到队列中,这里只要改一下度数小于等于1,最后DFS 判断一下 挫挫的代码: /* * Problem: HDU No.5438 * Run

hdu 5188 zhx and contest [ 排序 + 背包 ]

传送门 zhx and contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 145    Accepted Submission(s): 49 Problem Description As one of the most powerful brushes in the world, zhx usually takes part

HDU 1412 {A} + {B}【排序+合并】

/* 题目大意:合并一样的数 解题思路:排序后再合并相同的数 关键点:合并相同的数 解题人:lingnichong 解题时间:2014-08-29 01:04:44 解题体会:学会了如何合并一样的数 */ {A} + {B} Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11890    Accepted Submission(s)

HDU 4857 逃生(拓扑排序逆向+邻接表存图)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 题目: Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会是不平等的,这些人有的穷有的富.1号最富,2号第二富,以此类推.有钱人就贿赂负责人,所以他们有一些好处. 负责人现在可以安排大家排队的顺序,由于收了好处,所以他要让1号尽量靠前,