Codeforces Round #390 (Div. 2), problem: (D) Fedor and Coupons Solution in C/C++

Codeforces Round #390 (Div. 2), problem: (D) Fedor and Coupons Solution in C/C++

#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
ll n,k,ans,l;
priority_queue<ll>s;
struct item
{
	ll x,y,pos;
}a[333333];
bool cmp(item a, item b)
{
	return a.x<b.x;
}
int main()
{
	cin>>n>>k;
	for(ll i=1;i<=n;i++)
	{
		scanf("%lld%lld",&a[i].x,&a[i].y);
		a[i].pos=i;
	}
	sort(a+1,a+n+1,&cmp);
	for(ll i=1;i<=n;i++)
	{
		if(s.size()<k)
			s.push(-a[i].y);
		else if(-s.top()<a[i].y)
			s.pop(),s.push(-a[i].y);
		if(ans<-s.top()-a[i].x+1&&s.size()==k)
			ans=-s.top()-a[i].x+1,
			l=a[i].x;
	}
	cout<<ans<<endl;
	if(ans==0)
	{
		for(ll i=1;i<=k;i++)
			printf("%lld ",i);
		return 0;
	}
	for(ll i=1;i<=n&&k;i++)
		if(a[i].x<=l&&l+ans-1<=a[i].y)
			printf("%lld ",a[i].pos),k--;
}

Advertisement

spot_img

Is the Keurig K-Mini...

Keurig K-Mini Long-Term Review: 12 Months of Daily Brewing First-impression...

Can You Make Iced...

How to Brew Refreshing Iced Coffee with the Keurig...

Keurig K-Mini vs Keurig...

Keurig K-Mini vs Keurig K-Slim: Comparing Keurig’s Slimmest Brewers Keurig’s...

Best K-Cup Coffee Pods...

Top Rated K-Cup Coffee Pods for the Keurig K-Mini The...

How to Fix a...

How to Clear Needle Clogs and Fix Slow Flow...

Keurig K-Mini Auto-Off Feature...

Keurig K-Mini Energy Efficiency & Power Consumption Analysis As electricity...

Is the Keurig K-Mini Worth It Long Term? 1-Year Reliability, Durability & Value

Keurig K-Mini Long-Term Review: 12 Months of Daily Brewing First-impression reviews written after three days of testing can tell you how an appliance looks on...

Can You Make Iced Coffee with the Keurig K-Mini? Brew Temperature & Iced Recipes

How to Brew Refreshing Iced Coffee with the Keurig K-Mini During warm summer months, nothing satisfies quite like a tall glass of refreshing iced coffee....

Keurig K-Mini vs Keurig K-Slim: Multi-Cup Reservoir vs Ultra-Compact Brewer

Keurig K-Mini vs Keurig K-Slim: Comparing Keurig’s Slimmest Brewers Keurig’s product catalog features two popular models specifically marketed to space-conscious consumers: the ultra-compact Keurig K-Mini...

Best K-Cup Coffee Pods for Keurig K-Mini: Dark, Medium, Flavored & Decaf Roasts

Top Rated K-Cup Coffee Pods for the Keurig K-Mini The beauty of owning a Keurig K-Mini Single Serve Coffee Maker is universal pod compatibility. Unlike...

How to Fix a Clogged Keurig K-Mini: Exit Needle Cleaning & Troubleshooting Guide

How to Clear Needle Clogs and Fix Slow Flow on the Keurig K-Mini If your Keurig K-Mini suddenly dispenses half a cup of coffee, drips...

Keurig K-Mini Auto-Off Feature and Power Consumption: Energy Efficiency Tested

Keurig K-Mini Energy Efficiency & Power Consumption Analysis As electricity costs rise and consumers increasingly prioritize eco-conscious household appliances, power consumption has become an important...

Keurig K-Mini Dimensions and Cord Storage: Best Compact Coffee Maker for Dorms & RVs

Keurig K-Mini Dimensions & Space-Saving Ergonomics Countertop real estate in modern living spaces has never been tighter. In college dormitories, studio micro-apartments, home office workstations,...

How to Use Reusable K-Cup Coffee Filters with the Keurig K-Mini: Universal Pod Guide

How to Brew Fresh Ground Coffee in Your Keurig K-Mini While pre-packaged K-Cup pods deliver unbeatable morning convenience, they come with two distinct trade-offs: ongoing...

Keurig K-Mini vs Nespresso Vertuo Pop: Compact Coffee Maker Comparison

Keurig K-Mini vs Nespresso Vertuo Pop: The Battle of Compact Brewers Single-serve coffee has evolved into two dominant, competing ecosystems: the versatile, drip-style Keurig K-Cup...