This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 1987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define COMPRESS(a) sort(ALL(a));a.resize(unique(ALL(a))-a.begin())
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<db, db> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<double> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int,int,int,int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 1000000007;
ll POW(ll a, ll b, ll MMM=MOD) {ll ret=1; for(;b;b>>=1,a=(a*a)%MMM)if(b&1)ret=(ret*a)% MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[]={2,2,-2,-2,1,1,-1,-1},ddy[]={1,-1,1,-1,2,-2,2,-2};
vector<Vi> V;
VPi vv,v[2005];
int a[2005];
int cnt[2005];
int d[2005][2005][2];
void dfs(int N,int p=-1){
cnt[N]=1;
for(auto [next,cost]:v[N]){
if(next==p)continue;
dfs(next,N);
cnt[N]+=cnt[next];
}
}
void dfs2(int N,int p=-1){
if(p!=-1 && v[N].size()==1){
d[N][1][1]=a[N];
d[N][0][1]=d[N][1][0]=-INF;
return;
}
d[N][0][1]=-INF;
for(auto [next,cost]:v[N]){
if(next==p)continue;
dfs2(next,N);
fup(i,1,cnt[N],1){
d[N][i][0]=max(d[next][i][1],d[next][i][0]);
if(i==cnt[N])d[N][i][0]=-INF;
d[N][i][1]=max(d[next][i-1][0]+a[N],d[next][i-1][1]+a[N]+cost);
}
}
}
void g(int N){
if(v[N].empty()){
cnt[N]=1;
V.pb(Vi{0,a[N]});
return;
}
dfs(N);
dfs2(N);
Vi VV;
fup(i,0,cnt[N],1)VV.pb(max(d[N][i][0],d[N][i][1]));
V.pb(VV);
}
int dd[2005][2005];
int go(int N,int K){
if(N==V.size())return 0;
int &ret=dd[N][K];
if(~ret)return ret;
ret=0;
int n=min(K,(int)V[N].size()-1);
fup(i,0,n,1){
ret=max(ret,V[N][i]+go(N+1,K-i));
}
return ret;
}
int main() {
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
fup(i,1,n,1){
int x,y;
scanf("%d%d",&x,&y);
vv.pb(mp(x,i));
vv.pb(mp(y,-i));
}
sort(ALL(vv));
int ans=vv[0].X+m-vv.back().X;
fup(i,0,2*n-2,1){
int c=vv[i+1].X-vv[i].X;
if(vv[i].Y<0 && vv[i+1].Y<0){
a[-vv[i+1].Y]+=c;
}else if(vv[i].Y<0 && vv[i+1].Y>0){
ans+=c;
}else if(vv[i].Y>0 && vv[i+1].Y<0){
if(vv[i].Y==-vv[i+1].Y)a[vv[i].Y]+=c;
else{
v[vv[i].Y].pb(mp(-vv[i+1].Y,c));
v[-vv[i+1].Y].pb(mp(vv[i].Y,c));
}
}else{
a[vv[i].Y]+=c;
}
}
fup(i,1,n,1)if(!cnt[i] && v[i].size()<=1)g(i);
MEM_1(dd);
printf("%d\n",go(0,k)+ans);
}
Compilation message (stderr)
keys.cpp: In function 'void dfs2(int, int)':
keys.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
keys.cpp:66:3: note: in expansion of macro 'fup'
66 | fup(i,1,cnt[N],1){
| ^~~
keys.cpp: In function 'void g(int)':
keys.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
keys.cpp:82:2: note: in expansion of macro 'fup'
82 | fup(i,0,cnt[N],1)VV.pb(max(d[N][i][0],d[N][i][1]));
| ^~~
keys.cpp: In function 'int go(int, int)':
keys.cpp:87:6: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | if(N==V.size())return 0;
| ~^~~~~~~~~~
keys.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
keys.cpp:92:2: note: in expansion of macro 'fup'
92 | fup(i,0,n,1){
| ^~~
keys.cpp: In function 'int main()':
keys.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
keys.cpp:100:2: note: in expansion of macro 'fup'
100 | fup(i,1,n,1){
| ^~~
keys.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
keys.cpp:108:2: note: in expansion of macro 'fup'
108 | fup(i,0,2*n-2,1){
| ^~~
keys.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
keys.cpp:124:2: note: in expansion of macro 'fup'
124 | fup(i,1,n,1)if(!cnt[i] && v[i].size()<=1)g(i);
| ^~~
keys.cpp:99:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
99 | scanf("%d%d%d",&n,&m,&k);
| ~~~~~^~~~~~~~~~~~~~~~~~~
keys.cpp:102:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
102 | scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |