#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T>>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
int n;
const int N=1e5+1;
vector<vector<int> > jar(N);
ll sol=LLONG_MAX;
bool test(int m)
{
multiset<int> veci;
multiset<int,greater<int> > manji;
ll trsol=0;
int offset=0;
for(int j=N-1;j>0;j--)
{
for(auto p:jar[j])
manji.insert(p);
while(veci.size()&&*veci.begin()-offset<=0)
veci.erase(veci.begin());
while(veci.size()<m&&manji.size())
{
veci.insert(*manji.begin()+offset);
manji.erase(manji.begin());
}
while(manji.size()&&veci.size()&&*manji.begin()>*veci.begin()-offset)
{
int a=*manji.begin();
int b=*veci.begin()-offset;
manji.erase(manji.begin());
veci.erase(veci.begin());
manji.insert(b);
veci.insert(a+offset);
}
trsol+=(ll)veci.size()*((ll)veci.size()-1)/2;
offset++;
}
while(veci.size()&&*veci.begin()-offset<=0)
veci.erase(veci.begin());
bool ret=veci.size()==0&&manji.size()==0;
if(ret){
sol=min(sol,trsol);
}
return ret;
}
int main()
{
//freopen("sails.in.1b","r",stdin);
scanf("%i",&n);
jar.resize(n);
for(int i=0;i<n;i++)
{
int a,b;
scanf("%i %i",&a,&b);
jar[a].pb(b);
}
int l=0,r=N;
while(l<r)
{
int m=(l+r)>>1;
if(test(m))
r=m;
else
l=m+1;
}
test(l);
printf("%lld\n",sol);
return 0;
}
Compilation message
sails.cpp: In function 'bool test(int)':
sails.cpp:43:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(veci.size()<m&&manji.size())
~~~~~~~~~~~^~
sails.cpp: In function 'int main()':
sails.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&n);
~~~~~^~~~~~~~~
sails.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&a,&b);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
2688 KB |
Output is correct |
2 |
Correct |
11 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
2688 KB |
Output is correct |
2 |
Correct |
12 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
2732 KB |
Output is correct |
2 |
Correct |
9 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1067 ms |
2816 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1083 ms |
3204 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1085 ms |
4992 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1074 ms |
5116 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1082 ms |
6648 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1076 ms |
7300 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1054 ms |
7104 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |