Submission #1335879
Source Code Expand
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define rep(i,a,b) for(int i = a; i <= b; i++)
#define dep(i,a,b) for(int i = a; i >= b; i--)
#define Rep(i,a) for(int i = 0; i < a; i++)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define ab(x) ((x) < 0 ? -(x) : (x))
using namespace std;
typedef long long LL;
typedef map<int, int>::iterator mit;
typedef set<int>::iterator sit;
typedef pair<int, int> pii;
#define x first
#define y second
const int N = 5010;
const LL inf = 1LL << 60;
int n, l[N], r[N], id[N];
LL f[N][N], g[2][N];
inline bool cmp(const int &a, const int &b) { return l[a] + r[a] < l[b] + r[b]; }
int main() {
scanf("%d",&n);
rep(i,1,n) scanf("%d%d", l + i, r + i), swap(l[i], r[i]);
rep(i,1,n) id[i] = i; sort(id + 1, id + n + 1, cmp);
f[0][0] = 0; rep(j,1,n) f[0][j] = inf;
int cl = n >> 1, cr = n >> 1;
rep(i,1,n) {
int x = id[i];
rep(j,0,i) {
f[i][j] = inf;
if (j - 1 >= 0)
f[i][j] = min(f[i][j], f[i - 1][j - 1] + l[x] + 1LL * (l[x] + r[x]) * (cl - j));
f[i][j] = min(f[i][j], f[i - 1][j] + r[x] + 1LL * (l[x] + r[x]) * (cr - (i - j)));
}
rep(j,i+1,n) f[i][j] = inf;
}
if (!(n & 1)) printf("%lld\n",f[n][n / 2]);
else {
LL ans = inf;
g[(n+1)&1][0] = 0; rep(j,1,n) g[(n+1)&1][j] = inf;
dep(i,n,1) {
int x = id[i], cur = i & 1;
rep(j,0,n - i + 1) {
g[cur][j] = inf;
if (j - 1 >= 0) g[cur][j] = min(g[cur][j], g[!cur][j - 1] + l[x] + 1LL * (l[x] + r[x]) * (j - 1));
g[cur][j] = min(g[cur][j], g[!cur][j] + r[x] + 1LL * (l[x] + r[x]) * ((n - i + 1) - j - 1));
}
rep(j,n-i+2,n) g[cur][j] = inf;
int t = n >> 1;
LL tot = 1LL * t * (l[x] + r[x]);
rep(j,0,t) ans = min(ans, tot + f[i - 1][j] + g[!cur][t - j]);
}
cout <<ans<<endl;
}
return 0;
}
Submission Info
Submission Time
2017-06-07 20:08:41+0900
Task
F - Intervals
User
WuHongxun
Language
C++14 (GCC 5.4.1)
Score
1000
Code Size
1932 Byte
Status
AC
Exec Time
107 ms
Memory
196096 KB
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:29:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
^
./Main.cpp:30:58: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
rep(i,1,n) scanf("%d%d", l + i, r + i), swap(l[i], r[i]);
^
Judge Result
Set Name
Sample
All
Score / Max Score
0 / 0
1000 / 1000
Status
Set Name
Test Cases
Sample
example0.txt, example1.txt
All
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, example0.txt, example1.txt
Case Name
Status
Exec Time
Memory
000.txt
AC
81 ms
196096 KB
001.txt
AC
25 ms
83200 KB
002.txt
AC
81 ms
196096 KB
003.txt
AC
107 ms
170240 KB
004.txt
AC
81 ms
196096 KB
005.txt
AC
61 ms
120576 KB
006.txt
AC
81 ms
196096 KB
007.txt
AC
48 ms
136960 KB
008.txt
AC
81 ms
196096 KB
009.txt
AC
16 ms
58368 KB
010.txt
AC
81 ms
196096 KB
011.txt
AC
43 ms
126720 KB
012.txt
AC
81 ms
196096 KB
013.txt
AC
15 ms
52224 KB
014.txt
AC
81 ms
196096 KB
015.txt
AC
35 ms
83328 KB
016.txt
AC
81 ms
196096 KB
017.txt
AC
65 ms
170112 KB
018.txt
AC
81 ms
196096 KB
019.txt
AC
71 ms
132992 KB
020.txt
AC
1 ms
256 KB
021.txt
AC
1 ms
256 KB
022.txt
AC
81 ms
196096 KB
023.txt
AC
81 ms
196096 KB
024.txt
AC
81 ms
196096 KB
025.txt
AC
80 ms
196096 KB
026.txt
AC
81 ms
196096 KB
027.txt
AC
81 ms
196096 KB
028.txt
AC
81 ms
196096 KB
029.txt
AC
80 ms
196096 KB
030.txt
AC
81 ms
196096 KB
031.txt
AC
81 ms
196096 KB
032.txt
AC
81 ms
196096 KB
033.txt
AC
81 ms
196096 KB
034.txt
AC
81 ms
196096 KB
035.txt
AC
81 ms
196096 KB
036.txt
AC
81 ms
196096 KB
037.txt
AC
80 ms
196096 KB
038.txt
AC
80 ms
196096 KB
039.txt
AC
81 ms
196096 KB
040.txt
AC
81 ms
196096 KB
041.txt
AC
81 ms
196096 KB
042.txt
AC
81 ms
196096 KB
example0.txt
AC
1 ms
256 KB
example1.txt
AC
1 ms
384 KB