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>
using namespace std;
vector<int>tamDP;
vector<int>calcs;
int tam;
bool can(vector<int> dp,int x)
{
int ma, sob, i;
for(i=tam; i>0; i--)
{
ma=min(x,tamDP[i]);
sob=0;
if(dp[i]>ma)
{
sob=dp[i]-ma;
dp[i]=ma;
if(i>1)
dp[i-1]+=sob;
else
return 0;
}
}
return 1;
}
int calc(vector<int> & dp, int x)
{
int ma, sob, i, res=0;
for(i=tam; i>0; i--)
{
ma=min(x,tamDP[i]);
sob=0;
if(dp[i]>ma)
{
sob=dp[i]-ma;
dp[i]=ma;
dp[i-1]+=sob;
}
}
for(i=tam; i>0; i--)
{
res+=calcs[dp[i]];
}
return res;
}
int main()
{
int n, i, mi=0, ma=0, piv,pos;
cin >> n;
calcs.resize(n+1);
calcs[0]=0;
calcs[1]=0;
for(i=2; i<=n; i++)
{
calcs[i]+=calcs[i-1]+(i-1);
}
vector<pair<int,int>>v(n);
for(i=0; i<n; i++)
{
cin >> v[i].first >> v[i].second;
tam=max(v[i].first,tam);
}
vector<int> dp(tam+1,0);
tamDP.resize(tam+1,0);
for(i=0; i<n; i++)
{
dp[v[i].first]+=1;
dp[v[i].first-v[i].second]-=1;
tamDP[v[i].first]+=1;
}
ma=dp[tam];
for(i=tam-1; i>=0; i--)
{
dp[i]+=dp[i+1];
ma=max(ma,dp[i]);
tamDP[i]+=tamDP[i+1];
}
pos=ma;
while(mi<=ma)
{
piv=(mi+ma)/2;
if(can(dp,piv))
{
ma=piv-1;
pos=piv;
}
else
{
mi=piv+1;
}
}
cout << calc(dp,pos);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |