• 贪心(栈维护,好题)——[Usaco2006 Mar]Mooo


    暴力亦可过,同没意思。。。
    有个O(n)的算法
    从左向右扫
    {
    while{
    if(栈顶元素.h>当前元素.h)   入队,更新栈顶元素对应的牛的V ,结束while循环
    else 出队
    }直到队列为空
    }
    再从右向左
    View Code
    #include<stdio.h>
    #include
    <iostream>
    #include
    <stack>
    using namespace std;
    struct data
    {
    int h,v,no;
    }node[
    50009];
    int all[50009];

    int main()
    {
    int n;
    while(scanf("%d",&n)!=EOF)
    {
    int i;
    for(i=0;i<n;i++)
    {
    scanf(
    "%d%d",&node[i].h,&node[i].v);
    node[i].no
    =i;
    all[i]
    =0;
    }

    stack
    <data>ss;
    ss.push(node[
    0]);
    for(i=1;i<n;i++)//从左到右
    {
    while(!ss.empty())
    {
    if(ss.top().h>node[i].h)
    {
    all[ss.top().no]
    +=node[i].v;
    ss.push(node[i]);
    break;
    }
    ss.pop();
    }
    if(ss.empty())ss.push(node[i]);//若为空入栈
    }

    while(!ss.empty())
    ss.pop();
    ss.push(node[n
    -1]);
    for(i=n-2;i>=0;i--)//从右到左
    {
    while(!ss.empty())
    {
    if(ss.top().h>node[i].h)
    {
    all[ss.top().no]
    +=node[i].v;
    ss.push(node[i]);
    break;
    }
    ss.pop();
    }
    if(ss.empty())ss.push(node[i]);
    }

    int max=0;
    for(i=0;i<n;i++)
    if(max<all[i])
    max
    =all[i];

    printf(
    "%d\n",max);
    }
    }

      

      


    #include<stdio.h>
    #include
    <iostream>
    #include
    <stack>
    using namespace std;
    struct data
    {
    int h,v,no;
    }node[
    50009];
    int all[50009];

    int main()
    {
    int n;
    while(scanf("%d",&n)!=EOF)
    {
    int i;
    for(i=0;i<n;i++)
    {
    scanf(
    "%d%d",&node[i].h,&node[i].v);
    node[i].no
    =i;
    all[i]
    =0;
    }

    stack
    <data>ss;
    ss.push(node[
    0]);
    for(i=1;i<n;i++)//从左到右
    {
    while(!ss.empty())
    {
    if(ss.top().h>node[i].h)
    {
    all[ss.top().no]
    +=node[i].v;
    ss.push(node[i]);
    break;
    }
    ss.pop();
    }
    if(ss.empty())ss.push(node[i]);
    }

    while(!ss.empty())
    ss.pop();
    ss.push(node[n
    -1]);
    for(i=n-2;i>=0;i--)//从右到左
    {
    while(!ss.empty())
    {
    if(ss.top().h>node[i].h)
    {
    all[ss.top().no]
    +=node[i].v;
    ss.push(node[i]);
    break;
    }
    ss.pop();
    }
    if(ss.empty())ss.push(node[i]);
    }

    int max=0;
    for(i=0;i<n;i++)
    if(max<all[i])
    max
    =all[i];

    printf(
    "%d\n",max);
    }
    }

      

  • 相关阅读:
    any、some、all for sql
    Date CONVERT() 函数
    Config Database
    移动master 数据库
    使用 OpenRowSet 和 OpenDataSource 访问 Excel 972007
    动态管理视图和函数
    SQL Server 2005 Merge Replication Step by Step Procedure
    系统试图(返回表所有记录数及所有的 identity 列)
    js if without curly brackets bug All In One
    Web3 All In One
  • 原文地址:https://www.cnblogs.com/huhuuu/p/2118839.html
Copyright © 2020-2023  润新知