8 条题解

  • 0
    @ 2026-7-14 9:45:26

    方法如下:

    1.定义一个长度为n的数组(vla) (编译器(msvc)不支持vla可使用动态数组vector)

    2.读入数据(0~n-1)

    3.使用sort(不要漏了algorithm)升序排序(无需加greater)

    4.定义ans=0

    5.从0~n-1遍历,ans每次加a[i]和a[i]*(n-i) n-i为当前剩余人数

    6.输出ans

    为避免抄袭,本篇题解不放出任何代码

    • 0
      @ 2026-5-12 21:01:33

      #include<bits/stdc++.h> using namespace std;

      int cnt[100+5],n,a,r,t; int main(){ cin >> n; for(int i=0; i<n; i++){ cin >> a; cnt[a]++; } for(int i=0; i<=100; i++){ for(int j=0; j<cnt[i]; j++){ t+=i; r+=t; } } cout << r; return 0; }

      • 0
        @ 2025-12-13 16:23:00

        包AC

        #include<bits/stdc++.h>
        using namespace std;
        
        int cnt[100+5],n,a,r,t;
        int main(){
        	cin >> n;
        	for(int i=0; i<n; i++){
        		cin >> a;
        		cnt[a]++;
        	}
        	for(int i=0; i<=100; i++){
        		for(int j=0; j<cnt[i]; j++){
        			t+=i;
        			r+=t;
        		}
        	}
        	cout << r;
        	return 0;
        } 
        
        
        • 0
          @ 2023-4-26 19:43:01
          #include <iostream>
          #include <algorithm>
          using namespace std;
          int maxx=0x3f3f3f;
          const int N=1e3+10;
          int a[N] ,sum,cnt,n;
          int main()
          {		
          	cin>>n;
          	for(int i=1;i<=n;i++)
          	{
          		cin>>a[i];
          	}
          	sort(a+1,a+n+1);
          	for(int i=1;i<=n;i++)
          	{
          		sum=sum+(a[i]+cnt);
          		cnt+=a[i];
          	}
          	cout<<sum;
          	return 0;
          }
          
          • -1
            @ 2024-3-10 18:03:06
            #include <iostream>
            using namespace std;
            int main()
            {
            	int s,t,n,a[1001]; 
            	cin >> n;
            	for(int i = 1;i <= n;i++)
            		cin >> a[i];
            	sort(a+1,a+1+n);
            	s = t = a[1];
            	for(int i = 2;i <= n;i++) {
            		s += a[i]+t;
            		t += a[i];
            	}
            	cout << s;
            	return 0;
            }
            
            • -1
              @ 2023-4-26 19:43:40
              #include <math.h>
              #include <stack>
              #include <stdio.h>
              #include <iostream>
              #include <vector>
              #include <iomanip>
              #include <string.h>
              #include <algorithm>
              using namespace std;
              #define LL long long
              #define ull unsigned long long
              const int N = 1e3 + 10;
              const int INF = 0x3f3f3f3f;
              int n,a[N],cnt,sum;
              int main(void){
              	cin >> n;
              	for (int i=1; i<=n; i++){
              		cin >> a[i];
              		
              	}
              	sort(a+1, a+n+1);
              	
              	for (int i=1; i<=n; i++){
              		sum=sum+(a[i]+cnt);
              		cnt = cnt +a[i];
              	}
              	cout << sum << endl;
              	return 0;
              } 
              ```
              
              • -2
                @ 2023-9-10 12:05:09

                论前缀和的用处

                #include <bits/stdc++.h>
                using namespace std;
                const int N=1e5+10;
                const int INF=0x3f3f3f3f;
                int a[N],s[N],ans;
                int main()
                {
                	int n;
                	cin>>n;
                	for(int i=1;i<=n;i++)
                	{
                		cin>>a[i];
                	}
                	sort(a+1,a+n+1);
                	for(int i=1;i<=n;i++)
                	{
                		s[i]=a[i]+s[i-1];
                		ans+=s[i];
                	}
                	cout<<ans;
                }
                
                
                • -3
                  @ 2022-8-21 17:24:25
                  #include <queue>
                  #include <math.h>
                  #include <stack>
                  #include <stdio.h>
                  #include <iostream>
                  #include <vector>
                  #include <iomanip>
                  #include <string.h>
                  #include <algorithm>
                  using namespace std;
                  #define LL long long
                  #define ull unsigned long long
                  const int N = 1e3 + 10;
                  const int INF = 0x3f3f3f3f;
                  int a[N];
                  int main()
                  {
                  	int n;
                  	cin >> n;
                  	for(int i = 0 ; i < n ; i++)
                  		cin >> a[i];
                  	sort(a,a+n);
                  	int ans = 0;
                  	for(int i = 0 , j = n ; i < n , j > 0 ; i++ , j--)
                  		ans += a[i] * j;
                  	cout << ans << endl;
                  	return 0;
                  }
                  
                  • 1

                  信息

                  ID
                  1004
                  时间
                  1000ms
                  内存
                  256MiB
                  难度
                  4
                  标签
                  递交数
                  711
                  已通过
                  304
                  上传者