日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

PAT甲级1046 Shortest Distance:[C++题解]前缀和

發布時間:2025/4/5 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PAT甲级1046 Shortest Distance:[C++题解]前缀和 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

      • 題目分析
      • 題目鏈接

題目分析


來源:acwing

分析:

用前綴和快速求出一段的和。注意求兩段,取最小值。

ac代碼

#include<bits/stdc++.h> using namespace std;const int N = 1e5+10; int a[N]; int s[N]; int n ,m;int main(){cin >> n;for(int i = 1; i<= n; i++) cin>> a[i], s[i] = s[i-1] +a[i];cin >> m;while(m--){int x, y;cin >> x >> y;if( x > y) swap(x,y);int d1 = s[y-1] -s[x-1];int d2 = s[n]-s[y-1] +s[x-1];cout<< min(d1,d2)<<endl;} }

題目鏈接

PAT甲級1046 Shortest Distance
https://www.acwing.com/problem/content/1532/

總結

以上是生活随笔為你收集整理的PAT甲级1046 Shortest Distance:[C++题解]前缀和的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。