【PAT-1046】 Shortest Distance (20分)

news/2024/7/2 1:19:05 标签: 算法, 模拟, PAT

考点:简单模拟

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

int main(int argc, char const *argv[])
{
    int n;
    int m, sum = 0;
    scanf("%d", &n);
    vector<int> res(n + 1);
    for (int i = 1; i <= n; i++)
    {
        int temp;
        scanf("%d", &temp);
        sum += temp;
        res[i] = sum;
    }
    scanf("%d", &m);
    for (int i = 1; i <= m; i++)
    {
        int a, b, sum1 = 0, sum2 = 0;
        scanf("%d %d", &a, &b);
        if (a > b)
        {
            int temp = a;
            a = b;
            b = temp;
        }

        sum1 = res[b - 1] - res[a - 1];
        sum2 = sum - sum1;
        if (sum2 > sum1)
        {
            cout << sum1 << endl;
        }
        else
        {
            cout << sum2 << endl;
        }
    }

    return 0;
}

 


http://www.niftyadmin.cn/n/920422.html

相关文章

迭代法

hdu 3809; 一&#xff0c;迭代法 的基本概念&#xff1a; 迭代法事一种常用算法设计方法。迭代式一个不断用新值取代变量的旧值&#xff0c;或由旧值递推出变量的新值的过程。迭代机制需要以下一些要素&#xff1a; ①迭代表达式&#xff1b; ②迭代变量&#xff1b; ③迭代初…

【PTA-1083】 List Grades (25分)

【简单排序】 水题 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std;struct node {string name;string id;int grade; };bool cmp(node a, node b) {return a.grade >…

VS2008安装失败!Microsoft Visual Studio Web 创作组件

解决方案一&#xff1a;找到Office 2007的安装文件 第一步&#xff1a;因为除Visual Studio本身以外&#xff0c;VS安装过程中安装的其他附带安装的组件&#xff0c;基本上都是有单独安装包的。先找到Visual Studio Web 创作组件的独立安装包&#xff0c;试试单独安装能不能成…

【PTA-1129】 Recommendation System (25分)

Set 求解 #include <iostream> #include <cstdio> #include <algorithm> #include <set> using namespace std; // 按照频率进行降序排序&#xff0c;相同 // 按照index进行升序排序 const int maxn 50000 10; int has[maxn]; struct node {int valu…

【PTA-1154】 1154 Vertex Coloring (25分)

【SetHash】1154 Vertex Coloring (25分) // sethash #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <set> using namespace std; const int maxn 10000 10; struct node {int t1, t2; };int main(i…

openKM试用记

目前需要评估一款文档系统DMS,兼由知识库功能.数字资产管理等.所以瞄上了 OpenKM. 老实说,还真是做的不错!来给大家看看安装后在后台服务器端来预览一个已经上传了的文档的截图: 可以看出它的Preview功能做的相当好. 只是它的文档存储技术架构采用的是Apache Jackrabbit,这个东…

【PTA-1052】 Linked List Sorting (25分)

【PTA-1052】 Linked List Sorting (25分) #include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std;const int maxn 100000 10; int e[maxn], ne[maxn]; int n, h; bool cmp(int a, int b) {return e[…

【PTA-1086】 Tree Traversals Again (25)

【PTA-1086】 Tree Traversals Again (25) 分析&#xff1a; 本质上就是前序中序&#xff0c;输出后序 细节&#xff1a; 一个是输入数据的处理&#xff0c;一个是注意编号是从1开始的 #include <iostream> #include <cstdio> #include <algorithm> #includ…