PAT准备之2018.7.24

昨天被我划水滑过去了,今天终于完成了救赎,基本没有划水,一直在认真的学习,今天也做了不少题,发现自己还是有很多知识点薄弱的地方,还是基础不太好吧,以前总觉得自己这些东西都会,结果发现真到自己用的时候,真的是不会。。。唉!这个暑假再把基础知识补一补吧。今天也是做了三道题。如下1007MaximumSubsequenceSum(25)(25分)Givenasequenceo…

大家好,又见面了,我是你们的朋友全栈君。

昨天被我划水滑过去了,今天终于完成了救赎,基本没有划水,一直在认真的学习,今天也做了不少题,发现自己还是有很多知识点薄弱的地方,还是基础不太好吧,以前总觉得自己这些东西都会,结果发现真到自己用的时候,真的是不会。。。
唉!这个暑假再把基础知识补一补吧。今天也是做了三道题。如下
1007 Maximum Subsequence Sum (25)(25 分)
Given a sequence of K integers { N~1~, N~2~, …, N~K~ }. A continuous subsequence is defined to be { N~i~, N~i+1~, …, N~j~ } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:

10 1 4
求区间最大字段和,很早就做过的题,最开始忘记枚举的怎么搞了。写了一个NlogN的,每次把前缀和放入一个堆中,维护小顶堆,每次取出与其相减,相当于求出到当前位置的最大区间和了,然后不断更新这个值。觉得很对,但是不知道为啥有一个点没过,有点难受。。

然后更改为了标准的解法,其实更加好写了,只要不断记录sum就行了,不过要注意最后的最大区间和可能是0。不能直接单独判断。

代码如下:

#include<bits/stdc++.h>

using namespace std;
const int MAX = 10010;
typedef long long ll;
ll x[MAX];
int N;
int main(void){
    cin >> N;
    for(int i=0;i<N;++i){
        cin >> x[i];
    }
    ll s = 0;
    ll Max = -1;
    int l = 0,r = N-1;
    int index = 0;
    bool isok = false;
    for(int i=0;i<N;++i){
        if(x[i] >= 0){
            isok = true;
        }
        s += x[i];
        if(s < 0){
            s = 0;
            index = i+1;
        }
        else if(s > Max){
            Max = s;
            l = index;
            r = i;
        }
   // cout << "s = " << s << " l = " << l << " r =" << r << " Max= " << Max << endl;
    }
    if(Max < 0){
        cout << 0 << " " << x[0] << " " << x[N-1] << endl;
    }
    else{
        cout << Max << " " << x[l] << " " << x[r] << endl;
    }
    return 0;
}

1008 Elevator (20)(20 分)
大水题一道,没什么好说的。代码也不粘了。

1009 Product of Polynomials (25)(25 分)
This time, you are supposed to find A*B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 a~N1~ N2 a~N2~ … NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (i=1, 2, …, K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < … < N2 < N1 <=1000.

Output Specification:

For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input

2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output

3 3 3.6 2 6.0 1 1.6

两个多项式相乘,也没什么好说的,坑点就是系数为0时要提前计数,不要把它也计算当中。

代码如下:

#include<bits/stdc++.h>

using namespace std;
const int MAX = 110;
const double eps = 1e-6;
class Node{
public:
    int k;
    double v;
};
Node q[MAX],p[MAX];
map<int,double> mp;

int main(void){
    int N,M;
    cin >> N;
    for(int i=1;i<=N;++i){
        cin >> q[i].k >> q[i].v;
    }
    cin >> M;
    for(int i=1;i<=M;++i){
        cin >> p[i].k >> p[i].v;
    }
    for(int i=1;i<=N;++i){
        for(int j=1;j<=M;++j){
            mp[q[i].k+p[j].k] += q[i].v*p[j].v;
        }
    }
    int sum = 0;
    for(map<int,double>::iterator it = mp.begin();it != mp.end();++it){
        if(abs(it->second-0) < eps)
            continue;
        sum++;
    }
    cout << sum;
    for(map<int,double>::reverse_iterator it = mp.rbegin();it != mp.rend();++it){
        if(abs(it->second-0) < eps)
            continue;
        cout << " " << it->first << " ";
        cout << fixed << setprecision(1) << it->second;
    }
    return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/127471.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 人体检测–热释电传感器开发

    人体检测–热释电传感器开发人体检测–热释电传感器开发人体热释电传感器顾名思义是探测是否有人体通行和通过,由于它的廉价性,使得它的应用范围非常广泛。楼道里的灯,天台的报警设施等,都是利用这个来进行报警和检测。本文章将分为两个板块来介绍传感器的开发和应用。一·热释电传感器的工作原理  某些晶体,例如钽酸锂、硫酸三甘肽等受热时,晶体两端会产生数量相等、符号相反的电荷。1842年布鲁斯特将这种由温度变化引起的电极化现象正式命名为…

    2022年9月1日
    0
  • Head First Java 中文版 (第 2 版) PDF 下载

    Head First Java 中文版 (第 2 版) PDF 下载微信公众号:一个优秀的废人如有问题或建议,请后台留言,我会尽力解决你的问题。简介《HeadFirstJava》是一本完整地面向对象(object-oriented,OO)程序设计和Java的学习指导用书,根据学习理论所设计,你可以从程序语言的基础开始,到线程、网络与分布式程序等项目。重要的是,你可以学会如何像一个面向对象开发者一样去思考,而且不只是读死书。  在这里,你可以会玩游戏、拼…

    2022年7月8日
    27
  • h5页面 请在微信客户端打开链接_模拟微信接口时,提示“请在微信客户端打开链接”(转)…[通俗易懂]

    h5页面 请在微信客户端打开链接_模拟微信接口时,提示“请在微信客户端打开链接”(转)…[通俗易懂]背景描述相信有模拟微信页面请求的测试都有看到过这个页面,简单点说就是爬虫爬微信页面,进行回放的时候会出现这个页面。大概在1年前,专门安排了一个人去解决这个技术问题,遗憾的是当时没有找到解决方案,接下来所有微信端的接口测试和性能测试都无法进行,今天和大家分享下我们的解决方案,希望大家可以绕过微信的坑。业务场景我这里以JMeter来举例,我们可以通过在JMeter上开启代理,手机上设置代理来录制微信端…

    2022年6月7日
    30
  • Subversion代码提交中的org.apache.subversion.javahl.ClientException: svn: E200007: Commit failed异常解决

    Subversion代码提交中的org.apache.subversion.javahl.ClientException: svn: E200007: Commit failed异常解决

    2021年7月18日
    72
  • android系统中toast是什么_android studio toast不显示

    android系统中toast是什么_android studio toast不显示Toast控件介绍Toast是Android系统提供的轻量级信息提醒机制,用于向用户提示即时消息,它显示在应用程序界面的最上层,显示一段时间后自动消失不会打断当前操作,也不获得焦点。使用Toast提示信息的实例代码:Toast.makeText(Context,Text,Time),show();这段代码首先调用了Toast的makeText方法用来设置提示信息,Context:表示应用程序环境的信息,就是当前组件的上下文环境,如果在Activity中使用的话,那么该参数可设置为”Activi

    2022年9月13日
    0
  • Ewebeditor最新漏洞及漏洞大全

    Ewebeditor最新漏洞及漏洞大全

    2021年12月7日
    47

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号