Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. using System.Collections.Concurrent;
  7. using System.Threading;
  8.  
  9. namespace slice
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             int maxPizzaSlice;
  16.             List<int> pizzaTypes;
  17.  
  18.  
  19.             using (FileStream fs = new FileStream("TextFile1.txt", FileMode.Open))
  20.             using (StreamReader sr = new StreamReader(fs))
  21.             {
  22.                 string[] s = sr.ReadLine().Split(' ');
  23.  
  24.                 maxPizzaSlice = int.Parse(s[0]);
  25.  
  26.                 pizzaTypes = sr.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList();
  27.             }
  28.  
  29.  
  30.  
  31.             int i = 0, j = 0;
  32.  
  33.             Parallel.ForEach(
  34.                 Partitioner.Create(0, pizzaTypes.Count, 10),
  35.                 () => new ValueTuple<int, int>(0, 0),
  36.                 (range, loop, tuple) =>
  37.                 {
  38.                     Console.WriteLine($"Thread: {Thread.CurrentThread.ManagedThreadId} {range.Item1} {range.Item2}");
  39.                     Solve(ref tuple, pizzaTypes.Skip(range.Item1).Take(range.Item2).ToList(), maxPizzaSlice);
  40.  
  41.                 },
  42.                 (x) =>
  43.                 {
  44.                    
  45.                 }
  46.                 );
  47.  
  48.             //Solve(ref i, ref j, pizzaTypes, maxPizzaSlice);
  49.  
  50.             Console.WriteLine(i);
  51.         }
  52.  
  53.         static void Solve(ref ValueTuple<int, int> tupl, List<int> list, int max, int sum = 0)
  54.         {
  55.  
  56.             for (int i = list.Count - 1; i >= 0; i--)
  57.             {
  58.  
  59.                 if (sum + list[i] > max)
  60.                 {
  61.                     list.RemoveAt(i);
  62.                     continue;
  63.                 }
  64.  
  65.                 if (sum + list[i] == max)
  66.                     return;
  67.  
  68.                 if (sum + list[i] > tupl.Item2)
  69.                     tupl.Item2 = sum + list[i];
  70.  
  71.                 sum += list[i];
  72.  
  73.                 if (list.Count != 1)
  74.                     Solve(ref tupl, list.RemoveAtReturn(i), max, sum);
  75.  
  76.                 sum -= list[i];
  77.                 list.RemoveAt(i);
  78.  
  79.             }
  80.  
  81.             tupl.Item1 = tupl.Item2;
  82.         }
  83.     }
  84.  
  85.     static class Extension
  86.     {
  87.         public static List<T> RemoveAtReturn<T>(this List<T> list, int index)
  88.         {
  89.             List<T> newList = new List<T>(list);
  90.             newList.RemoveAt(index);
  91.             return newList;
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement