Pagini recente » Profil Mihnea769 | Profil Ambro | Cod sursa (job #1649232) | Profil hot_sparks27 | Cod sursa (job #797331)
Cod sursa(job #797331)
#include <cstdio>
const int MAX_SIZE(50000);
int n, best;
struct street
{
int d;
int l;
} heap [MAX_SIZE];
inline void swap (int a, int b)
{
heap[a].d ^= heap[b].d;
heap[b].d ^= heap[a].d;
heap[a].d ^= heap[b].d;
heap[a].l ^= heap[b].l;
heap[b].l ^= heap[a].l;
heap[a].l ^= heap[b].l;
}
inline void read (void)
{
std::freopen("orase.in","r",stdin);
int m;
std::scanf("%d%d",&m,&n);
for (struct street *iterator(heap), *end(heap + n) ; iterator < end ; ++iterator)
std::scanf("%d%d",&(iterator->d),&(iterator->l));
std::fclose(stdout);
}
inline void print (void)
{
std::freopen("orase.out","w",stdout);
std::printf("%d\n",best);
std::fclose(stdout);
}
inline void increase (int index, const int LIMIT)
{
int left((index << 1) + 1), right(left + 1), greatest;
while (true)
{
if (left < LIMIT && heap[left].d > heap[index].d)
greatest = left;
else
greatest = index;
if (right < LIMIT && heap[right].d > heap[greatest].d)
greatest = right;
if (greatest == index)
break;
swap(index,greatest);
index = greatest;
left = (index << 1) + 1;
right = left + 1;
}
}
inline void build_heap (void)
{
for (int index((n - 2) >> 1) ; index >= 0 ; --index)
increase(index,n);
}
inline void heap_sort (void)
{
build_heap();
int size(n - 1);
while (true)
{
swap(0,size);
--size;
if (size)
increase(0,size);
else
break;
}
}
inline void dynamic (void)
{
int max(heap->l - heap->d);
for (struct street *iterator(heap + 1), *end(heap + n) ; iterator < end ; ++iterator)
{
if (iterator->d + iterator->l + max > best)
best = iterator->d + iterator->l + max;
if (iterator->l - iterator->d > max)
max = iterator->l - iterator->d;
}
}
int main (void)
{
read();
heap_sort();
dynamic();
print();
return 0;
}