Maximise count of intersections possible from the given endpoints of a line
#include <iostream>
using
namespace
std;
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using
namespace
std;
using
namespace
__gnu_pbds;
template
<
class
T>
using
oset
= tree<T, null_type, greater_equal<T>, rb_tree_tag,
tree_order_statistics_node_update>;
void
maximumIntersection(
int
EndPoints[],
int
N)
{
oset<
int
> st;
int
ans = 0;
map<
int
,
int
> mp;
for
(
int
i = 0; i < N; i++) {
st.insert(EndPoints[i]);
ans += st.order_of_key(EndPoints[i])
+ mp[EndPoints[i]];
mp[EndPoints[i]]++;
}
if
(mp.size() == 1) {
cout <<
"Maximum Intersection Possible: "
<< ans / 2
<<
"\n"
;
}
else
{
cout <<
"Maximum Intersection Possible: "
<< ans
<<
"\n"
;
}
}
int
main()
{
int
X = 7;
int
EndPoints[] = { 4, 1, 4, 6, 7, 7, 5 };
int
Y = 5;
int
EndPoints2[] = { 2, 1, 4, 3, 5 };
int
Z = 5;
int
EndPoints3[] = { 1, 1, 1, 1, 1 };
maximumIntersection(EndPoints, X);
maximumIntersection(EndPoints2, Y);
maximumIntersection(EndPoints3, Z);
}
#include <iostream>
using
namespace
std;
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using
namespace
std;
using
namespace
__gnu_pbds;
template
<
class
T>
using
oset
= tree<T, null_type, greater_equal<T>, rb_tree_tag,
tree_order_statistics_node_update>;
void
maximumIntersection(
int
EndPoints[],
int
N)
{
oset<
int
> st;
int
ans = 0;
map<
int
,
int
> mp;
for
(
int
i = 0; i < N; i++) {
st.insert(EndPoints[i]);
ans += st.order_of_key(EndPoints[i])
+ mp[EndPoints[i]];
mp[EndPoints[i]]++;
}
if
(mp.size() == 1) {
cout <<
"Maximum Intersection Possible: "
<< ans / 2
<<
"\n"
;
}
else
{
cout <<
"Maximum Intersection Possible: "
<< ans
<<
"\n"
;
}
}
int
main()
{
int
X = 7;
int
EndPoints[] = { 4, 1, 4, 6, 7, 7, 5 };
int
Y = 5;
int
EndPoints2[] = { 2, 1, 4, 3, 5 };
int
Z = 5;
int
EndPoints3[] = { 1, 1, 1, 1, 1 };
maximumIntersection(EndPoints, X);
maximumIntersection(EndPoints2, Y);
maximumIntersection(EndPoints3, Z);
}