結果

問題 No.370 道路の掃除
ユーザー 小指が強い人小指が強い人
提出日時 2016-05-18 16:54:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,436 bytes
コンパイル時間 1,884 ms
コンパイル使用メモリ 172,548 KB
実行使用メモリ 5,632 KB
最終ジャッジ日時 2024-04-15 22:55:11
合計ジャッジ時間 5,933 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 RE -
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 RE -
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 RE -
testcase_14 AC 2 ms
5,376 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 AC 2 ms
5,376 KB
testcase_20 RE -
testcase_21 WA -
testcase_22 RE -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 3 ms
5,376 KB
testcase_26 RE -
testcase_27 AC 6 ms
5,376 KB
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 WA -
testcase_32 RE -
testcase_33 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> veci;
typedef vector<string> vecs;
template<class T,class U> using Hash=unordered_map<T,U>;

#define REP(i, a, n) for(ll i = a; i < n; i++)
#define RREP(i, a, n) for(ll i = n-1; i >= a; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define MD 1000000007

template<class T> T read(){T a;cin >> a;return a;}
template<class T> void read(T& a){cin >> a;}
template<class T> void rarr(T a, int n){for(int i = 0; i < n; i++) {cin >> a[i];}}
template<class T> void write(T a){cout << a << endl;}
template<class T> void writes(vector<T> a, char* c = " "){cout << a[0];for(int i = 1; i < a.size(); i++)cout << c << a[i];cout << endl;;}
void write(double a){cout << fixed << setprecision(12) << a << endl;}
template<class T> void warr(T a, int n, char* c = " "){cout << a[0];for(int i = 1; i < n; i++)cout << c << a[i];cout << endl;}
void split(string s, string delim, veci& result){result.clear();string::size_type pos = 0;while(pos != string::npos){string::size_type p = s.find(delim, pos);if(p == string::npos){result.push_back(atoi(s.substr(pos).data()));break;}else {result.push_back(atoi(s.substr(pos, p - pos).data()));}pos = p + delim.size();}}
void split(string s, string delim, vecs& result){result.clear();string::size_type pos = 0;while(pos != string::npos){string::size_type p = s.find(delim, pos);if(p == string::npos){result.push_back(s.substr(pos));break;}else {result.push_back(s.substr(pos, p - pos));}pos = p + delim.size();}}
ll gcd(ll a, ll b){while(true){ll k = a % b;if(k == 0)return b;a = b;b = k;}}
ll comb(ll n, ll m){ll p=1;m=min(m,n-m);for(ll i=1;i<=m;i++){p*=n-i+1;p/=i;}return p;}

int n,m;
ll a[1000];
ll dp[1010][1010]={0};
ll MAX_VALUE=99999999999;

ll solve(ll pos,int l,int r,int i)
{
    if(i>=n)return 0;
    if(dp[l+1][r+1]>0)return dp[l+1][r+1];
    ll lv = MAX_VALUE;
    ll rv = MAX_VALUE;
    ll res=MAX_VALUE;
    if(l>=0){
        lv=abs(a[l]-pos);
        res=solve(a[l],l-1,r,i+1)+lv;
    }
    if(r<m){
        rv=abs(a[r]-pos);
        res=min(res,solve(a[r],l,r+1,i+1)+rv);
    }
    return dp[l][r]=res;
}

int main(void)
{
    cin>>n>>m;
    rep(i,m)cin>>a[i];
    sort(a,a+m);
    int mid=-1;
    rep(i,m)
        if(a[i]>=0){
            mid=i;
            break;
        }
    if(mid==-1)mid=m;
    cout<<solve(0,mid-1,mid,0)<<endl;
    return 0;
}
0