POJ3186+3190+3250

来源:互联网 发布:js获取鼠标当前位置 编辑:程序博客网 时间:2024/06/05 15:45
总结一块发到这里吧
今天竟然放了重题,还好刚做了不久,印象挺深,然后就开始了GG,
题目都有点队列的意思,题目看的是不少,
在中间问有没有人在A  c e的时候,我做e已经一个半小时了,若是单纯的计算需要几台机器
倒是简单,就和公司搬桌子问题一样了,不用贪心了,直接转化为查找最大区间重复范围即可
加上这头牛正在使用第几胎机器,我感到很惆怅,另一道动态规划我傻啊傻的模拟,看别人代码
长度才几百,我就有点慌啊,意识到了问题,(就真的只是意识到了,无作为)最后那个搜索直接没敢看
前几天看大牛的博客,动态规划还得刷,如他说的  一通百通
我现在七窍通六窍,哎,一窍不通,有点夸张了
先把题目附上吧


FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. 

The treats are interesting for many reasons:
  • The treats are numbered 1..N and stored sequentially in single file in a long box that is open at both ends. On any day, FJ can retrieve one treat from either end of his stash of treats.
  • Like fine wines and delicious cheeses, the treats improve with age and command greater prices.
  • The treats are not uniform: some are better and have higher intrinsic value. Treat i has value v(i) (1 <= v(i) <= 1000).
  • Cows pay more for treats that have aged longer: a cow will pay v(i)*a for a treat of age a.
Given the values v(i) of each of the treats lined up in order of the index i in their box, what is the greatest value FJ can receive for them if he orders their sale optimally? 

The first treat is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.
Input
Line 1: A single integer, N 

Lines 2..N+1: Line i+1 contains the value of treat v(i)
Output
Line 1: The maximum revenue FJ can achieve by selling the treats
Sample Input
513152
Sample Output
43
Hint
Explanation of the sample: 

Five treats. On the first day FJ can sell either treat #1 (value 1) or treat #5 (value 2). 

FJ sells the treats (values 1, 3, 1, 5, 2) in the following order of indices: 1, 5, 2, 3, 4, making 1x1 + 2x2 + 3x3 + 4x1 + 5x5 = 43.

题意:大体就是给出n个数,然后每次只能从首尾位置上选一个乘以当前出队的个数,求最大值

思路:直接来个模拟  求最大值 肯定是首尾位置上小的先出队  这样必然是最大值,

 但是有一种情况难以解决   如果首尾位置 相等 还要继续往内收敛,

那么就没法解决了 这时候就有点觉着是动态规划了 好吧 我写不出来

一直拖到最后看了题解    感觉有点一点就通了  代码应该可以秒懂

  • Source Code
    /*#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<cmath>using namespace std;int main(){    int i,j,k;    int n;    int a[2001];    int vis[100001];    while(cin>>n)    {        for(i=0;i<n;i++)        {            cin>>a[i];            }        j=0;        k=n-1;        long long ans=0;        int num=1;        memset(vis,0,sizeof(vis));        int flag=1;        for(int m=0;m<n;m++)        {            flag=0;            for(i=0;i<n;i++)            {                //cout<< vis[i]<<" ";                if(vis[i]==0)                {                    flag=1;                    }                }            //cout<<endl;            if(flag==0)break;            if(a[j]<a[k]&&vis[j]==0&&vis[k]==0)            {                vis[j]=1;                ans+=(a[j]*num);                num++;                j++;                }            else if(a[k]<a[j]&&vis[j]==0&&vis[k]==0)            {                vis[k]=1;                ans+=(a[k]*num);                num++;                k--;                }            }            for(i=0;i<n;i++)            {                if(vis[i]==0)                {                    ans+=(a[i]*num);                    break;                    }                }            cout<<ans<<endl;        }    return 0;    }*/#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<cmath>using namespace std;int max(int a,int b){    return a>b?a:b;    }int ans[2001][2001];int main(){    int i,j;    int n;    int a[2002];    cin>>n;    for(i=1;i<=n;i++)        cin>>a[i];    for(i=n;i>0;i--)        for(j=i;j<=n;j++)            ans[i][j]=max(ans[i+1][j]+a[i]*(n+i-j),ans[i][j-1]+a[j]*(n+i-j));    cout<<ans[1][n]<<endl;}

Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads.

Each cow i has a specified height hi (1 ≤ h≤ 1,000,000,000) and is standing in a line of cows all facing east (to the right in our diagrams). Therefore, cow i can see the tops of the heads of cows in front of her (namely cows i+1, i+2, and so on), for as long as these cows are strictly shorter than cow i.

Consider this example:

        ==       ==   -   =         Cows facing right -->=   =   == - = = == = = = = =1 2 3 4 5 6 

Cow#1 can see the hairstyle of cows #2, 3, 4
Cow#2 can see no cow's hairstyle
Cow#3 can see the hairstyle of cow #4
Cow#4 can see no cow's hairstyle
Cow#5 can see the hairstyle of cow 6
Cow#6 can see no cows at all!

Let ci denote the number of cows whose hairstyle is visible from cow i; please compute the sum of c1 through cN.For this example, the desired is answer 3 + 0 + 1 + 0 + 1 + 0 = 5.

Input
Line 1: The number of cows, N
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i.
Output
Line 1: A single integer that is the sum of c 1 through cN.
Sample Input
610374122
Sample Output
5
题意:一周之前刚写过这个  再写一次吧

向右寻找比当前位置低的  碰到高的则更新位置为下一个  求一个总和

思路:

首先第一个元素入队,然后找到比他小的入队,如果第三个比第二个大,则更新二号位置,ans里记下下表即可,每次入队的元素逗比前面小,每次加上的num值包含前面所有比他高的数的个数

  • Source Code
    #include<iostream>#include<stdio.h>#include<string.h>using namespace std;int map[80001];int main(){    int i,j;    int n,num,x;    long long ans;    while(cin>>n)    {        memset(map,0,sizeof(map));        num=0;        ans=0;        for(i=1;i<=n;i++)        {            cin>>x;            while(num>0&&map[num]<=x)            {                num--;                }            ans+=num;            num++;            map[num]=x;            }        cout<<ans<<endl;        }    return 0;    }
Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows. 

Help FJ by determining:
  • The minimum number of stalls required in the barn so that each cow can have her private milking period
  • An assignment of cows to these stalls over time
Many answers are correct for each test dataset; a program will grade your answer.
Input
Line 1: A single integer, N 

Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.
Output
Line 1: The minimum number of stalls the barn must have. 

Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.
Sample Input
51 102 43 65 84 7
Sample Output
412324
Hint
Explanation of the sample: 

Here's a graphical schedule for this output: 

Time     1  2  3  4  5  6  7  8  9 10Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..Stall 3 .. .. c3>>>>>>>>> .. .. .. ..Stall 4 .. .. .. c5>>>>>>>>> .. .. ..
Other outputs using the same number of stalls are possible.
题意:这个题开始理解错了,输出第一行还是没错的,后面一度理解为当前需要几台机器

还一度怀疑样例出错了  提示愣是看不懂 细心看了看 才领悟

n头牛挤奶,每头牛都有一个时间段,一台机器只能为一头牛挤奶,问你最少需要几台机器

后面分别是是那一台机器榜这头牛挤奶的

看样例   因为2 4  4 7这两头牛没有重复的区间

所以两头牛相当于公用一台机器

理解这点就有点絮头了

思路:单纯的最少的机器很好解决

但是机器的标号问题让人很伤脑筋

对于每个新进来的牛  若是他开始的时间在前面结束的后面 则说明两头牛可以共用 

反之 则机器的数量上需要+1   并且新来的需要用当前加的这台机器

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
    int l;
    int e;
    int c;
    bool friend operator<(node A,node B)
    {
        if(A.e==B.e)
            return A.l>B.l;
        return A.e>B.e;
    }
}d[60000];
priority_queue<node>q;
int num[50001];
bool cmp(node A,node B)
{
    if(A.l==B.l)
        return A.e<B.e;
    return A.l<B.l;
}
int main()
{
    int n;
    int i,j;
    int ans=1;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        scanf("%d %d",&d[i].l,&d[i].e);
        d[i].c=i;
    }
    sort(d+1,d+n+1,cmp);
    q.push(d[1]);
    num[d[1].c]=1;
    for(i=2;i<=n;i++)
    {
        node temp;
        temp=q.top();
        int x=temp.e;
        if(x<d[i].l)
        {
            num[d[i].c]=num[temp.c];
            q.pop();
            }
        else
        {
            ans++;
            num[d[i].c]=ans;
            }
        q.push(d[i]);
        }
    cout<<ans<<endl;
    for(i=1;i<=n;i++)
        cout<<num[i]<<endl;
    }

原创粉丝点击