Skip to content

Instantly share code, notes, and snippets.

@nickroh
Created May 3, 2019 07:09
Show Gist options
  • Save nickroh/4336539801e7babeb5effc75712b962e to your computer and use it in GitHub Desktop.
Save nickroh/4336539801e7babeb5effc75712b962e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int n;
long long sum=0;
struct _tuple{
int x;
int y;
};
_tuple arr[1000000];
bool sf(_tuple a,_tuple b)
{
if(a.y==b.y)
{
return a.x<b.x;
}
return a.y<b.y;
}
void input()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d %d",&arr[i].x,&arr[i].y);
}
}
void arrow()
{
for(int i=0;i<n;i++)
{
int tmp=0;
if(arr[i].y==arr[i+1].y && i<n-1)
{
tmp=arr[i+1].x-arr[i].x;
}
if(arr[i].y==arr[i-1].y && i>0)
{
if(tmp>arr[i].x-arr[i-1].x || tmp==0)
{
tmp=arr[i].x-arr[i-1].x;
}
}
//printf("%d\n",tmp);
sum+=tmp;
}
}
void print()
{
for(int i=0;i<n;i++)
{
printf("%d %d\n",arr[i].x,arr[i].y);
}
}
int main()
{
input();
sort(arr,arr+n,sf);
// print();
arrow();
printf("%lld",sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment