using Newtonsoft.Json.Linq;
using System.Collections.Generic;
public int id { get; set; }
public int location { get; set; }
public int startTime { get; set; }
public int length { get; set; }
public static void Main(string[] args)
string json = "{\"ads\":[{\"id\":54,\"location\":1,\"startTime\":0,\"length\":300},{\"id\":54,\"location\":5,\"startTime\":250,\"length\":300},{\"id\":54,\"location\":1,\"startTime\":0,\"length\":300},{\"id\":341,\"location\":2,\"startTime\":0,\"length\":300},{\"id\":12,\"location\":3,\"startTime\":0,\"length\":99},{\"id\":19,\"location\":3,\"startTime\":100,\"length\":299},{\"id\":61,\"location\":3,\"startTime\":300,\"length\":99},{\"id\":32,\"location\":4,\"startTime\":0,\"length\":149},{\"id\":591,\"location\":4,\"startTime\":150,\"length\":149}]}";
JToken input = JObject.Parse(json)["ads"];
items = input.ToObject<List<Item>>();
Dictionary<int, List<Item>> locationToItemMap = new Dictionary<int, List<Item>>();
foreach (Item item in items)
if(!locationToItemMap.ContainsKey(item.location))
locationToItemMap[item.location] = new List<Item>();
InsertElementDescending(locationToItemMap[item.location], item);
foreach(var pair in locationToItemMap)
for(int i = 0; i < pair.Value.Count - 1; ++i)
int endTime = pair.Value[i].startTime + pair.Value[i].length;
int nextStartTime = pair.Value[i + 1].startTime;
if (endTime > nextStartTime)
Console.WriteLine(pair.Value[i + 1].id + " conflicts with " + pair.Value[i + 1].id + " on location " + pair.Key);
Dictionary<int, List<Item>> idToItemMap = new Dictionary<int, List<Item>>();
foreach (Item item in items)
if (!idToItemMap.ContainsKey(item.id))
idToItemMap[item.id] = new List<Item>();
InsertElementDescending(idToItemMap[item.id], item);
foreach (var pair in idToItemMap)
for (int i = 0; i < pair.Value.Count - 1; ++i)
int endTime = pair.Value[i].startTime + pair.Value[i].length;
int nextStartTime = pair.Value[i + 1].startTime;
if (endTime > nextStartTime)
Console.WriteLine(pair.Key + " plays simultaneous in more than one location");
Console.WriteLine("fin");
public static void InsertElementDescending(List<Item> source, Item element)
int index = source.FindLastIndex(e => e.startTime > element.startTime);
if (index == 0 || index == -1)
source.Insert(0, element);
source.Insert(index + 1, element);