cometoj——C1100 [Contest #8]支援城市

    技术2022-07-11  102

    题目描述

    1267年,战争的味道在空气中弥漫,强大的尼弗迦德帝国蓄势待发。觊觎着雅鲁加河对岸的北方领域。莱里亚的女王米薇为了抵御尼弗迦德帝国的进攻,在莱里亚王国内建造了 n 个城市。第 i个城市中居住着 w_i个公民。当尼弗迦德帝国进攻某一个城市时,其他所有城市将支援被进攻的城市。但这些城市的居民会因为支援其他城市而产生不满意度。

    当城市 a 要前往城市 b 支援时,会产生 (w_a-w_b)^2点不满意度。

    米薇女王想知道对于每个城市被进攻时,分别会产生多少点不满意度。

    即对于每个城市 xx ,你需要回答 \sum_{i=1}n{(w_i-w_x)2} 的值。

    输入描述

    第 1行一个整数 n ,代表有 n 座城市。

    第 2 行 n 个整数,第 i 个整数 w_i代表第 i 个城市的人口数量。

    2≤n≤10^5

    1≤w_i≤10^6

    输出描述

    一行 n 个整数,分别是第 1 个被攻击产生的不满意度到第 n 个城市被攻击的不满意度。

    样例输入 1

    3 3 3 3 样例输出 1

    0 0 0 样例输入 2

    3 3 4 5 样例输出 2

    5 2 5 样例输入 3

    5 19 4326 7891 744 999 样例输出 3

    82004658 55159127 173256882 64500983 59594018

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub Scanner read = new Scanner(System.in); int n = read.nextInt(); long human[] = new long[n];//int会爆 long allsum=0; long allsquaredsum=0; for(int i=0;i<n;i++) { human[i] = read.nextLong(); allsum+=human[i]; allsquaredsum+=human[i]*human[i]; } for(int i=0;i<n;i++) { if(i!=n-1) System.out.print(allsquaredsum+n*human[i]*human[i]-2*human[i]*allsum+" "); else System.out.print(allsquaredsum+n*human[i]*human[i]-2*human[i]*allsum); } } }
    Processed: 0.012, SQL: 9