// [SolutionMethod] public TAnswer TotalBruteForce(TSample Sample) { ArrayManipulationSample sample = Sample as ArrayManipulationSample; Int32 n = sample.n; Int32[][] queries = sample.queries; Int32 m = queries.Length; Int64[] vals = new Int64[n]; for (int i = 0; i < m; i++) { for (int j = queries[i][0] - 1; j <= queries[i][1] - 1; j++) { vals[j] += queries[i][2]; } } Int64 MaxValue = Int64.MinValue; for (int i = 0; i < n; i++) { if (vals[i] > MaxValue) { MaxValue = vals[i]; } } return(new ArrayManipulationAnswer() { MaxValue = MaxValue }); }
public TAnswer BF(TSample Sample) { sherlockAndAnagramsSample sample = Sample as sherlockAndAnagramsSample; String s = sample.s; //HashSet<String> Subs = new HashSet<string>(); Dictionary <String, Int32> Subs = new Dictionary <String, Int32>(); Int32 result = 0; for (int i = 0; i <= s.Length - 1; i++) { for (int j = i + 1; j <= s.Length; j++) { char[] ss = s.Substring(i, j - i).ToArray(); Array.Sort(ss); String SortedSubString = new string(ss); if (Subs.ContainsKey(SortedSubString)) { Subs[SortedSubString]++; result += Subs[SortedSubString]; } else { Subs.Add(SortedSubString, 0); } } } return(new sherlockAndAnagramsAnswer() { result = result }); }
public TAnswer BruteForce(TSample Sample) { MinimumSwaps2Sample sample = Sample as MinimumSwaps2Sample; Int32[] arr = sample.arr; Int32[] Positions = new Int32[arr.Length]; for (int i = 0; i < arr.Length; i++) { Positions[arr[i] - 1] = i; } Int32 SwapsCount = 0; for (int i = 0; i < arr.Length; i++) { Int32 CurrentNumberPosition = Positions[i]; if (CurrentNumberPosition != i) { SwapsCount++; arr[CurrentNumberPosition] = arr[i]; Positions[arr[i] - 1] = CurrentNumberPosition; arr[i] = i; Positions[i] = i; } } return(new MinimumSwaps2Answer() { result = SwapsCount }); }
public TAnswer Greedy(TSample Sample) { GreedyFloristSample sample = Sample as GreedyFloristSample; Int32 n = sample.n; Int32 k = sample.k; Int32[] c = sample.c; Int32 result = 0; Array.Sort(c); if (k >= n) { for (int i = 0; i < n; i++) { result += c[i]; } } else { for (int i = c.Length - 1; i >= 0; i--) { Int32 mult = (c.Length - 1 - i) / k + 1; result += mult * c[i]; } } return(new GreedyFloristAnswer() { result = result }); }
public TAnswer Solution2(TSample Sample) { SamAndSubstringsSample sample = Sample as SamAndSubstringsSample; Int32 modulus = 1000000007; byte[] nb = NumStringToByteArray(sample.s); Int64[] B = new Int64[sample.s.Length]; Int64[] T = new Int64[sample.s.Length]; B[0] = nb[0]; T[0] = B[0]; for (int i = 1; i < sample.s.Length; i++) { B[i] = (B[i - 1] * 10 + (i + 1) * nb[i]) % modulus; T[i] = (T[i - 1] + B[i]) % modulus; } return(new SamAndSubstringsAnswer() { result = (Int32)T[nb.Length - 1] }); }
public TAnswer Greedy(TSample Sample) { GreedyMaxMinSample sample = Sample as GreedyMaxMinSample; Int32 k = sample.k; Int32[] arr = sample.arr; Array.Sort(arr); Int32 result = arr[k - 1] - arr[0]; for (int i = 0; i <= arr.Length - k; i++) { if (arr[i + k - 1] - arr[i] < result) { result = arr[i + k - 1] - arr[i]; } } return(new GreedyMaxMinAnswer() { result = result }); }
public TAnswer Stub(TSample Sample) { TemplateProblemSample sample = Sample as TemplateProblemSample; return(new TemplateProblemAnswer() { }); }
public TAnswer Solution2(TSample Sample) { Int32[] a = (Sample as PrimeXORSample).a; Int32 modulus = 1000000007; Int32 m = (1 << (int)(Math.Log(a.Max(), 2) + 1)) - 1; var primes = GetPrimes(m); Int64 count = 0; Int32[] counts = new Int32[a.Max() + 1]; foreach (var val in a) { counts[val]++; } a = a.Distinct().ToArray(); Int32 n = a.Length; Int64[,] dp = new Int64[n + 1, m + 1]; dp[0, 0] = 1; for (Int32 i = 1; i <= n; i++) { for (int j = 0; j <= m; j++) { dp[i, j] = (((dp[i - 1, j] * (1 + counts[a[i - 1]] / 2))) + ((dp[i - 1, j ^ a[i - 1]] * ((counts[a[i - 1]] + 1) / 2)))) % modulus ; } } for (int i = 0; i < primes.Count; i++) { checked { count = (count + (dp[n, primes[i]])) % modulus; } } return(new PrimeXORAnswer() { result = (Int32)count }); }
public TAnswer DP(TSample Sample) { SpecialStringAgainSample sample = Sample as SpecialStringAgainSample; String s = sample.s; Int64 result = 0; Int64[] F = new Int64[s.Length + 1]; F[0] = 1; F[1] = 1; Char CurrentChar = s[0]; Int64 cc = 1; Boolean OneChar = true; for (int i = 1; i < s.Length; i++) { F[i + 1] = F[i] + i + 1; if (s[i] == CurrentChar) { cc++; } else { OneChar = false; int j = i + 1; result += F[cc]; while (j < s.Length && s[j] == CurrentChar) { j++; } int cc2 = j - i - 1; if (cc2 > 0) { result += Math.Min(cc, cc2); } CurrentChar = s[i]; cc = 1; } } if (OneChar) { result = F[s.Length]; } else { result += F[cc]; } return(new SpecialStringAgainAnswer() { result = result }); }
public TAnswer Solution2(TSample Sample) { AbbreviateSample sample = Sample as AbbreviateSample; return(new AbbreviateAnswer() { Answer = abbreviation2(sample.A, sample.B) }); }
public TAnswer TotalBruteForce(TSample Sample) { FrequencyQueriesSample sample = Sample as FrequencyQueriesSample; List <List <int> > queries = sample.queries; List <Int32> result = new List <int>(); Dictionary <Int32, Int32> data = new Dictionary <int, int>(); for (int i = 0; i < queries.Count; i++) { if (queries[i][0] == 1) { Int32 AddedValue = queries[i][1]; if (data.ContainsKey(AddedValue)) { data[AddedValue]++; } else { data.Add(AddedValue, 1); } } if (queries[i][0] == 2) { Int32 DeletedValue = queries[i][1]; if (data.ContainsKey(DeletedValue)) { if (data[queries[i][1]] > 0) { data[queries[i][1]]--; } } } if (queries[i][0] == 3) { if (data.ContainsValue(queries[i][1])) { result.Add(1); } else { result.Add(0); } } } return(new FrequencyQueriesAnswer() { answer = result }); }
public TAnswer stub(TSample Sample) { CountingInversionsSample sample = Sample as CountingInversionsSample; Int32[] arr = sample.arr; Int64 result = mergeSort(arr, 0, arr.Length - 1); return(new CountingInversionsAnswer() { result = result }); }
public TAnswer SumsArray(TSample Sample) { MaximumSubarraySumSample sample = Sample as MaximumSubarraySumSample; Int64[] arr = sample.arr; Int64 m = sample.m; Int64[] sums = new Int64[arr.Length]; Int64 result = 0; Int64 temp = 0; for (int i = 0; i < arr.Length; i++) { arr[i] = arr[i] % m; temp = (arr[i] + temp) % m; sums[i] = temp; if (temp > result) { result = temp; } if (arr[i] > result) { result = arr[i]; } } Int32[] idx = Enumerable.Range(0, sums.Length).ToArray(); QS(sums, 0, sums.Length - 1, idx); for (int i = 0; i < sums.Length - 1; i++) { if (idx[i] > idx[i + 1]) { temp = (sums[i] - sums[i + 1] + m) % m; if (temp > result) { result = temp; } } } return(new MaximumSubarraySumAnswer() { result = result }); }
//[SolutionMethod] public TAnswer DP3(TSample Sample) { BeautifulQuadruplesSample sample = Sample as BeautifulQuadruplesSample; Array.Sort(sample.arr); Int32 a = sample.arr[0]; Int32 b = sample.arr[1]; Int32 c = sample.arr[2]; Int32 d = sample.arr[3]; Int32 MaxXor = (1 << (int)(Math.Log(d, 2) + 1)) - 1; Int32[,] cnt = new Int32[MaxXor + 1, MaxXor + 1]; Int32[] tot = new Int32[MaxXor + 1]; Int32 ans = 0; for (int i = 1; i <= a; ++i) { for (int j = i; j <= b; ++j) { ++cnt[i ^ j, j]; ++tot[j]; } } for (int i = 0; i < MaxXor; ++i) { for (int j = 1; j < MaxXor; ++j) { cnt[i, j] += cnt[i, j - 1]; } if (i > 0) { tot[i] += tot[i - 1]; } } for (int i = 1; i <= c; ++i) { for (int j = i; j <= d; ++j) { ans += tot[i] - cnt[i ^ j, i]; } } return(new BeautifulQuadruplesAnswer() { result = ans }); }
//[SolutionMethod] public TAnswer MS2(TSample Sample) { CountingInversionsSample sample = Sample as CountingInversionsSample; Int32[] arr = new Int32[sample.arr.Length]; Array.Copy(sample.arr, arr, arr.Length); Int64 result = MSS(arr, 0, arr.Length - 1, 0); return(new CountingInversionsAnswer() { result = result }); }
public TAnswer BruteForce(TSample Sample) { NewYearChaosSample sample = Sample as NewYearChaosSample; Int32[] q = sample.q; Int32[] Positions = new Int32[q.Length + 1]; for (int i = 1; i < q.Length + 1; i++) { Positions[q[i - 1]] = i; } Int32[] Bribes = new Int32[q.Length + 1]; Int32 TotalBribes = 0; for (int i = 1; i < q.Length + 1; i++) { Int32 CurrentPersonPosition = Positions[i]; while (CurrentPersonPosition != i) { Int32 LeftPerson = q[CurrentPersonPosition - 2]; if (Bribes[LeftPerson] < 2) { Bribes[LeftPerson]++; } else { return(new NewYearChaosAnswer() { ans = "Too chaotic" }); } //swap Positions[i] = CurrentPersonPosition - 1; Positions[LeftPerson] = CurrentPersonPosition; q[CurrentPersonPosition - 2] = i; q[CurrentPersonPosition - 1] = LeftPerson; TotalBribes++; CurrentPersonPosition--; } } return(new NewYearChaosAnswer() { ans = TotalBribes.ToString() }); }
public TAnswer BruteForce(TSample Sample) { IceCreamParlorSample sample = Sample as IceCreamParlorSample; Int32[] cost = sample.cost; Int32 money = sample.money; Int32[] result = new Int32[2]; Int32[] idx = Enumerable.Range(0, cost.Length).ToArray(); for (int i = 0; i < cost.Length; i++) { idx[i] = i + 1; } QS(cost, 0, cost.Length - 1, idx); Int32 LeftPtr = 0; Int32 RightPtr = cost.Length - 1; while (cost[RightPtr] + cost[LeftPtr] > money) { RightPtr--; } for (int i = RightPtr; i > LeftPtr; i--) { for (int j = LeftPtr; j < i; j++) { if (cost[i] + cost[j] == money) { //Console.WriteLine($"{cost[j]} {cost[i]} "); //Console.WriteLine($"{Math.Min(idx[i], idx[j])} {Math.Max(idx[i], idx[j])}"); return(new IceCreamParlorAnswer() { arr = new Int32[] { Math.Min(idx[i], idx[j]), Math.Max(idx[i], idx[j]) } }); } } } return(new IceCreamParlorAnswer() { arr = result }); }
public TAnswer test1(TSample Sample) { ArrayManipulationSample sample = Sample as ArrayManipulationSample; Int32 n = sample.n; Int32[][] queries = sample.queries; Int32 m = queries.Length; Int64[] numList = new Int64[n + 1]; for (int i = 0; i < m; i++) { Int64 a = queries[i][0]; Int64 b = queries[i][1]; Int64 k = queries[i][2]; numList[a] += k; if (b + 1 <= n) { numList[b + 1] -= k; } } long tempMax = 0; long max = 0; for (int i = 1; i <= n; i++) { tempMax += numList[i]; if (tempMax > max) { max = tempMax; } } return(new ArrayManipulationAnswer() { MaxValue = max }); }
public TAnswer Second(TSample Sample) { MRotateSample sample = Sample as MRotateSample; List <List <Int64> > arr = new List <List <Int64> >(sample.matrix); Int32 r = sample.r; Int32 Rings = Math.Min(arr.Count, arr[0].Count) / 2; for (int j = 0; j < Rings; j++) { RotateRing3(arr, j, r); } MRotateAnswer ans = new MRotateAnswer() { matrix = arr }; return(ans); }
public TAnswer DP(TSample Sample) { CoinChangeSample sample = Sample as CoinChangeSample; Int64[] c = sample.c; Int32 n = sample.n; Int64 result = 0; Array.Sort(c); Int64 MaxCoin = c[c.Length - 1]; Int64[,] DP = new Int64[c.Length + 1, n + 1]; for (int i = 1; i <= c.Length; i++) { for (int j = 1; j <= n; j++) { DP[i, j] = DP[i - 1, j]; if (j % c[i - 1] == 0) { DP[i, j]++; } if (j - c[i - 1] > 0) { for (int k = 1; k <= j / c[i - 1]; k++) { DP[i, j] += DP[i - 1, j - c[i - 1] * k]; } } } } result = DP[c.Length, n]; return(new CoinChangeAnswer() { w = result }); }
public TAnswer Solution1(TSample Sample) { FBModifiedSample sample = Sample as FBModifiedSample; Int32 n = sample.n; Int32 t1 = sample.t1; Int32 t2 = sample.t2; BigInteger[] F = new BigInteger[n]; F[0] = t1; F[1] = t2; for (int i = 2; i < n; i++) { F[i] = F[i - 2] + F[i - 1] * F[i - 1]; } return(new FBModifiedAnswer() { result = F[n - 1].ToString() }); }
public TAnswer BF(TSample Sample) { ReverseShuffleMergeSample sample = Sample as ReverseShuffleMergeSample; String s = sample.s; char[] result = new Char[s.Length / 2]; Int32 aPos = 'a'; Int32 zPos = 'z'; Int32[] cnt = new Int32[zPos - aPos + 1]; Int32 UniqueLetters = 0; for (int i = 0; i < s.Length; i++) { cnt[s[i] - aPos]++; } Int32[] ap = new Int32[cnt.Length]; for (int i = 0; i < cnt.Length; i++) { ap[i] = cnt[i] / 2; if (cnt[i] != 0) { UniqueLetters++; } } Int32 ZeroesCount = 0; Int32 ptr = 0; for (int i = 0; i < s.Length; i++) { ap[s[i] - aPos]--; if (ap[s[i] - aPos] == 0) { ZeroesCount++; } if (UniqueLetters == ZeroesCount) { ptr = i; break; } } Char min = 'z'; Int32 MinPtr = ptr; for (int i = ptr; i < s.Length; i++) { if (min >= s[i]) { min = s[i]; MinPtr = i; } } result[0] = min; Int32[] ReverseCnt = new Int32[cnt.Length]; Int32[] ShuffleCnt = new Int32[cnt.Length]; ReverseCnt[min - aPos]++; for (int i = MinPtr + 1; i < s.Length; i++) { ShuffleCnt[s[i] - aPos]++; } Int32 pos = 1; Int32 iptr = MinPtr - 1; while (iptr >= 0 && pos != result.Length) { if (ReverseCnt[s[iptr] - aPos] == cnt[s[iptr] - aPos] / 2) { ShuffleCnt[s[iptr] - aPos]++; iptr--; continue; } if (ShuffleCnt[s[iptr] - aPos] == cnt[s[iptr] - aPos] / 2) { result[pos] = s[iptr]; ReverseCnt[s[iptr] - aPos]++; iptr--; pos++; continue; } Int32[] tempCnt = new Int32[ShuffleCnt.Length]; Array.Copy(ShuffleCnt, tempCnt, ShuffleCnt.Length); Int32 NewMinPtr = iptr; for (int j = iptr; j >= 0; j--) { if (ReverseCnt[s[j] - aPos] < cnt[s[j] - aPos] / 2 && s[NewMinPtr] > s[j]) { NewMinPtr = j; } if (tempCnt[s[j] - aPos] == cnt[s[j] - aPos] / 2) { break; } tempCnt[s[j] - aPos]++; } for (int j = iptr; j > NewMinPtr; j--) { ShuffleCnt[s[j] - aPos]++; } ReverseCnt[s[NewMinPtr] - aPos]++; result[pos] = s[NewMinPtr]; pos++; iptr = NewMinPtr - 1; } return(new ReverseShuffleMergeAnswer() { result = new string(result) }); }
public TAnswer DP(TSample Sample) { CommonChildSample sample = Sample as CommonChildSample; Int32 result = 0; String s1 = sample.s1; String s2 = sample.s2; Int32[,] DP = new Int32[s1.Length + 1, s2.Length + 1]; /* * Console.WriteLine(); * for (int i = 0; i < s1.Length; i++) * { * String str = i.ToString().PadLeft(2); * Console.Write($"{str} "); * } * * Console.WriteLine(); */ for (int i = 0; i < s1.Length; i++) { for (int j = 0; j < s2.Length; j++) { if (s1[i] == s2[j]) { DP[i, j] = 1; } } } for (int j = s2.Length - 1; j >= 0; j--) { for (int i = s1.Length - 1; i >= 0; i--) { Int32 M = Math.Max(DP[i + 1, j], DP[i + 1, j + 1] + DP[i, j]); M = Math.Max(M, DP[i, j + 1]); //DP[i, j] = Math.Max(DP[i+1,j], DP[i+1, j+1]+ DP[i,j]); DP[i, j] = M; if (DP[i, j] > result) { result = DP[i, j]; } } /* * for (int i = 0; i < s1.Length; i++) * { * Console.Write($"{DP[i, j].ToString().PadLeft(2)} "); * } * Console.WriteLine(); */ } /* * for (int i = 0; i < s1.Length; i++) * { * if (DP[i, s2.Length] > result) * { * result = DP[s1.Length-1, i]; * } * } */ return(new CommonChildAnswer() { result = result }); }
public TAnswer BF3(TSample Sample) { RKSumSample sample = Sample as RKSumSample; Int32 n = sample.N; Int64 k = sample.K; Int64[] s = sample.seq; List <Int64> KnownValues = new List <Int64>(); List <Tuple <Int64, List <Int64> > > KSumsWN = new List <Tuple <long, List <long> > >(); //ez way if (1 + (n - 1) * k == s.Length) { for (int i = 0; i < n; i++) { KnownValues.Add(s[i * k] / k); } } else { Int64 a1 = s[0] / k; Int64 a2 = s[1] - a1 * (k - 1); Int64 a3 = s[s.Length - 1] / k; Int64 a4 = s[s.Length - 1 - 1] - a3 * (k - 1); if (n == 3) { return(new RKSumAnswer() { a = new Int64[3] { a1, a2, a3 } }); } if (n == 2) { return(new RKSumAnswer() { a = new Int64[2] { a1, a2 } }); } if (n == 1) { return(new RKSumAnswer() { a = new Int64[1] { a1 } }); } KnownValues.Add(a1); KSumsWN.Add(new Tuple <long, List <long> >(a1, new List <long>() { a1 *k })); List <List <Int64> > Counts = new List <List <Int64> >() { new List <Int64>() { k } }; for (int i = 1; i < n; i++) { Counts.Add(new List <Int64>()); for (int j = 0; j < Counts[i - 1].Count; j++) { for (int l = 0; l < Counts[i - 1][j]; l++) { Counts[i].Add(l + 1); } } } Stopwatch sw = new Stopwatch(); for (int i = 1; i < s.Length; i++) { if (s[i] != 0) { Int64 next = s[i] - a1 * (k - 1); KnownValues.Add(next); if (KnownValues.Count == n) { break; } List <Int64> NewKSums = new List <long>(); Int64 PrevValue = KnownValues[KnownValues.Count - 2]; List <Int64> PrevKSums = KSumsWN[KnownValues.Count - 2].Item2; var CurrentCounts = Counts[KnownValues.Count - 2]; Console.WriteLine($"Generating combinations for {next}"); sw.Start(); for (int j = 0; j < PrevKSums.Count; j++) { Int64 PrevSum = PrevKSums[j]; Int64 ReplaceCount = CurrentCounts[j]; for (int m = 0; m < ReplaceCount; m++) { NewKSums.Add(PrevSum - PrevValue * (m + 1) + next * (m + 1)); } } sw.Stop(); Console.WriteLine($"{NewKSums.Count} combinations generated for {next} by {sw.Elapsed}"); KSumsWN.Add(new Tuple <long, List <long> >(next, NewKSums)); Console.WriteLine($"clearing s"); sw.Start(); /* * for (int j = 0; j < NewKSums.Count; j++) * { * Boolean ValueWasSkipped = false; * for (int ptr = i; ptr < s.Length; ptr++) * { * if (s[ptr] > NewKSums[j]) * { * break; * } * if (s[ptr] == NewKSums[j]) * { * s[ptr] = 0; * break; * } * * if (s[ptr] < NewKSums[j] ) * { * ValueWasSkipped = true; * } * * if (!ValueWasSkipped) * { * i = ptr; * } * } * * if (ValueWasSkipped && KnownValues.Count == n - 1 && j == NewKSums.Count-1) * { * break; * } * } */ List <Int64> temp = new List <Int64>(NewKSums); Int64 MaxSum = NewKSums[NewKSums.Count - 1]; for (int ptr = i; ptr < s.Length; ptr++) { if (s[ptr] == 0) { continue; } Int32 index = temp.IndexOf(s[ptr]); if (index != -1) { s[ptr] = 0; temp.RemoveAt(index); } else { if (KnownValues.Count == n - 1) { break; } } if (s[ptr] > MaxSum) { break; } } sw.Stop(); Console.WriteLine($"s cleared in {sw.Elapsed}"); } } if (n > KnownValues.Count) { Console.WriteLine("Need to find doubles"); } } return(new RKSumAnswer() { a = KnownValues.ToArray() }); }
//[SolutionMethod] public TAnswer OpDP(TSample Sample) { MaximumSubarraySumSample sample = Sample as MaximumSubarraySumSample; Int64[] arr = sample.arr; Int64 m = sample.m; Int32 nc = 0; Int64 maxm = 0; for (int i = 0; i < arr.Length; i++) { arr[nc] = arr[i] % m; if (arr[nc] != 0) { if (maxm < arr[nc]) { maxm = arr[nc]; } nc++; } } Int64 result = maxm; if (result != m - 1) { Int64[] v = new Int64[arr.Length]; Int32 vc = 1; v[0] = arr[0]; for (int i = 1; i < nc; i++) { Int32 vptr = -1; for (int j = 0; j < vc; j++) { Int64 ts = v[j] + arr[i]; if (ts == m) { continue; } if (ts > m) { ts = ts - m; } vptr++; v[vptr] = ts; if (v[vptr] > result) { result = v[vptr]; } } vptr++; v[vptr] = arr[i]; vc = vptr + 1; } Int64[] vcd = v.Take(vc).Distinct().ToArray(); Console.WriteLine($"vc = {vc} vcd count = {vcd.Count()}"); } return(new MaximumSubarraySumAnswer() { result = result }); }
public TAnswer DP(TSample Sample) { sherlockAndAnagramsSample sample = Sample as sherlockAndAnagramsSample; String s = sample.s; Int32 result = 0; Dictionary <Char, Int32> Counts = new Dictionary <Char, Int32>(); Int32[] Stats = new Int32[s.Length + 1]; //Int32 MaxLen = s.Length / 2; Int32 MaxLen = s.Length - 1; Int32[] F = new Int32[s.Length]; F[0] = 1; F[1] = 1; Int32 Counter = 0; foreach (var c in s) { if (Counts.ContainsKey(c)) { Counts[c]++; } else { Counts.Add(c, 1); } Stats[Counts[c]]++; if (Counter > 1) { F[Counter] = F[Counter - 1] + Counter; } Counter++; } Int32[,] DP = new Int32[Counts.Count + 1, MaxLen + 1]; Counter = 1; foreach (var c in Counts.Keys) { DP[Counter, 1] = (Counts[c] - 1) * Counts[c] / 2; result += DP[Counter, 1]; Counter++; } Counter = 1; foreach (var c in Counts.Keys) { for (int i = 2; i <= MaxLen; i++) { DP[Counter, i] = DP[Counter - 1, i]; Int32 SCI = Counts[c] - i + 1; if (SCI > 1) { Int32 ACount = F[SCI - 1]; DP[Counter, i] += ACount; } for (int j = 1; j <= i - 1; j++) { DP[Counter, i] += DP[Counter, j] * DP[Counter - 1, i - j]; } } Counter++; } for (int i = 2; i <= MaxLen; i++) { result += DP[Counter - 1, i]; } return(new sherlockAndAnagramsAnswer() { result = result }); }
public TAnswer DP2(TSample Sample) { BeautifulQuadruplesSample sample = Sample as BeautifulQuadruplesSample; //sampleInfo(sample); Array.Sort(sample.arr); Int32 a = sample.arr[0]; Int32 b = sample.arr[1]; Int32 c = sample.arr[2]; Int32 d = sample.arr[3]; Int64[] F = new Int64[d + 1]; F[0] = 0; for (int i = 1; i <= d; i++) { F[i] = F[i - 1] + i; } Int64[,] SB = new Int64[b + 1, 2]; SB[1, 0] = d * c - F[c - 1]; SB[1, 1] = SB[1, 0]; for (int i = 2; i <= b; i++) { SB[i, 0] = d * c - F[c - 1] - (d * (i - 1) - F[i - 2]); SB[i, 1] = SB[i - 1, 1] + SB[i, 0]; } Int64[,] SA = new Int64[a + 1, 2]; SA[1, 0] = SB[b, 1]; SA[1, 1] = SB[b, 1]; for (int i = 2; i <= a; i++) { SA[i, 0] = SA[i - 1, 0] - SB[i - 1, 0]; SA[i, 1] = SA[i - 1, 1] + SA[i, 0]; } Int64 Nulls = a * c - F[a - 1]; Int64 result = SA[a, 1] - Nulls; Int32 MaxXor = (1 << (int)(Math.Log(d, 2) + 1)) - 1; Int32[,] counts1 = new Int32[MaxXor + 1, b + 1]; for (int i = 1; i <= a; i++) { for (int j = i + 1; j <= b; j++) { counts1[i ^ j, j]++; } } Int32[,] counts2 = new Int32[MaxXor + 1, c + 1]; for (int i = 1; i <= c; i++) { for (int j = i + 1; j <= d; j++) { counts2[i ^ j, i]++; } } for (int i = 1; i <= MaxXor; i++) { for (int j = c; j >= 1; j--) { counts2[i, j - 1] += counts2[i, j]; } } for (int i = 1; i <= MaxXor; i++) { for (int j = 1; j <= b; j++) { result -= counts2[i, j] * counts1[i, j]; } } return(new BeautifulQuadruplesAnswer() { result = result }); }
public TAnswer BruteForce(TSample Sample) { AlmostSortedSample sample = Sample as AlmostSortedSample; Int32[] arr = sample.arr; if (arr.Length == 1) { return(new AlmostSortedAnswer() { Verdict = "yes", Answer = "" }); } if (arr.Length == 2) { String ans = ""; if (arr[1] < arr[0]) { ans = $"swap 1 2"; } return(new AlmostSortedAnswer() { Verdict = "yes", Answer = ans }); } List <Int32> pop = new List <Int32>(); if (arr[1] < arr[0]) { pop.Add(0); } Boolean poprowbroken = false; for (int i = 1; i < arr.Length - 1; i++) { if (arr[i] > arr[i + 1]) { pop.Add(i); if (!poprowbroken && pop.Count > 1 && pop[pop.Count - 1] - pop[pop.Count - 2] != 1) { poprowbroken = true; } } } if (pop.Count == 0) { return(new AlmostSortedAnswer() { Verdict = "yes", Answer = "" }); } if (pop.Count == 1) { if (pop[0] == 0) { if (arr[0] < arr[2]) { return(new AlmostSortedAnswer() { Verdict = "yes", Answer = $"swap {1} {2}" }); } } else { if (pop[0] == arr.Length - 2) { if (arr[arr.Length - 1] > arr[pop[0] - 1]) { return(new AlmostSortedAnswer() { Verdict = "yes", Answer = $"swap {pop[0] + 1} {pop[0] + 2}" }); } } else { if (arr[pop[0] + 1] > arr[pop[0] - 1] && arr[pop[0]] < arr[pop[0] + 2]) { return(new AlmostSortedAnswer() { Verdict = "yes", Answer = $"swap {pop[0] + 1} {pop[0] + 2}" }); } } } return(new AlmostSortedAnswer() { Verdict = "no", Answer = "" }); } if (pop.Count == 2) { Boolean Swap1LeftOk = true; Boolean Swap1RightOk = true; for (int i = 0; i < 4; i++) { Int32 pop0 = pop[0] + i % 2; Int32 pop1 = pop[1] + i / 2; if (pop0 != 0) { Swap1LeftOk = arr[pop0 - 1] < arr[pop1]; } Swap1RightOk = arr[pop0 + 1] > arr[pop1]; Boolean Swap2LeftOk = true; Boolean Swap2RightOk = true; if (pop0 != arr.Length - 1) { Swap2RightOk = arr[pop1 + 1] > arr[pop0]; } Swap2LeftOk = arr[pop1 - 1] < arr[pop0]; if (Swap1LeftOk && Swap2LeftOk && Swap2RightOk && Swap1RightOk) { return(new AlmostSortedAnswer() { Verdict = "yes", Answer = $"swap {pop0 + 1} {pop1 + 1}" }); } } return(new AlmostSortedAnswer() { Verdict = "no", Answer = $"" }); } if (pop.Count > 2 && !poprowbroken) { return(new AlmostSortedAnswer() { Verdict = "yes", Answer = $"reverse {pop[0] + 1} {pop[pop.Count - 1] + 2}" }); } return(new AlmostSortedAnswer() { Verdict = "no", Answer = "" }); }
public TAnswer BruteForce(TSample Sample) { FrequencyQueriesSample sample = Sample as FrequencyQueriesSample; List <List <int> > queries = sample.queries; List <Int32> result = new List <int>(); Dictionary <Int32, Int32> data = new Dictionary <int, int>(); Dictionary <Int32, Int32> fq = new Dictionary <int, int>(); for (int i = 0; i < queries.Count; i++) { Int32 OpValue = queries[i][1]; if (queries[i][0] == 1) { if (data.ContainsKey(OpValue)) { data[OpValue]++; } else { data.Add(OpValue, 1); } if (fq.ContainsKey(data[OpValue])) { fq[data[OpValue]]++; } else { fq.Add(data[OpValue], 1); } if (data[OpValue] - 1 > 0) { fq[data[OpValue] - 1]--; } } if (queries[i][0] == 2) { if (data.ContainsKey(OpValue) && data[OpValue] > 0) { fq[data[OpValue]]--; data[OpValue]--; if (data[OpValue] > 0) { fq[data[OpValue]]++; } } } Int32[] a = new Int32[3]; if (queries[i][0] == 3) { if (fq.ContainsKey(OpValue) && fq[OpValue] > 0) { result.Add(1); } else { result.Add(0); } } } return(new FrequencyQueriesAnswer() { answer = result }); }
public TAnswer DP(TSample Sample) { PowerSumSample sample = Sample as PowerSumSample; Int32 N = sample.N; Int32 X = sample.X; Int32 result = 0; List <Int32> Pows = new List <Int32>() { 0, 1 }; Int32 SummPows = 1; Int32 MaxAddendumCount = 2; Int32 Counter = 2; while (true) { Int32 pow = (Int32)Math.Pow(Counter, N); if (pow == X) { result++; } SummPows += pow; Pows.Add(pow); if (pow >= X) { break; } if (SummPows <= X) { MaxAddendumCount++; } Counter++; } Int32[,] DP = new Int32[Pows.Count + 1, Pows[Pows.Count - 1] + 1]; /* * for (int i = 0; i < Pows.Count; i++) * { * DP[1, Pows[i]] = 1; * } */ DP[0, Pows[0]] = 1; for (int i = 1; i < Pows.Count; i++) { for (int j = 0; j <= X; j++) { if (Pows[i] <= j) { DP[i, j] = DP[i - 1, j] + DP[i - 1, j - Pows[i]]; } else { DP[i, j] = DP[i - 1, j]; } } } result = DP[Pows.Count - 1, X]; return(new PowerSumAnswer() { ps = result }); }