Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.IO;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using System.Collections.Concurrent;
- using System.Threading;
- namespace slice
- {
- class Program
- {
- static void Main(string[] args)
- {
- int maxPizzaSlice;
- List<int> pizzaTypes;
- using (FileStream fs = new FileStream("TextFile1.txt", FileMode.Open))
- using (StreamReader sr = new StreamReader(fs))
- {
- string[] s = sr.ReadLine().Split(' ');
- maxPizzaSlice = int.Parse(s[0]);
- pizzaTypes = sr.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList();
- }
- int i = 0, j = 0;
- Parallel.ForEach(
- Partitioner.Create(0, pizzaTypes.Count, 10),
- () => new ValueTuple<int, int>(0, 0),
- (range, loop, tuple) =>
- {
- Console.WriteLine($"Thread: {Thread.CurrentThread.ManagedThreadId} {range.Item1} {range.Item2}");
- Solve(ref tuple, pizzaTypes.Skip(range.Item1).Take(range.Item2).ToList(), maxPizzaSlice);
- },
- (x) =>
- {
- }
- );
- //Solve(ref i, ref j, pizzaTypes, maxPizzaSlice);
- Console.WriteLine(i);
- }
- static void Solve(ref ValueTuple<int, int> tupl, List<int> list, int max, int sum = 0)
- {
- for (int i = list.Count - 1; i >= 0; i--)
- {
- if (sum + list[i] > max)
- {
- list.RemoveAt(i);
- continue;
- }
- if (sum + list[i] == max)
- return;
- if (sum + list[i] > tupl.Item2)
- tupl.Item2 = sum + list[i];
- sum += list[i];
- if (list.Count != 1)
- Solve(ref tupl, list.RemoveAtReturn(i), max, sum);
- sum -= list[i];
- list.RemoveAt(i);
- }
- tupl.Item1 = tupl.Item2;
- }
- }
- static class Extension
- {
- public static List<T> RemoveAtReturn<T>(this List<T> list, int index)
- {
- List<T> newList = new List<T>(list);
- newList.RemoveAt(index);
- return newList;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement