Submission #836191


Source Code Expand

Copy
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <deque>
#include <algorithm>
#include <queue>
#include <cmath>
#include <map>
#include <complex>
#include <cstring>
#include <iomanip>
#include <bitset>

using namespace std;
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define repd(i, a, b) for(int i = (a); i > (b); i--)
#define forIt(it, a) for(__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define forRev(it, a) for(__typeof((a).rbegin()) it = (a).rbegin(); it != (a).rend(); it++)
#define ft(a) __typeof((a).begin())
#define ll long long
#define ld long double
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define bitcount(n) __builtin_popcountll(n)


typedef complex<ld> cplex;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef pair<ll, ll> pll;

const int N = 1000 + 7;
const int M = 30;
const int inf = 1e9 + 7;
const long long linf = (ll) inf * inf + 7;
const double pi = acos(-1);
const double eps = 1e-20;
const bool multipleTest = false;

int n;
int a[N];

ll sqr(ll x) {
    return x * x;
}

void solve() {
    cin >> n;
    for(int i = 0; i < n; ++i) {
        scanf("%d", a + i);
    }
    sort(a, a + n);
    ll cost = linf;
    int res = a[0];
    
    for(int i = a[0]; i <= a[n - 1]; ++i) {
        ll cur = 0;
        for(int j = 0; j < n; ++j)
            cur += sqr(a[j] - i);
        //cout << i << ' ' << cost << "\n";
        if (cur < cost) {
            cost = cur;
            res = i;
        }
    }
    
    cout << cost;
    
}

int main() {
//#ifndef ONLINE_JUDGE
//    freopen("in.txt", "r", stdin);
//    auto t1 = clock();
//#endif
    
    
    int Test = 1;
    if (multipleTest) {
        cin >> Test;
    }
    
    for(int i = 0; i < Test; ++i) {
        solve();
    }
    
//#ifndef ONLINE_JUDGE
//    printf("\n%.9lf\n", (0.0 + clock() - t1) / CLOCKS_PER_SEC);
//#endif
}

Submission Info

Submission Time
Task C - Be Together
User creatnx
Language C++14 (GCC 5.4.1)
Score 200
Code Size 2199 Byte
Status AC
Exec Time 4 ms
Memory 256 KB

Compile Error

./Main.cpp: In function ‘void solve()’:
./Main.cpp:59:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", a + i);
                           ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 200 / 200
Status
AC × 4
AC × 15
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt
Case Name Status Exec Time Memory
0_000.txt AC 4 ms 256 KB
0_001.txt AC 4 ms 256 KB
0_002.txt AC 4 ms 256 KB
0_003.txt AC 4 ms 256 KB
1_004.txt AC 4 ms 256 KB
1_005.txt AC 4 ms 256 KB
1_006.txt AC 4 ms 256 KB
1_007.txt AC 4 ms 256 KB
1_008.txt AC 4 ms 256 KB
1_009.txt AC 4 ms 256 KB
1_010.txt AC 4 ms 256 KB
1_011.txt AC 4 ms 256 KB
1_012.txt AC 4 ms 256 KB
1_013.txt AC 4 ms 256 KB
1_014.txt AC 4 ms 256 KB