PAT (Advanced Level) 1007 Maximum Subsequence Sum( DP ) 1006 Sign In and Sign Out( map )

PAT 1007

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
typedef long long LL;
const int maxn=1e4+5;
const LL INF=0x3f3f3f3f3f3f3f3f;
LL max(LL a,LL b)
{
    return a>b?a:b;
}
int main()
{
    PII p[maxn];
    LL n,dp[maxn],a[maxn];
    scanf("%lld",&n);
    LL solve=-INF;
    int l,r;
    bool flag=true;
    for(int i=0;i<n;i++)
    {
        LL x;
        scanf("%lld",&x);
        if(x>=0) flag=false;
        a[i]=x;
        if(i==0) {
                dp[i]=x;
                p[i].first=0;
                p[i].second=0;
                if(solve<dp[i]) {
                l=p[i].first;
                r=p[i].second;
                solve=dp[i];
            }
        }
        else {
            if(x>dp[i-1]+x) {
                p[i].first=i;
                p[i].second=i;
                p[i-1].second=i-1;
            }
            else {
                p[i].first=p[i-1].first;
                p[i].second=p[i-1].second+1;
            }
            dp[i]=max(x,dp[i-1]+x);
            if(solve<dp[i]) {
                l=p[i].first;
                r=p[i].second;
                solve=dp[i];
            }
        }
    }
    if(flag) {
        cout<<"0"<<" "<<a[0]<<" "<<a[n-1]<<endl;
    }
    else cout<<solve<<" "<<a[l]<<" "<<a[r]<<endl;
    return 0;
}

PAT 1006

#include<bits/stdc++.h>
using namespace std;
map<int,string>str;
int main()
{
    str.clear();
    int n;
    scanf("%d",&n);
    getchar();
    for(int i=0;i<n;i++)
    {
        string k,k1,k2,k3;
        k.clear();
        k1.clear();
        k2.clear();
        k3.clear();
        getline(cin,k,'\n');
        int len=k.length(),j=0;
        for(;j<len;j++)
        {
            if(k[j]==' ') {
                k1=k.substr(0,j);
                break;
            }
        }
        k2=k.substr(j+1,8);
        k3=k.substr(j+10,8);
        //cout<<k1<<endl<<k2<<endl<<k3<<endl;
        int x1=0,x2=0;
        x1+=((k2[0]-'0')*10+(k2[1]-'0'))*3600+((k2[3]-'0')*10+(k2[4]-'0'))*60+((k2[6]-'0')*10+(k2[7]-'0'));
        x2+=((k3[0]-'0')*10+(k3[1]-'0'))*3600+((k3[3]-'0')*10+(k3[4]-'0'))*60+((k3[6]-'0')*10+(k3[7]-'0'));
        str[x1]=k1;
        str[x2]=k1;
    }
    map<int,string>::iterator it;
    it=str.begin();
    cout<<(*it).second<<" ";
    string k;
    k.clear();
    k=(*it).second;
    for(;it!=str.end();it++)
    {
        k=(*it).second;
    }
    cout<<k<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值