題目:MT2047距離平方和
你有𝑛n個點,請編寫一個程序,求這𝑛n個點的距離的平方和。
格式
輸入格式:
第一行:一個整數𝑛(0≤𝑛≤100000)n(0≤n≤100000);
接下來𝑛n行:每行兩個整數𝑥,𝑦x,y,表示該點坐標(?10000≤𝑥,𝑦≤10000)(?10000≤x,y≤10000)。
輸出格式:
僅一行:所有點的距離的平方和。
樣例 1
輸入:
4 1 1 -1 -1 1 -1 -1 1
輸出:
32
#include<bits/stdc++.h>
using namespace std;int main() {int n;cin >> n;long long ans = 0; long long sx = 0, sy = 0; for (int i = 0; i < n; ++i) {int x, y;cin >> x >> y;ans += (n - 1LL) * (x*x + y*y) - 2 * (x*sx + y*sy);sx += x;sy += y;}cout << ans;return 0;
}
題目:MT2051矩形
給定一個N?M的矩陣,11表示已經占用了,00表示沒有被占用,求一個由00構成的矩陣,使其周長最大。
格式
輸入格式:
第一行兩個整數𝑛,𝑚n,m含義如上;
接下來𝑛n行每行𝑚m個數表示這個矩陣。
輸出格式:
輸出一個數,表示最大周長。
樣例 1
輸入:
3 3 000 010 000
輸出:
8
樣例 2
輸入:
5 4 1100 0000 0000 0000 0000
輸出:
16
#include<bits/stdc++.h>
using namespace std;
//二維前綴和模版題
int main( )
{int n,m;cin>>n>>m;int sum[30][30];memset(sum,0,sizeof(sum));for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){char x;cin>>x;sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+x-'0';}}int maxn=0;for(int x1=1;x1<=n;x1++){for(int y1=1;y1<=m;y1++){for(int x2=x1;x2<=n;x2++){for(int y2=y1;y2<=m;y2++){if(sum[x2][y2]-sum[x2][y1-1]-sum[x1-1][y2]+sum[x1-1][y1-1]>0)continue; maxn=max(maxn,(x2-x1+1+y2-y1+1)*2);}}}} cout<<maxn;return 0;
}
知識點
memest :初始化數組或結構體。