hdoj 1506&&1505(City Game) dp

// l表示从l[i]到i连续大于a[i]的最远左区间。r表示从i到r[i]连续大于a[i]的最远又区间

DP 找出 a[i] 的最远左区间和最远右区间与自己连着的比自己大的数的长度 , 然后用这个长度乘以 a[i], 乘积最大的那个就是答案

hdoj 1506

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 100000+10
#define INF 0xfffffff
#define ll __int64
ll Max(ll x,ll y)
{
    if(x>y)
        return x;
    return y;
}
ll r[N],l[N];
ll a[N];
int main()
{
    ll n;
    while(scanf("%I64d",&n),n)
    {
        for(int i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        a[0]=a[n+1]=-INF;
        l[0]=r[n+1]=0;
        for(int i=1;i<=n;i++)
        {

                l[i]=i;
                while(a[l[i]-1]>=a[i])
                   l[i]=l[l[i]-1];

        }
        for(int i=n;i>=1;i--)
        {

                r[i]=i;
                while(a[r[i]+1]>=a[i])
                    r[i]=r[r[i]+1];

        }
        ll maxn=0;
        for(int i=1;i<=n;i++)
        {
            maxn=Max((r[i]-l[i]+1)*a[i],maxn);
        }
        printf("%I64d\n",maxn);
    }
    return 0;
}

hdoj 1505是1506的加强版,对于矩阵的每一行採取相同的操作

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define  N 1000+10
char a[N][N];
int dp[N][N];
int r[N],l[N];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        int maxn=0;
        scanf("%d%d",&n,&m);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
                {
                    scanf(" %c",&a[i][j]);
                    if(a[i][j]=='R')
                        dp[i][j]=0;
                    else
                        dp[i][j]=dp[i-1][j]+1;
                }
        }

        for(int i=1;i<=n;i++)
        {
            dp[i][0]=dp[i][m+1]=-1;
            l[0]=r[m+1]=0;
            for(int j=1;j<=m;j++)
            {
                l[j]=j;
                while(dp[i][l[j]-1]>=dp[i][j])
                   l[j]=l[l[j]-1];
            }
            for(int j=m;j>=1;j--)
            {

                r[j]=j;
                while(dp[i][r[j]+1]>=dp[i][j])
                   r[j]=r[r[j]+1];

            }
            for(int j=1;j<=m;j++)
            {
                maxn=max(maxn,(r[j]-l[j]+1)*3*dp[i][j]);
            }

        }
        printf("%d\n",maxn);
    }
    return 0;
}
时间: 2024-10-24 03:17:57

hdoj 1506&amp;&amp;1505(City Game) dp的相关文章

hdoj 1506&amp;&amp;1505(City Game) dp

// l表示从l[i]到i连续大于a[i]的最远左区间,r表示从i到r[i]连续大于a[i]的最远又区间 DP 找出 a[i] 的最远左区间和最远右区间与自己连着的比自己大的数的长度 , 然后用这个长度乘以 a[i], 乘积最大的那个就是答案 hdoj 1506 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define N 100000+10 #define

hdoj 1097 A hard puzzle (找规律)

A hard puzzle                            T   ime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29231    Accepted Submission(s): 10494 Problem Description lcy gives a hard puzzle to feng5166,lwg,JG

HDOJ题目3440 House Man(差分约束)

House Man Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2256    Accepted Submission(s): 896 Problem Description In Fuzhou, there is a crazy super man. He can't fly, but he could jump from hou

URAL 1506. Columns of Numbers(模拟啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1506 Every New Russian has to look through long columns of numbers for analyzing market trends and planning his investments. Psychologists assure that the longer is a column of numbers, the more diffi

树链剖分+线段树 HDOJ 4897 Little Devil I(小恶魔)

题目链接 题意: 给定一棵树,每条边有黑白两种颜色,初始都是白色,现在有三种操作: 1 u v:u到v路径(最短)上的边都取成相反的颜色 2 u v:u到v路径上相邻的边都取成相反的颜色(相邻即仅有一个节点在路径上) 3 u v:查询u到v路径上有多少个黑色边 思路: 对树进行树链剖分,分成重链和轻链,用两棵线段树W,L来维护.W维护树上在重链上的u和v之间的边的翻转情况(操作在线段树上的[pos[v],pos[u]]区间):L维护树上在重链上的u和v之间的相邻边的翻转情况.那么某一个点u与它父

HDOJ 题目2896 病毒侵袭(AC自动机)

病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15352    Accepted Submission(s): 3901 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年一遇的世界奇观,那是多么幸福的事

HDOJ 题目2222 Keywords Search(AC自动机)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 38611    Accepted Submission(s): 12437 Problem Description In the modern time, Search engine came into the life of everybody like

hdoj 2516 取石子游戏(Fib公式)

Problem Description 1堆石子有n个,两人轮流取.先取者第1次可以取任意多个,但不能全部取完.以后每次取的石子数不能超过上次取子数的2倍.取完者胜.先取者负输出"Second win".先取者胜输出"First win". Input 输入有多组.每组第1行是2<=n<2^31. n=0退出. Output 先取者负输出"Second win". 先取者胜输出"First win". 参看Samp

SDNU 1506.D.Swiss-system tournament(并列排序)

Description A Swiss-system tournament is a tournament which uses a non-elimination format. The first tournament of this type was a chess tournament in Zurich in 1895, hence the name "Swiss system". The tournament will be held based on following