├── YAC └── c1 │ └── f ├── NOIP ├── 2015 │ └── P5.cpp ├── 2016 │ ├── f │ └── toy.cpp ├── 公交换乘.cpp ├── 海港.cpp ├── 过河卒.cpp └── 方格取数.cpp ├── codeforces ├── div 3 │ ├── 693 │ │ ├── Long Jumps.cpp │ │ ├── Fair Division.cpp │ │ └── Cards for Friend.cpp │ ├── 702 │ │ ├── Sum of Cubes.cpp │ │ ├── Dense Array.cpp │ │ └── Balanced Remainder.cpp │ ├── 713 │ │ ├── Short Task.cpp │ │ └── Education.cpp │ ├── 748 │ │ └── A.java │ ├── 805 │ │ └── F │ └── 888 │ │ ├── A.cpp │ │ ├── B_Parity_Sort.py │ │ ├── A_Escalator_Conversations.py │ │ └── C_Tiles_Comeback.py ├── interactive │ └── guess_the_sum.cpp └── div 2 │ ├── 105 │ └── D.cpp │ └── 740 │ └── D2.cpp ├── Google Kickstart └── 21 │ ├── Round H │ └── f │ └── Round B │ ├── Problem A.cpp │ └── Problem C.cpp ├── CCC ├── 2000 │ ├── 00S4.cpp │ └── J2.java ├── 2001 │ ├── S3.py │ └── 01S3.cpp ├── 2003 │ └── S2.cpp ├── 2008 │ └── 08S5.cpp ├── 2009 │ ├── S1.cpp │ ├── S2.cpp │ └── S4.cpp ├── 2013 │ └── 13S5.cpp ├── 2014 │ ├── 14S1.cpp │ ├── 14S2.cpp │ └── 14S3.cpp ├── 2015 │ ├── 15S3.cpp │ └── j5.cpp ├── 2017 │ └── 17S3.cpp ├── 2018 │ ├── 18j5.py │ └── 18S4.cpp ├── 2020 │ ├── 20S5.cpp │ └── 20S2.cpp ├── 2021 │ ├── 21S1.cpp │ └── 21S2.cpp ├── 2022 │ ├── S1.cpp │ ├── S3.cpp │ ├── S4.cpp │ └── J5.cpp ├── 2023 │ ├── J1.py │ ├── J2.py │ ├── J3.py │ ├── S2.cpp │ └── S1.cpp ├── mock_20 │ └── S2.py ├── Mock_14 │ └── 14_s3.py ├── Mock_21 │ └── S3.py └── Mock_22_2 │ ├── S1.cpp │ └── S2.cpp ├── other solutions ├── or-deal.py ├── Paving_road.py ├── knapsack1.py ├── A_Greedy_Problem.py ├── coding_spree.py ├── TSS_Club_Fair_2017_Problem_B.py ├── art2.py ├── vacation.py ├── frog_1.cpp ├── knapsack2.cpp ├── Christmas_cards.cpp ├── cherry_tree.py ├── Dominos Tiling.cpp ├── Big_Deposits.cpp ├── deque.cpp ├── persuation.cpp ├── dpm.cpp ├── That_problem.cpp ├── 最大子序和.cpp ├── chair_stacking.cpp ├── CamelCase.cpp ├── globexcup18_s2.py ├── TSP_问题.cpp ├── connected_component.py ├── redstone.cpp ├── cherry.cpp ├── Frogs.cpp ├── hydration.cpp ├── dps.cpp ├── 游戏.cpp ├── 2primes.cpp ├── BOI_'09_P1.py ├── Cat Girls.cpp ├── frog2.cpp ├── 能量项链.cpp ├── Marcia_and_maze.cpp ├── String_Finding.cpp ├── 最小弹药数.cpp ├── dynamic_medianv2.cpp ├── balancing_act.cpp └── farming_simulator.cpp ├── DMOPC ├── 15 │ └── Contest 2 │ │ └── sawmill.py ├── 20 │ ├── Contest 5 │ │ └── Home Row.cpp │ └── Contest 4 │ │ └── Missing Numbers.cpp └── 21 │ ├── Dec │ └── P3.py │ └── Feb │ ├── P2.cpp │ └── P1.cpp ├── USACO ├── 2008 │ └── Janurary │ │ └── cow_race.cpp ├── 2011 │ └── Open │ │ └── Gold │ │ └── Mowing_the_Lawn.cpp ├── 2016 │ └── US Open │ │ └── Bronze │ │ └── P1.py ├── 2017 │ ├── Feb │ │ └── Gold │ │ │ └── Why Did the Cow Cross the Road II.cpp │ └── Jan │ │ └── Gold │ │ └── P2.cpp ├── 2018 │ └── February │ │ └── Silver │ │ └── Snow_Boots.cpp ├── 2019 │ ├── Jan │ │ ├── silver │ │ │ ├── P1.py │ │ │ └── P1.cpp │ │ └── Gold │ │ │ ├── sleepy.cpp │ │ │ └── sleepy_v2.cpp │ ├── Dec │ │ └── Silver │ │ │ └── MooBuzz.cpp │ └── Open │ │ └── Gold │ │ └── P1.cpp ├── 2020 │ ├── Dec │ │ ├── Bronze │ │ │ ├── Do You Know ABCs?.cpp │ │ │ └── Daisy Chains.cpp │ │ └── Silver │ │ │ └── Cowntagion.cpp │ ├── US_Open │ │ ├── Bronze │ │ │ └── P2.py │ │ └── Gold │ │ │ ├── exercise.cpp │ │ │ └── haircut.cpp │ ├── Feb │ │ └── Gold │ │ │ └── help.cpp │ └── Jan │ │ ├── Silver │ │ ├── Loan_Repayment.cpp │ │ └── Berry_Picking.cpp │ │ └── Gold │ │ └── time.cpp ├── 2021 │ ├── Jan │ │ └── Bronze │ │ │ ├── Uddered but not Herd.cpp │ │ │ ├── Even More Odd Photos.cpp │ │ │ └── Just Stalling.cpp │ ├── Feb │ │ ├── Silver │ │ │ └── Year of the Cow.md │ │ ├── Bronze │ │ │ ├── Clockwise Fence.cpp │ │ │ └── Comfortable Cows.cpp │ │ └── Gold │ │ │ └── Modern Art 3.cpp │ ├── Dec │ │ ├── Silver │ │ │ └── P3.cpp │ │ └── bronze │ │ │ ├── P2.cpp │ │ │ └── P1.cpp │ └── US Open │ │ └── Gold │ │ ├── United_Cows_of_Farmer_John_v2.cpp │ │ └── United_Cows_of_Farmer_John.cpp ├── 2022 │ ├── Dec │ │ ├── Bronze │ │ │ └── P1.py │ │ └── Silver │ │ │ └── P3.cpp │ ├── Open │ │ └── Silver │ │ │ ├── P2.cpp │ │ │ ├── P3.cpp │ │ │ └── P1.cpp │ └── Jan │ │ └── Silver │ │ └── P2.cpp ├── Farmer John.md └── training │ └── riding_the_fence.cpp ├── COCI ├── 22 │ └── c1 │ │ └── P1.py ├── 2014 │ └── c3 │ │ └── P4.cpp └── COI │ └── 06 │ └── c1 │ └── patrik.cpp ├── Leetcode ├── easy │ ├── maximum-count-of-positive-integer-and-negative-integer.cpp │ ├── minimum-common-value.cpp │ └── difference-between-element-sum-and-digit-sum-of-an-array.cpp ├── hard │ └── check-if-point-is-reachable.cpp └── medium │ ├── maximal-score-after-applying-k-operations.cpp │ ├── increment-submatrices-by-one.cpp │ ├── minimum-operations-to-make-array-equal-ii.cpp │ ├── count-the-number-of-good-subarrays.cpp │ ├── maximum-subsequence-score.cpp │ └── make-number-of-distinct-characters-equal.cpp ├── algorithms ├── Dynamic Programming │ ├── 0-1 knapsack.cpp │ ├── Complete-Knapsack.cpp │ ├── getLCS.cpp │ ├── getLPS.cpp │ ├── LCS.py │ ├── expected_value.cpp │ ├── shortest edit dist.cpp │ ├── getLIS.cpp │ └── add_to_n_num_of_lcm.cpp ├── 整出分块.cpp ├── math │ ├── counting_triangles.py │ ├── hamilton_distance.cpp │ ├── mod_inverse.cpp │ ├── linear_basis.cpp │ ├── euler_function.cpp │ └── chinese_remainder_theorem.cpp ├── Monotone Stack.cpp ├── String algorithm │ ├── smallest_permutation.cpp │ ├── LLS.cpp │ ├── KMP.cpp │ ├── hashing.cpp │ ├── manacher.cpp │ └── Trie.cpp ├── Graph Theory │ ├── shortest path │ │ ├── Floyd.cpp │ │ └── Bellman-Ford.cpp │ ├── mst │ │ └── Kruskal Algorithm.cpp │ └── bigraph │ │ └── Maximum_matching_bigraph.cpp ├── Monotone_queue_dp.cpp ├── number theory │ └── 倍数问题.cpp ├── game_theory │ ├── ICG.cpp │ └── S-Nim.cpp ├── Matrices │ ├── Fibonacci.cpp │ └── Matrix_fast_pow.cpp ├── RMQ Problems │ └── Segment Tree.cpp └── Monotone_queue.cpp ├── SAC22 └── c3 │ ├── P1.cpp │ ├── P2.cpp │ └── P3.cpp ├── CCO ├── 10 │ └── P6.cpp ├── 11 │ └── P4.cpp ├── 14 │ ├── P5.cpp │ ├── P1.cpp │ └── P3.py ├── 17 │ ├── P4.cpp │ └── P5.cpp ├── 18 │ ├── P2.cpp │ ├── P1.cpp │ └── P3.cpp ├── 20 │ └── P2.cpp ├── 21 │ └── P2.cpp ├── 98 │ └── P1.cpp ├── 06 │ └── P1.py ├── 05 │ └── P5.cpp └── Mock_18 │ └── P2.cpp ├── An Animal Contest └── 4 │ ├── P1 - Dasher's Digits.py │ └── P2 - Lavish Lights.py ├── Mock_USACO ├── 单峰树计数.cpp ├── Game.cpp ├── travel.cpp ├── DNA.cpp ├── 华盛顿的斧子.cpp ├── 招待.cpp ├── calc.cpp └── 矩阵行走.cpp ├── CSP └── 2022 │ └── J │ ├── T1.cpp │ ├── T2.cpp │ └── T4.cpp ├── Appleby ├── rectangles.py └── matrix_operation.cpp ├── ioi └── 2000 │ └── palindrome.cpp ├── SGS Programming Challenge ├── P1.cpp └── P2.cpp └── mistakes reminder.md /YAC/c1/f: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /NOIP/2016/f: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /codeforces/div 3/888/A.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Google Kickstart/21/Round H/f: -------------------------------------------------------------------------------- 1 | f 2 | -------------------------------------------------------------------------------- /CCC/2023/J1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | m = int(input()) 3 | x = n * 50 - m * 10 4 | if n > m: x += 500 5 | print(x) -------------------------------------------------------------------------------- /codeforces/div 3/805/F: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andyzhu23/Competitive-Programming/HEAD/codeforces/div 3/805/F -------------------------------------------------------------------------------- /other solutions/or-deal.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | cnt = 1 3 | while (1 << cnt) <= n: 4 | cnt += 1 5 | print('1' * cnt) 6 | -------------------------------------------------------------------------------- /DMOPC/21/Dec/P3.py: -------------------------------------------------------------------------------- 1 | print('''0 2 | 10001 3 | 10101 4 | 11011 5 | 11111 6 | 10201 7 | 12021 8 | 11211 9 | 10301 10 | 13031''') 11 | -------------------------------------------------------------------------------- /other solutions/Paving_road.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | d = list(map(int, input().split())) 3 | d.insert(0, 0) 4 | ans = 0 5 | for i in range(1, n + 1): 6 | ans += max(0, d[i] - d[i - 1]) 7 | print(ans) 8 | -------------------------------------------------------------------------------- /USACO/2022/Dec/Bronze/P1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | a = list(map(int, input().split())) 3 | a = sorted(a) 4 | mx = 0 5 | ans = 0 6 | for i in range(n): 7 | if mx < (n - i) * a[i]: 8 | mx = (n - i) * a[i] 9 | ans = a[i] 10 | print(mx, ans) -------------------------------------------------------------------------------- /other solutions/knapsack1.py: -------------------------------------------------------------------------------- 1 | n, w = map(int, input().split()) 2 | dp = [0 for i in range(w + 1)] 3 | for i in range(n): 4 | a, b = map(int, input().split()) 5 | for j in range(w, a - 1, -1): 6 | dp[j] = max(dp[j], dp[j - a] + b) 7 | print(dp[w]) 8 | -------------------------------------------------------------------------------- /DMOPC/15/Contest 2/sawmill.py: -------------------------------------------------------------------------------- 1 | a = int(input()) 2 | energy = list(map(int, input().split())) 3 | energy.sort() 4 | logs = list(map(int, input().split())) 5 | logs.sort(reverse = True) 6 | total = 0 7 | for i in range(a): 8 | total += (energy[i]*logs[i]) 9 | print(total) 10 | -------------------------------------------------------------------------------- /USACO/2019/Jan/silver/P1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | N, lines = int(input()), [] 3 | for i in range(N-1): 4 | lines.append(list(map(int, input().split()))) 5 | adjacency = [0]*N 6 | 7 | for i in lines: 8 | adjacency[i[0]-1] += 1 9 | adjacency[i[1]-1] += 1 10 | 11 | print(max(adjacency)+1) 12 | -------------------------------------------------------------------------------- /CCC/2023/J2.py: -------------------------------------------------------------------------------- 1 | d = { 2 | "Poblano" : 1500, 3 | "Mirasol" : 6000, 4 | "Serrano" : 15500, 5 | "Cayenne" : 40000, 6 | "Thai" : 75000, 7 | "Habanero" : 125000, 8 | } 9 | n = int(input()) 10 | ans = 0 11 | for i in range(n): 12 | x = input() 13 | ans += d[x] 14 | print(ans) -------------------------------------------------------------------------------- /COCI/22/c1/P1.py: -------------------------------------------------------------------------------- 1 | n, r, s = map(int, input().split()) 2 | for i in range(n): 3 | mn = 0x3f3f3f3f 4 | mx = 0 5 | for j in range(r): 6 | x = input() 7 | for c in x: 8 | if c == '#': 9 | mx = max(mx, j) 10 | mn = min(mn, j) 11 | print(mx - mn) 12 | -------------------------------------------------------------------------------- /codeforces/div 3/888/B_Parity_Sort.py: -------------------------------------------------------------------------------- 1 | 2 | def solve(): 3 | n = int(input()) 4 | a = list(map(int, input().split())) 5 | b = sorted(a) 6 | ans = True 7 | for i in range(n): 8 | ans &= a[i] % 2 == b[i] % 2 9 | print("YES" if ans else "NO") 10 | 11 | t = int(input()) 12 | for i in range(t): 13 | solve() -------------------------------------------------------------------------------- /Leetcode/easy/maximum-count-of-positive-integer-and-negative-integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumCount(vector& nums) { 4 | int a = 0, b = 0; 5 | for(auto x : nums) { 6 | if(x > 0) ++a; 7 | else if(x < 0) ++b; 8 | } 9 | return max(a, b); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /CCC/2009/S1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int a,b,ans; 5 | 6 | int main() { 7 | cin>>a>>b; 8 | ans = 0; 9 | for(int i=0; i<100; i++) 10 | { 11 | if(pow(i,6)>=a and pow(i,6)<=b) 12 | { 13 | ans+=1; 14 | } 15 | } 16 | cout<= ord(c): dq.appendleft(c) 10 | else: dq.append(c) 11 | for i in dq: 12 | print(i, end='') 13 | print() 14 | -------------------------------------------------------------------------------- /USACO/2016/US Open/Bronze/P1.py: -------------------------------------------------------------------------------- 1 | n, k = map(int, input().split()) 2 | 3 | psa = [0] * 10005 4 | 5 | for i in range(n): 6 | x = int(input()) 7 | psa[x] += 1 8 | 9 | for i in range(1, 10001): 10 | psa[i] += psa[i - 1] 11 | 12 | ans = 0 13 | 14 | for i in range(k + 1, 10000): 15 | ans = max(ans, psa[i] - psa[i - k - 1]) 16 | 17 | print(ans) 18 | -------------------------------------------------------------------------------- /USACO/Farmer John.md: -------------------------------------------------------------------------------- 1 | # Farmer John 2 | ### Oh Farmer John with his cow 3 | ### Real things but imaginary actions 4 | ### Don’t ask cows paint walls how 5 | ### Cows attend parties with passions 6 | ### One may say it is a contradiction 7 | ### But I see cows as intelligent 8 | ### Those who promote feel satisfaction 9 | ### Those who didn’t gain development 10 | -------------------------------------------------------------------------------- /codeforces/div 3/888/A_Escalator_Conversations.py: -------------------------------------------------------------------------------- 1 | def solve(): 2 | n, m, k, h = map(int, input().split()) 3 | a = list(map(int, input().split())) 4 | cnt = 0 5 | for x in a: 6 | if x % k == h % k and 0 < abs(h - x) <= (m - 1) * k: 7 | cnt += 1 8 | print(cnt) 9 | 10 | t = int(input()) 11 | for i in range(t): 12 | solve() -------------------------------------------------------------------------------- /other solutions/A_Greedy_Problem.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | f = [] 3 | ans = 0 4 | for i in range(n): 5 | f.append(list(map(int, input().split()))) 6 | f.sort(key=lambda i: i[0]) 7 | for i in f: 8 | if m <= i[1]: 9 | ans += m * i[0] 10 | break 11 | else: 12 | ans += i[1] * i[0] 13 | m -= i[1] 14 | print(ans) 15 | -------------------------------------------------------------------------------- /Leetcode/easy/minimum-common-value.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getCommon(vector& nums1, vector& nums2) { 4 | set s; 5 | for(auto x : nums2) s.insert(x); 6 | for(auto x : nums1) { 7 | if(s.count(x)) { 8 | return x; 9 | } 10 | } 11 | return -1; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /CCC/Mock_14/14_s3.py: -------------------------------------------------------------------------------- 1 | a,b = map(int,input().split()) 2 | arr = [] 3 | for i in range(a): 4 | x = list(map(int,input().split())) 5 | arr.append(x) 6 | n = int(input()) 7 | for i in range(n): 8 | m = int(input()) 9 | arr.sort(key = lambda l:l[m-1]) 10 | 11 | for h in arr: 12 | for l in range(len(h)): 13 | h[l] = str(h[l]) 14 | print(" ".join(h)) 15 | -------------------------------------------------------------------------------- /other solutions/coding_spree.py: -------------------------------------------------------------------------------- 1 | n, t = map(int, input().split()) 2 | probs = [list(map(int, input().split())) for i in range(n)] 3 | arr = [0 for i in range(t + 1)] 4 | for prob in probs: 5 | for i in range(t-prob[1], -1, -1): 6 | #if arr[i] != -1 and i + prob[1] <= t: 7 | arr[i + prob[1]] = max(arr[i + prob[1]], arr[i] + prob[0]) 8 | #print(arr) 9 | print(max(arr)) 10 | -------------------------------------------------------------------------------- /algorithms/Dynamic Programming/0-1 knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int N = 1e3 + 10; 6 | 7 | int n, v, c, w, dp[N]; 8 | 9 | int main(){ 10 | cin>>n>>v; 11 | for(int i = 1;i<=n;i++){ 12 | cin>>c>>w; 13 | for(int j = v; j>=c; j--){ 14 | dp[j] = max(dp[j], dp[j - c] + w); 15 | } 16 | } 17 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(0)->ios::sync_with_stdio(0); 6 | #ifdef LOCAL 7 | freopen("in.txt", "r", stdin); 8 | freopen("out.txt", "w", stdout); 9 | #endif 10 | int a, b, c; 11 | cin>>a>>b>>c; 12 | for(int i =1;i<=c;++i) { 13 | a -= b; 14 | cout<& nums) { 4 | int a = 0, b = 0; 5 | for(auto x : nums) { 6 | a += x; 7 | while(x > 0) { 8 | b += x % 10; 9 | x /= 10; 10 | } 11 | } 12 | return abs(a - b); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /algorithms/Dynamic Programming/Complete-Knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 1e3+10; 4 | 5 | int n,v; 6 | int c,w,dp[N]; 7 | int main() { 8 | cin>>n>>v; 9 | for(int i = 1;i<=n;i++){ 10 | cin>>c>>w; 11 | for(int j = c;j<=v;j++){ 12 | dp[j] = max(dp[j], dp[j - c] + w); 13 | } 14 | } 15 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int nums[7]; 6 | 7 | void solve(){ 8 | int a = nums[0]; 9 | int b = nums[1]; 10 | int c = nums[6] - a - b; 11 | cout<>nums[i]; 17 | } 18 | sort(nums,nums+7); 19 | solve(); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /CCC/2023/J3.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | s = [] 3 | for i in range(n): 4 | s.append(input()) 5 | mx = 0 6 | ans = [] 7 | for i in range(5): 8 | cnt = 0 9 | for j in range(n): 10 | if s[j][i] == 'Y': 11 | cnt += 1 12 | if cnt > mx: 13 | mx = cnt 14 | ans = [i + 1] 15 | elif cnt == mx: 16 | ans.append(i + 1) 17 | for i in range(len(ans) - 1): 18 | print(ans[i], end=',') 19 | print(ans[len(ans) - 1]) 20 | -------------------------------------------------------------------------------- /DMOPC/20/Contest 5/Home Row.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define endl "\n" 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(0); 7 | cin.tie(0); 8 | string s, t; 9 | cin>>s>>t; 10 | int i; 11 | for(i = 0;i& nums, int k) { 4 | priority_queue heap; 5 | for(auto x : nums) heap.push(x); 6 | long long ans = 0; 7 | while(k--) { 8 | int u = heap.top(); heap.pop(); 9 | ans += u; 10 | heap.push((u - 1) / 3 + 1); 11 | } 12 | return ans; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /USACO/2021/Jan/Bronze/Uddered but not Herd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define endl "\n" 3 | using namespace std; 4 | string a, b; 5 | int ans; 6 | 7 | int main() { 8 | ios::sync_with_stdio(0); 9 | cin.tie(0); 10 | cin>>a>>b; 11 | int cur = 0; 12 | while(cur < b.size()){ 13 | for(int i = 0;i<26;i++){ 14 | if(a[i] == b[cur]) cur ++; 15 | } 16 | ++ans; 17 | } 18 | 19 | cout< 2 | using namespace std; 3 | using ll = long long; 4 | ll n; 5 | 6 | int main() { 7 | ios::sync_with_stdio(0); 8 | cin.tie(0); 9 | #ifdef LOCAL 10 | freopen("in.txt", "r", stdin); 11 | freopen("out.txt", "w", stdout); 12 | #endif 13 | cin>>n; 14 | ll ans = 0; 15 | for(int i = 1;i<=n;++i) { 16 | ans += n / i * (n / (n / i) - i + 1); 17 | i = n / (n / i); 18 | } 19 | cout< 2 | using namespace std; 3 | #define pb push_back 4 | int n; 5 | 6 | signed main() { 7 | // cin.tie(0)->ios::sync_with_stdio(0); 8 | #ifdef LOCAL 9 | freopen("in.txt", "r", stdin); 10 | freopen("out.txt", "w", stdout); 11 | #endif 12 | cin>>n; 13 | int ans = 0; 14 | for(int i = 0;i * 4<=n;++i) { 15 | int j = n - i * 4; 16 | if(j % 5 == 0) { 17 | ans++; 18 | } 19 | } 20 | cout<= mcv: 9 | mcv = a[i] 10 | pos0 = i + 1 11 | 12 | pos = 0 13 | for i in range(n): 14 | if x[i] == "0": 15 | pos += 1 16 | if pos == pos0: 17 | pos = i + 1 18 | break 19 | 20 | x = x.replace("0", "") 21 | pos -= pos0 22 | 23 | print(x[pos:n] + x[:pos]) -------------------------------------------------------------------------------- /algorithms/math/counting_triangles.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | ans = 0 3 | 4 | def gcd(a, b): 5 | if a * b == 0: return a + b 6 | return gcd(b, a % b) 7 | 8 | for i in range(0, n + 1): 9 | for j in range(0, m + 1): 10 | if i == 0 and j == 0: continue 11 | if i == 0 or j == 0: ans += (gcd(i, j) - 1) * (n + 1 - i) * (m + 1 - j) 12 | else: ans += (gcd(i, j) - 1) * (n + 1 - i) * (m + 1 - j) << 1 13 | print(((n + 1) * (m + 1)) * ((n + 1) * (m + 1) - 1) * ((n + 1) * (m + 1) - 2) // 6 - ans) 14 | -------------------------------------------------------------------------------- /algorithms/Dynamic Programming/getLCS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | const int N = 1e4; 5 | string a,b; 6 | int dp[N][N],len1,len2; 7 | int main() { 8 | cin>>a>>b; 9 | len1 = a.size(); 10 | len2 = b.size(); 11 | for(int i = 1;i<=len1;i++){ 12 | for(int j = 1;j<=len2;j++){ 13 | if(a[i - 1] == b[j - 1]) dp[i][j] = dp[i-1][j-1]+1; 14 | else dp[i][j] = max(dp[i-1][j], dp[i][j-1]); 15 | } 16 | } 17 | cout< 2 | #include 3 | #define ll long long 4 | using namespace std; 5 | 6 | string solve(){ 7 | ll x; 8 | cin>>x; 9 | unordered_set vis; 10 | for(ll i = 1;i * i * i <= x;i++){ 11 | ll j = x - i * i * i; 12 | vis.insert(i * i * i); 13 | if(vis.count(j)) return "YES"; 14 | } 15 | return "NO"; 16 | } 17 | 18 | int t; 19 | int main() { 20 | cin>>t; 21 | while(t--){ 22 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(0); 6 | cin.tie(0); 7 | int l = 1, r = 1000000; 8 | while(l <= r) { 9 | 10 | int mid = (l + r + 1) >> 1; 11 | cout<>s; 13 | if(s == "<") { 14 | r = mid - 1; 15 | } else l = mid; 16 | if(l == r) { 17 | cout<<"! "< 2 | using namespace std; 3 | const int mod = 1234567; 4 | 5 | template 6 | T1 fp(T1 a, T2 b) { 7 | T1 c = 1; 8 | while(b) { 9 | if(b & 1) c = (c * a) % mod; 10 | b >>= 1; 11 | a = a * a % mod; 12 | } 13 | return c; 14 | } 15 | 16 | int main() { 17 | #ifdef LOCAL 18 | freopen("in.txt", "r", stdin); 19 | freopen("out.txt", "w", stdout); 20 | #endif 21 | int n; 22 | scanf("%d", &n); 23 | printf("%lld\n", fp(2ll, n - 1)); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /algorithms/Dynamic Programming/getLPS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | string s1,s2; 4 | const int N = 1e3+10; 5 | int dp[N][N]; 6 | int main() { 7 | cin>>s1; 8 | int len = s1.size(); 9 | for(int i = 0;i 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | ios::sync_with_stdio(0); 7 | cin.tie(0); 8 | int n; 9 | cin >> n; 10 | int h[n]; 11 | for(int i = 0; i < n; i++) cin>>h[i]; 12 | 13 | int dp[n]; 14 | 15 | for(int i = 0; i 2 | using namespace std; 3 | 4 | void solve() { 5 | int n; cin>>n; 6 | string s; cin>>s; 7 | int tot = 0; 8 | for(int i = 0;i>t; 24 | while(t--) solve(); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /algorithms/math/hamilton_distance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int inf = 0x3f3f3f3f; 4 | int n, a = -inf, b = inf, c = -inf, d = inf; 5 | 6 | int main() { 7 | cin.tie(0)->ios::sync_with_stdio(0); 8 | #ifdef LOCAL 9 | freopen("in.txt", "r", stdin); 10 | freopen("out.txt", "w", stdout); 11 | #endif 12 | cin>>n; 13 | for(int i = 1;i<=n;++i) { 14 | int x, y; cin>>x>>y; 15 | a = max(x + y, a); 16 | b = min(x + y, b); 17 | c = max(x - y, c); 18 | d = min(x - y, d); 19 | } 20 | cout< 2 | using namespace std; 3 | const int N = 2e5 + 10; 4 | int n; 5 | void solve(){ 6 | int a[N] = {0}; 7 | int suffix[N] = {0}; 8 | cin>>n; 9 | for(int i = 1;i<=n;i++){ 10 | cin>>a[i]; 11 | } 12 | int ans = 0; 13 | for(int i = n;i >=1;i--){ 14 | if(i + a[i] > n) suffix[i] = a[i]; 15 | else suffix[i] = a[i] + suffix[i + a[i]]; 16 | ans = max(ans, suffix[i]); 17 | } 18 | cout<>t; 25 | while(t--){ 26 | solve(); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /CSP/2022/J/T1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | using ll = long long; 4 | 5 | int main() { 6 | #ifdef LOCAL 7 | freopen("in.txt", "r", stdin); 8 | freopen("out.txt", "w", stdout); 9 | #endif 10 | ll a, b; 11 | cin>>a>>b; 12 | if(a == 0) { 13 | cout<<0<<'\n'; 14 | return 0; 15 | } 16 | ll c = 1; 17 | while(b) { 18 | if(b & 1) { 19 | c *= a; 20 | if(c > 1e9) c = 0; 21 | } 22 | b >>= 1; 23 | a = a * a; 24 | if(a > 1e9) a = 0; 25 | } 26 | if(c == 0) cout<<-1<<'\n'; 27 | else cout< 2 | using namespace std; 3 | using ll = long long; 4 | ll m; 5 | 6 | int main() { 7 | cin.tie(0)->ios::sync_with_stdio(0); 8 | #ifdef LOCAL 9 | freopen("in.txt", "r", stdin); 10 | freopen("out.txt", "w", stdout); 11 | #endif 12 | cin>>m; 13 | double l = 0, r = 1e18; 14 | double ans; 15 | auto get = [&](double mid) { 16 | return 12 * mid * mid - 5 * mid + 1; 17 | }; 18 | while(l <= r) { 19 | double mid = (l + r) / 2; 20 | if(get(mid) <= m) ans = mid, l = mid + 0.0000001; 21 | else r = mid - 0.0000001; 22 | } 23 | printf("%.7f", ans); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /algorithms/Monotone Stack.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This algorithm figures out the first element greater than itself in the array 3 | */ 4 | 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | const int N = 1e4 + 10; 11 | 12 | int n, a[N], ans[N]; 13 | stack stk; 14 | 15 | int main(){ 16 | cin>>n; 17 | for(int i =1;i<=n;i++){ 18 | cin>>a[i]; 19 | 20 | while(!stk.empty() && a[stk.top()] < a[i]){ 21 | ans[stk.top()] = i; 22 | stk.pop(); 23 | } 24 | stk.push(i); 25 | } 26 | while(!stk.empty()){ 27 | ans[stk.top()] = n + 1; 28 | stk.pop(); 29 | } 30 | for(int i = 1;i<=n;i++) cout< 2 | using namespace std; 3 | const int N = 1e5 + 10; 4 | int n, W, dp[N]; 5 | 6 | int main() { 7 | cin.tie(0)->ios::sync_with_stdio(0); 8 | #ifdef LOCAL 9 | freopen("in.txt", "r", stdin); 10 | freopen("out.txt", "w", stdout); 11 | #endif 12 | cin>>n>>W; 13 | memset(dp, 0x3f, sizeof(dp)); 14 | dp[0] = 0; 15 | for(int i = 1;i<=n;++i) { 16 | int w, v; 17 | cin>>w>>v; 18 | for(int j = N - 1;j >= v;--j) dp[j] = min(dp[j], dp[j - v] + w); 19 | } 20 | int ans = 0; 21 | for(int i = 1;i 2 | #include 3 | using namespace std; 4 | 5 | int n; 6 | 7 | typedef pair p; 8 | 9 | p findFactor(int x){ 10 | int y = x / 2; 11 | while((x - y) % y != 0){ 12 | y--; 13 | } 14 | return make_pair(y, x - y); 15 | } 16 | 17 | int main() { 18 | scanf("%d",&n); 19 | int ans = 0; 20 | while(n > 1){ 21 | if(n%2 == 0){ 22 | n = n / 2; 23 | ans++; 24 | } else { 25 | p fac = findFactor(n); 26 | ans += fac.second / fac.first; 27 | n = fac.second; 28 | } 29 | } 30 | printf("%d", ans); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /other solutions/Christmas_cards.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 5005; 4 | using ll = long long; 5 | ll dp[N]; 6 | int n; 7 | 8 | int main() { 9 | cin.tie(0)->ios::sync_with_stdio(0); 10 | #ifdef LOCAL 11 | freopen("in.txt", "r", stdin); 12 | freopen("out.txt", "w", stdout); 13 | #endif 14 | cin>>n; 15 | memset(dp, 0x3f, sizeof(dp)); 16 | dp[1] = 0; 17 | for(int i = 1;i<=n;++i) { 18 | ll c; 19 | int d; 20 | cin>>c>>d; 21 | for(int j = n;j>i;--j) { 22 | dp[j] = min(dp[j], dp[max(j - d, i)] + c); 23 | } 24 | } 25 | cout< k): 9 | continue 10 | if cur not in cnt: 11 | cnt[cur] = 0 12 | cnt[cur] += 1 13 | a.append(cur) 14 | 15 | stk = [0 for i in range(k + 10)] 16 | vis = {} 17 | len = 0 18 | for i in a: 19 | if i not in vis: 20 | while len > 0 and stk[len] > i and cnt[stk[len]] > 0: 21 | del vis[stk[len]] 22 | len -= 1 23 | len += 1 24 | stk[len] = i 25 | vis[i] = True 26 | cnt[i] -= 1 27 | 28 | for i in range(1, len): 29 | print(stk[i], end=' ') 30 | print(stk[len]) 31 | -------------------------------------------------------------------------------- /Appleby/rectangles.py: -------------------------------------------------------------------------------- 1 | num = int(input()) 2 | positions = [] 3 | max_area = 0 4 | for i in range(num): 5 | pos = input() 6 | pos = pos.split() 7 | pos = (int(pos[0]), int(pos[1])) 8 | positions.append(pos) 9 | 10 | for pos1 in positions: 11 | x1, y1 = pos1 12 | for pos2 in positions: 13 | x2, y2 = pos2 14 | if x1 == x2 and pos2 is not pos1: 15 | for pos3 in positions: 16 | x3, y3 = pos3 17 | if y1 == y3 and pos3 is not pos1: 18 | if (x3, y2) in positions: 19 | area = abs((x3-x1) * (y2-y1)) 20 | max_area = max(max_area, area) 21 | print(max_area) 22 | -------------------------------------------------------------------------------- /Google Kickstart/21/Round B/Problem A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define endl "\n" 3 | using namespace std; 4 | string s; 5 | int n; 6 | inline void solve(){ 7 | cin>>n; 8 | cin>>s; 9 | s = "@" + s; 10 | cout<<1<<" "; 11 | int cnt = 1; 12 | for(int i = 2;i<=n;i++){ 13 | if(s[i] > s[i - 1]) cout<<++cnt<<" "; 14 | else { 15 | cnt = 1; 16 | cout<<1<<" "; 17 | } 18 | } 19 | } 20 | 21 | int main() { 22 | ios::sync_with_stdio(0); 23 | cin.tie(0); 24 | int t; cin>>t; 25 | for(int i = 1;i<=t;i++){ 26 | cout<<"Case #" << i << ": "; 27 | solve(); cout << endl; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /other solutions/cherry_tree.py: -------------------------------------------------------------------------------- 1 | num_patches, cherry_goal, max_weight = list(map(int, input().split())) 2 | num_cherries = list(map(int, input().split())) 3 | counter = 0 4 | branches = {} 5 | for i in range(num_patches-1): 6 | p1, p2, k = list(map(int, input().split())) 7 | if p1 > p2: 8 | p1, p2 = p2, p1 9 | branches[p2] = [p1, num_cherries[p2-1], k] 10 | for i in range(num_patches, 1, -1): 11 | branch = branches[i] 12 | if branch[1] >= cherry_goal and branch[2] <= max_weight: 13 | counter += 1 14 | if branch[0] != 1: 15 | c_branch = branches[branch[0]] 16 | c_branch[1] += branch[1] 17 | c_branch[2] += branch[2] 18 | print(counter) 19 | -------------------------------------------------------------------------------- /algorithms/Dynamic Programming/LCS.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | t = input() 3 | ls = len(s) 4 | lt = len(t) 5 | 6 | 7 | dp = [[0 for i in range(lt + 1)] for j in range(ls + 1)] 8 | 9 | for i in range(ls): 10 | for j in range(lt): 11 | if s[i] == t[j]: 12 | dp[i + 1][j + 1] = dp[i][j] + 1 13 | else: 14 | dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]) 15 | 16 | i = ls - 1 17 | j = lt - 1 18 | answer = '' 19 | while (0 <= i) & (0 <= j): 20 | if s[i] == t[j]: 21 | answer += s[i] 22 | i -= 1 23 | j -= 1 24 | elif dp[i + 1][j + 1] == dp[i + 1][j]: 25 | j -= 1 26 | else: 27 | i -= 1 28 | print(answer[::-1]) 29 | -------------------------------------------------------------------------------- /algorithms/math/mod_inverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | using pii = pair; 4 | int a, b; 5 | 6 | int exgcd(int a, int b, int& x, int& y) { 7 | if(a * b == 0) { 8 | x = 1, y = 0; 9 | return a + b; 10 | } 11 | int gcd = exgcd(b, a % b, x, y); 12 | int tmp = y; 13 | y = x - a / b * y; 14 | x = tmp; 15 | return gcd; 16 | } 17 | 18 | int main() { 19 | cin.tie(0)->ios::sync_with_stdio(0); 20 | #ifdef LOCAL 21 | freopen("in.txt", "r", stdin); 22 | freopen("out.txt", "w", stdout); 23 | #endif 24 | cin>>a>>b; 25 | int x, y; 26 | exgcd(a, b, x, y); 27 | cout<<(x + b) % b<<'\n'; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /NOIP/2016/toy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 1e5 + 5; 4 | string s[N]; 5 | int n, m; 6 | bitset p; 7 | 8 | int main() { 9 | cin.tie(0)->ios::sync_with_stdio(0); 10 | #ifdef LOCAL 11 | freopen("in.txt", "r", stdin); 12 | freopen("out.txt", "w", stdout); 13 | #endif 14 | cin>>n>>m; 15 | for(int i = 0;i>x; 17 | p[i] = x; 18 | cin>>s[i]; 19 | } 20 | int ans = 0; 21 | for(int i = 1;i<=m;++i) { 22 | int u, v; cin>>u>>v; 23 | if(!p[ans]) u ^= 1; 24 | if(u) v *= -1; 25 | ans = (ans + n + v) % n; 26 | } 27 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | using ll = long long; 5 | const int MOD = 1e6; 6 | const int N = 40; 7 | int n, dp[N]; 8 | inline void solve(){ 9 | dp[0] = 1; 10 | for(int i = 2;i>n; 27 | cout< 2 | using namespace std; 3 | 4 | string s; 5 | int n; 6 | 7 | int main() { 8 | ios::sync_with_stdio(0); 9 | cin.tie(0); 10 | #ifdef LOCAL 11 | freopen(".in", "r", stdin); 12 | freopen(".out", "w", stdout); 13 | #endif 14 | cin>>s; 15 | n = s.size(); 16 | s = '@' + s + s; 17 | int i = 1, j = 2, k = 0; 18 | while(i <= n && j <= n) { 19 | while(s[i + k] == s[j + k]) ++k; 20 | s[i + k] > s[j + k] ? i += k + 1 : j += k + 1; 21 | k = 0; 22 | if(i == j) ++j; 23 | } 24 | for(int x = 0;x 2 | using namespace std; 3 | const int N = 2e4 + 5; 4 | int n, dp[N][2], l[N], r[N]; 5 | 6 | int main() { 7 | #ifdef LOCAL 8 | freopen("in.txt", "r", stdin); 9 | freopen("out.txt", "w", stdout); 10 | #endif 11 | scanf("%d", &n); 12 | l[0] = r[0] = 1; 13 | for(int i = 1;i<=n;++i) { 14 | scanf("%d%d", l + i, r + i); 15 | dp[i][0] = r[i] - l[i] + min(dp[i - 1][0] + abs(l[i - 1] - r[i]), dp[i - 1][1] + abs(r[i - 1] - r[i])) + 1; 16 | dp[i][1] = r[i] - l[i] + min(dp[i - 1][0] + abs(l[i - 1] - l[i]), dp[i - 1][1] + abs(r[i - 1] - l[i])) + 1; 17 | } 18 | printf("%d\n", min(dp[n][0] + n - l[n], dp[n][1] + n - r[n]) - 1); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /CCC/2000/00S4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | const int N = 52; 5 | const int V = 5380; 6 | const int INF = 1e7 + 10; 7 | 8 | int v, n, c[N]; 9 | int dp[V]; 10 | 11 | int main() { 12 | cin>>v; 13 | cin>>n; 14 | for(int i = 1;i<=v;i++){ 15 | dp[i] = INF; 16 | } 17 | for(int i = 1;i<=n;i++){ 18 | cin>>c[i]; 19 | dp[c[i]] = 1; 20 | } 21 | 22 | for(int i = 1;i<=v;i++){ 23 | for(int j = 1;j 2 | 3 | using namespace std; 4 | 5 | int n, p; 6 | 7 | int main() { 8 | cin>>n>>p; 9 | int x = 0; 10 | vector > v; 11 | for(int i = 1;i<=n;++i) { 12 | if(i == p) { 13 | int tmp; 14 | cin>>x>>tmp; 15 | continue; 16 | } 17 | int a, b; 18 | cin>>a>>b; 19 | if(a >= b) v.push_back({b, a}); 20 | } 21 | sort(v.begin(), v.end()); 22 | int cnt = 1; 23 | for(auto[b, a] : v) { 24 | if(x >= b) { 25 | // cout<= k: 15 | ans = c[i + 1] >= k 16 | break 17 | if a[0] == a[-1]: ans = b[-1] >= k 18 | if ans: print("YES") 19 | else: print("NO") 20 | 21 | t = int(input()) 22 | 23 | for i in range(t): 24 | solve() -------------------------------------------------------------------------------- /NOIP/公交换乘.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 1e5+45; 4 | int n,q,a,b; 5 | 6 | struct cows{ 7 | int x = 0; 8 | int y = 0; 9 | int z = 0; 10 | }cows[N]; 11 | 12 | int main() { 13 | scanf("%d %d",&n,&q); 14 | for(int i =1;i<=n;i++){ 15 | scanf("%d",&a); 16 | cows[i].x = cows[i-1].x; 17 | cows[i].y = cows[i-1].y; 18 | cows[i].z = cows[i-1].z; 19 | if(a == 1) cows[i].x++; 20 | else if(a == 2) cows[i].y++; 21 | else cows[i].z++; 22 | } 23 | while(q--){ 24 | scanf("%d %d",&a,&b); 25 | printf("%d %d %d\n",cows[b].x - cows[a-1].x, cows[b].y - cows[a-1].y, cows[b].z - cows[a-1].z); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /CCO/11/P4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n; 5 | multiset bst; 6 | 7 | int main() { 8 | cin.tie(0)->ios::sync_with_stdio(0); 9 | #ifdef LOCAL 10 | freopen("in.txt", "r", stdin); 11 | freopen("out.txt", "w", stdout); 12 | #endif 13 | cin>>n; 14 | int x; cin>>x; 15 | bst.insert(x); 16 | bst.insert(x); 17 | for(int i = 2;i<=n;++i) { 18 | int x; cin>>x; 19 | auto it = bst.upper_bound(x); 20 | if(it == bst.begin()) { 21 | puts("NO"); 22 | return 0; 23 | } 24 | it = prev(it); 25 | bst.erase(it); 26 | bst.insert(x); 27 | bst.insert(x); 28 | } 29 | puts("YES"); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /algorithms/Dynamic Programming/expected_value.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 1010; 4 | double dp[N]; 5 | // dp[i] means the expected value after i turns 6 | bool vis[N]; 7 | double n; 8 | int m; 9 | 10 | double dfs(int u) { 11 | if(vis[u]) return dp[u]; 12 | double last = dfs(u - 1); 13 | dp[u] = (n - last) / n * (last + 1) + last / n * last; 14 | vis[u] = 1; 15 | return dp[u]; 16 | } 17 | 18 | int main() { 19 | ios::sync_with_stdio(0); 20 | cin.tie(0); 21 | #ifdef LOCAL 22 | freopen("in.txt", "r", stdin); 23 | freopen("out.txt", "w", stdout); 24 | #endif 25 | vis[1] = 1; 26 | dp[1] = 1; 27 | cin>>n>>m; 28 | printf("%.6lf\n", dfs(m)); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /DMOPC/21/Feb/P2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, k; 5 | string s; 6 | 7 | priority_queue heap; 8 | 9 | int main() { 10 | cin.tie(0)->ios::sync_with_stdio(0); 11 | #ifdef LOCAL 12 | freopen("in.txt", "r", stdin); 13 | freopen("out.txt", "w", stdout); 14 | #endif 15 | cin>>n>>k; 16 | cin>>s; 17 | int tot = 0; 18 | for(int i = 0;i 2 | using namespace std; 3 | const int N = 5001; 4 | int n, dp[2][N]; 5 | char s[N], t[N]; 6 | 7 | int main() { 8 | #ifdef LOCAL 9 | freopen("in.txt", "r", stdin); 10 | freopen("out.txt", "w", stdout); 11 | #endif 12 | scanf("%d", &n); 13 | scanf("%s", s + 1); 14 | for(int i = 1;i<=n;++i) t[i] = s[n - i + 1]; 15 | for(int i = 1;i<=n;++i) { 16 | for(int j = 1;j<=n;++j) { 17 | if(s[i] == t[j]) dp[i & 1][j] = dp[i & 1 ^ 1][j - 1] + 1; 18 | dp[i & 1][j] = max(dp[i & 1][j], max(dp[i & 1 ^ 1][j], dp[i & 1][j - 1])); 19 | } 20 | memset(dp[i & 1 ^ 1], 0, sizeof(dp[i & 1 ^ 1])); 21 | } 22 | printf("%d\n", n - dp[n & 1][n]); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /codeforces/div 3/702/Dense Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | const int N = 55; 5 | int t, n; 6 | double a[N]; 7 | 8 | int solve(){ 9 | cin>>n; 10 | for(int i =1;i<=n;i++) cin>>a[i]; 11 | int ans = 0; 12 | for(int i =1;i 2.0){ 14 | int tmp = min(a[i], a[i + 1]) * 2; 15 | ans ++; 16 | while(tmp < max(a[i], a[i + 1])){ 17 | tmp *= 2; 18 | ans ++; 19 | } 20 | ans--; 21 | } 22 | } 23 | return ans; 24 | } 25 | 26 | int main() { 27 | cin>>t; 28 | while(t--){ 29 | cout< 2 | using namespace std; 3 | using ll = long long; 4 | ll p, y, t; 5 | 6 | bool ok(ll n) { 7 | __int128 ret = 0; 8 | for(int i = 1;i<=y;++i) { 9 | ret = (ret + n) * (100 + p) / 100; 10 | if(ret >= t) return 1; 11 | } 12 | return ret >= t; 13 | } 14 | 15 | int main() { 16 | cin.tie(0)->ios::sync_with_stdio(0); 17 | #ifdef LOCAL 18 | freopen("in.txt", "r", stdin); 19 | freopen("out.txt", "w", stdout); 20 | #endif 21 | cin>>p>>y>>t; 22 | ll lo = 1, hi = t, ans; 23 | while(lo <= hi) { 24 | ll mid = lo + hi >> 1; 25 | if(ok(mid)) ans = mid, hi = mid - 1; 26 | else lo = mid + 1; 27 | } 28 | cout< 2 | #include 3 | #define endl "\n" 4 | using namespace std; 5 | const int N = 1e7 + 10; 6 | int res[N], ans[N]; 7 | 8 | inline void cal(){ 9 | int cnt = 0; 10 | while(++cnt < N){ 11 | for(int i = cnt;i= N) continue; 17 | if(ans[res[i]] == -1) ans[res[i]] = i; 18 | } 19 | } 20 | 21 | int main(){ 22 | ios::sync_with_stdio(0); 23 | cin.tie(0); 24 | memset(ans, -1, sizeof(ans)); 25 | cal(); 26 | int t; cin>>t; 27 | while(t--){ 28 | int n; 29 | cin>>n; 30 | cout< 2 | #include 3 | #define endl "\n" 4 | using namespace std; 5 | using ll = long long; 6 | int n, m; 7 | queue q; 8 | 9 | int main() { 10 | ios::sync_with_stdio(0); 11 | cin.tie(0); 12 | cin>>n; 13 | for(int i = 1;i<=n;i++) q.push(i); 14 | cin>>m; 15 | while(m--){ 16 | int cnt = 0; 17 | int sz = q.size(); 18 | int r; 19 | cin>>r; 20 | for(int i = 1;i<=sz;i++){ 21 | int cur = q.front(); 22 | q.pop(); 23 | ++cnt; 24 | if(cnt % r != 0){ 25 | q.push(cur); 26 | } 27 | } 28 | } 29 | while(!q.empty()){ 30 | cout< 2 | using namespace std; 3 | const int N = 1e6 + 5; 4 | using ll = long long; 5 | int n, m, ans[N]; 6 | ll k; 7 | 8 | int main() { 9 | #ifdef LOCAL 10 | freopen("in.txt", "r", stdin); 11 | freopen("out.txt", "w", stdout); 12 | #endif 13 | cin>>n>>m>>k; 14 | k -= n; 15 | if(k < 0) { 16 | puts("-1"); 17 | return 0; 18 | } 19 | for(int i = 1;i<=n;++i) { 20 | int nxt = min({(ll)m - 1, (ll)i - 1, k}); 21 | if(i - nxt - 1 == 0) ans[i] = ans[i - 1] + 1; 22 | else ans[i] = ans[i - nxt - 1]; 23 | k -= nxt; 24 | } 25 | if(k) { 26 | puts("-1"); 27 | return 0; 28 | } 29 | for(int i = 1;i<=n;++i) cout< 2 | #include 3 | #define p pair 4 | #define f first 5 | #define s second 6 | using namespace std; 7 | const int N = 1e5 + 10; 8 | int n, t, k, a, cont[N], cnt; 9 | queue

dock; 10 | int main() { 11 | cin>>n; 12 | cnt = 0; 13 | for(int i =1 ;i<=n;i++){ 14 | cin>>t>>k; 15 | for(int j = 1;j<=k;j++){ 16 | cin>>a; 17 | dock.push(make_pair(a,t)); 18 | cont[a] ++; 19 | if(cont[a] == 1) cnt++; 20 | } 21 | while(!dock.empty() && t - dock.front().s >= 86400){ 22 | p b = dock.front(); dock.pop(); 23 | cont[b.f] --; 24 | if(cont[b.f] == 0) cnt--; 25 | } 26 | cout< 8 | using namespace std; 9 | 10 | const int N = 1e5 + 10; 11 | int n; 12 | int a[N], b[N]; 13 | 14 | signed main() { 15 | cin>>n; 16 | for(int i = 1;i <= n + 1;i++){ 17 | cin>>a[i]; 18 | } 19 | for(int i = 1;i<=n;i++){ 20 | cin>>b[i]; 21 | } 22 | int ans = 0; 23 | for(int i = 1;i<=n;i++){ 24 | ans += b[i] * (a[i] + a[i + 1]); 25 | } 26 | if(ans % 2 == 0) cout< 2 | using namespace std; 3 | const int N = 2005; 4 | using ll = long long; 5 | int n, dp[N][N]; 6 | char c[N][N]; 7 | 8 | int dfs(int x, int y) { 9 | if(c[x][y] != '#' || dp[x][y]) return dp[x][y]; 10 | dp[x][y] = min({dfs(x + 1, y - 1), dfs(x + 1, y), dfs(x + 1, y + 1)}) + 1; 11 | return dp[x][y]; 12 | } 13 | 14 | int main() { 15 | #ifdef LOCAL 16 | freopen("in.txt", "r", stdin); 17 | freopen("out.txt", "w", stdout); 18 | #endif 19 | scanf("%d", &n); 20 | ll ans = 0; 21 | for(int i = 1;i<=n;++i) scanf("%s", c[i] + 1); 22 | for(int i = 1;i<=n;++i) { 23 | for(int j = 1;j<=n;++j) if(c[i][j] == '#') { 24 | ans += dfs(i, j); 25 | } 26 | } 27 | printf("%lld\n", ans); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /CCO/18/P2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 105; 4 | int n, a[N][N]; 5 | 6 | int main() { 7 | cin.tie(0)->ios::sync_with_stdio(0); 8 | #ifdef LOCAL 9 | freopen("in.txt", "w", stdout); 10 | #endif 11 | int n = 100; 12 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | const int N = 110; 5 | 6 | int f[N][N], n, m; 7 | 8 | int main(){ 9 | cin>>n>>m; 10 | for(int i = 1;i<=m;i++){ 11 | int a, b; 12 | cin>>a>>b; 13 | f[a][b] = 1; 14 | } 15 | for(int k = 1;k<=n;k++){ 16 | for(int i = 1;i<=n;i++){ 17 | for(int j = 1;j<=n;++j){ 18 | f[i][j] |= f[i][k] & f[k][j]; 19 | } 20 | } 21 | } 22 | int ans = 0; 23 | for(int i = 1;i<=n;i++){ 24 | int cnt = 0; 25 | for(int j = 1;j<=n;j++){ 26 | if(f[i][j] || f[j][i]) ++cnt; 27 | } 28 | if(cnt == n - 1) ans++; 29 | } 30 | cout< 2 | using namespace std; 3 | using ll = long long; 4 | 5 | 6 | void solve() { 7 | ll n, d, e; 8 | cin>>n>>d>>e; 9 | ll x = n - e * d + 2; 10 | ll lo = 0, hi = x / 2, ans = -1; 11 | while(lo <= hi) { 12 | ll mid = lo + hi >> 1; 13 | if(mid * (x - mid) == n) { 14 | cout<ios::sync_with_stdio(0); 24 | #ifdef LOCAL 25 | freopen("in.txt", "r", stdin); 26 | freopen("out.txt", "w", stdout); 27 | #endif 28 | int t; cin>>t; 29 | while(t--) solve(); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /SGS Programming Challenge/P1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | using ll = long long; 4 | const int N = 2e5 + 5; 5 | int n, a[N]; 6 | bitset b; 7 | 8 | bool get() { 9 | ll cnt1 = 0, cnt2 = 0; 10 | for(int i = 1;i<=n;++i) { 11 | cnt1 += (ll)b[i] * i * (n - i + 1); 12 | cnt2 += (ll)(b[i] ^ 1) * i * (n - i + 1); 13 | } 14 | return cnt2 < cnt1; 15 | } 16 | 17 | int main() { 18 | #ifdef LOCAL 19 | freopen("in.txt", "r", stdin); 20 | freopen("out.txt", "w", stdout); 21 | #endif 22 | cin>>n; 23 | for(int i = 1;i<=n;++i) cin>>a[i]; 24 | int ans = 0; 25 | for(int i = 0;i<=31;++i) { 26 | for(int j = 1;j<=n;++j) b[j] = a[j] >> i & 1; 27 | ans |= get() << i; 28 | } 29 | cout< 2 | using namespace std; 3 | 4 | 5 | void solve(){ 6 | int n; 7 | int cnt[3] = {0}; 8 | cin>>n; 9 | for(int i = 1;i<=n;i++){ 10 | int a; cin>>a; 11 | cnt[a]++; 12 | } 13 | int tot = cnt[1] + cnt[2] * 2; 14 | if(tot % 2){ 15 | cout<<"NO"<= 2 && cnt[2] > 0){ 20 | sum -= 2; 21 | cnt[2] --; 22 | } 23 | while(sum >=1 && cnt[1] > 0){ 24 | sum -= 1; 25 | cnt[1] --; 26 | } 27 | if(sum == 0) cout<<"YES"<>t; 33 | while(t--){ 34 | solve(); 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /algorithms/Dynamic Programming/shortest edit dist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 1e3+10; 4 | const int INF = 1e9; 5 | string a,b; 6 | int dp[N][N]; 7 | int main() { 8 | cin>>a>>b; 9 | int len1 = a.size(); 10 | int len2 = b.size(); 11 | for(int i = 0;i<=len1;i++){ 12 | for(int j = 0;j<=len2;j++){ 13 | dp[i][j] = INF; 14 | } 15 | } 16 | 17 | for(int i = 0;i<=1000;i++){ 18 | dp[0][i] = dp[i][0] = i; 19 | } 20 | 21 | for(int i = 1;i<=len1;i++){ 22 | for(int j = 1;j<=len2;j++){ 23 | if(a[i-1] == b[j-1]) dp[i][j] = dp[i-1][j-1]; 24 | else dp[i][j] = min(dp[i-1][j-1]+1, min(dp[i-1][j]+1,dp[i][j-1]+1)); 25 | } 26 | } 27 | cout< 2 | #include 3 | using namespace std; 4 | const int N = 1e3+ 10; 5 | 6 | int n, a[N], b[N], dp[N][N]; 7 | 8 | bool isFriend(int x, int y){ 9 | if(abs(x - y) <= 4) return true; 10 | return false; 11 | } 12 | 13 | 14 | int main() { 15 | scanf("%d",&n); 16 | for(int i = 1; i<=n;i++){ 17 | scanf("%d", &a[i]); 18 | } 19 | for(int i = 1;i<=n;i++){ 20 | scanf("%d", &b[i]); 21 | } 22 | 23 | for(int i = 1;i<=n;i++){ 24 | for(int j = 1;j<=n;j++){ 25 | if(isFriend(a[i], b[j])) dp[i][j] = dp[i - 1][j - 1] + 1; 26 | dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1])); 27 | } 28 | } 29 | printf("%d", dp[n][n]); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /USACO/2020/Feb/Gold/help.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | using ll = long long; 4 | const int MOD = 1e9 + 7; 5 | const int N = 2e5 + 10; 6 | int n, lazy[N], l[N], r[N], p[N]; 7 | 8 | int main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | freopen("help.in", "r", stdin); 12 | freopen("help.out", "w", stdout); 13 | cin>>n; 14 | for(int i = 1;i<=n;++i) { 15 | cin>>l[i]>>r[i]; 16 | lazy[l[i]]++; 17 | lazy[r[i] + 1]--; 18 | } 19 | p[0] = 1; 20 | for(int i = 1;i<=2 * n + 2;++i) { 21 | lazy[i] += lazy[i - 1]; 22 | p[i] = (p[i - 1] << 1) % MOD; 23 | } 24 | ll ans = 0; 25 | for(int i = 1;i<=n;++i) { 26 | ans = (ans + p[n - lazy[l[i]]]) % MOD; 27 | } 28 | cout< 2 | using namespace std; 3 | 4 | bool flag = 0; 5 | 6 | int main() { 7 | cin.tie(0)->ios::sync_with_stdio(0); 8 | #ifdef LOCAL 9 | freopen("in.txt", "r", stdin); 10 | freopen("out.txt", "w", stdout); 11 | #endif 12 | int d; cin>>d; 13 | string s; cin>>s; 14 | for(int i = 0;i=0;--i) { 23 | // if(s[i] == s[i + 1]) { 24 | // cout< 6 | using namespace std; 7 | const int N =5e6 + 10; 8 | int m, n, k; 9 | 10 | bool e[N], f[N]; 11 | 12 | int main() { 13 | cin>>m>>n>>k; 14 | //bool e[m][n]; 15 | 16 | for(int i = 1;i<=k;i++){ 17 | char c; 18 | int a; 19 | cin>>c >>a; 20 | if(c == 'R'){ 21 | e[a] = !e[a]; 22 | } else { 23 | f[a] = !f[a]; 24 | } 25 | } 26 | int ans = 0; 27 | for(int i = 1;i<=m;i++){ 28 | for(int j = 1;j<=n;j++){ 29 | if(e[i] && f[j]) continue; 30 | if(e[i] || f[j] && !(e[i] && f[j])) ans++; 31 | } 32 | } 33 | cout<> rangeAddQueries(int n, vector>& queries) { 4 | vector > ans(n, vector(n)); 5 | for(vector x : queries) { 6 | ans[x[0]][x[1]] += 1; 7 | if(x[3] < n - 1) ans[x[0]][x[3] + 1] -= 1; 8 | if(x[2] < n - 1) ans[x[2] + 1][x[1]] -= 1; 9 | if(x[3] < n - 1 && x[2] < n - 1) ans[x[2] + 1][x[3] + 1] += 1; 10 | } 11 | for(int i = 0;i 2 | #define int long long 3 | using namespace std; 4 | int n, s; 5 | void solve(){ 6 | cin>>n>>s; 7 | int sum = (1 + n) * n / 2; 8 | int taken = sum - s; 9 | int smallest = taken - n; 10 | int biggest = taken - 1; 11 | if(smallest >= n || (smallest<=0 && biggest > n)){ 12 | cout<<0< 0) ans = (n - smallest + 1) / 2; 18 | else if(biggest < n) ans =(biggest) / 2; 19 | else{ 20 | smallest ++; 21 | ans = (n - smallest) / 2; 22 | } 23 | if(ans < 0) ans = 0; 24 | cout<>t; 30 | while(t--){ 31 | solve(); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Leetcode/medium/minimum-operations-to-make-array-equal-ii.cpp: -------------------------------------------------------------------------------- 1 | using ll = long long; 2 | class Solution { 3 | public: 4 | long long minOperations(vector& nums1, vector& nums2, int k) { 5 | int n = nums1.size(); 6 | if(k == 0) { 7 | for(int i = 0;i nums2[i]) cnt += (nums1[i] - nums2[i]) / k; 18 | if(nums1[i] < nums2[i]) cnt2 += abs(nums1[i] - nums2[i]) / k; 19 | } 20 | if(cnt == cnt2) return cnt; 21 | else return -1ll; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /algorithms/math/linear_basis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | using ll = long long; 4 | const int N = 1005; 5 | using pli = pair; 6 | int n; 7 | ll d[70]; 8 | pli a[N]; 9 | 10 | bool insert(ll x) { 11 | for(int i = 62;~i;--i) if((1ll << i) & x){ 12 | if(d[i] == 0) { 13 | d[i] = x; 14 | return 1; 15 | } else x ^= d[i]; 16 | } 17 | return 0; 18 | } 19 | 20 | int main() { 21 | cin.tie(0)->ios::sync_with_stdio(0); 22 | cin>>n; 23 | for(int i = 1;i<=n;++i) { 24 | ll x; int y; 25 | cin>>x>>y; 26 | a[i] = {x, y}; 27 | } 28 | sort(a + 1, a + n + 1, [&](pli a, pli b) { 29 | return a.second > b.second; 30 | }); 31 | int ans = 0; 32 | for(int i = 1;i<=n;++i) { 33 | auto[x, y] = a[i]; 34 | ans += insert(x) * y; 35 | } 36 | cout< 2 | using namespace std; 3 | const int N = 3005; 4 | using ll = long long; 5 | int a[N], n; 6 | ll dp[2][N][N]; 7 | 8 | int main() { 9 | cin.tie(0)->ios::sync_with_stdio(0); 10 | #ifdef LOCAL 11 | freopen("in.txt", "r", stdin); 12 | freopen("out.txt", "w", stdout); 13 | #endif 14 | cin>>n; 15 | for(int i = 1;i<=n;++i) { 16 | cin>>a[i]; 17 | dp[0][i][i] = a[i]; 18 | dp[1][i][i] = -a[i]; 19 | } 20 | for(int l = 2;l<=n;++l) { 21 | for(int i = 1;i + l - 1<=n;++i) { 22 | int j = i + l - 1; 23 | dp[0][i][j] = max(dp[1][i][j - 1] + a[j], dp[1][i + 1][j] + a[i]); 24 | dp[1][i][j] = min(dp[0][i][j - 1] - a[j], dp[0][i + 1][j] - a[i]); 25 | } 26 | } 27 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | int n, odd, even; 5 | int main() { 6 | ios::sync_with_stdio(0); 7 | cin.tie(0); 8 | cin>>n; 9 | for(int i = 1;i<=n;i++){ 10 | int x; cin>>x; 11 | if(x % 2) odd ++; 12 | else even ++; 13 | } 14 | int ans = 0; 15 | while(odd > 0 || even > 0){ 16 | if(ans % 2) { 17 | if (odd > 0) odd--; 18 | else break; 19 | } 20 | else { 21 | if (even > 0) even--; 22 | else if (odd >= 2) odd -= 2; 23 | else if (odd == 1){ 24 | ans --; 25 | break; 26 | } 27 | else break; 28 | } 29 | ans ++; 30 | } 31 | 32 | cout< 2 | using namespace std; 3 | // 0x3f3f3f3f was not big enough 4 | const int INF = 0x7f7f7f7f; 5 | int dp[1 << 21], a[21], b[21], n; 6 | 7 | int dfs(int x, int mask) { 8 | if(dp[mask] != -INF) return dp[mask]; 9 | for(int i = 0;i>n; 22 | for(int i = 0;i<(1 << n) - 1;i++) dp[i] = -INF; 23 | for(int i = 0;i>a[i]; 24 | for(int i = 0;i>b[i]; 25 | cout< 2 | using namespace std; 3 | const int N = 105; 4 | const int K = 1e5 + 5; 5 | const int mod = 1e9 + 7; 6 | int a[N], n, k, dp[N][K], psa[N][K]; 7 | 8 | int main() { 9 | cin.tie(0)->ios::sync_with_stdio(0); 10 | #ifdef LOCAL 11 | freopen("in.txt", "r", stdin); 12 | freopen("out.txt", "w", stdout); 13 | #endif 14 | cin>>n>>k; 15 | for(int i = 1;i<=n;++i) cin>>a[i]; 16 | dp[0][0] = 1; 17 | for(int i = 1;i<=n;++i) { 18 | psa[i - 1][0] = dp[i - 1][0]; 19 | for(int j = 1;j<=k;++j) 20 | psa[i - 1][j] = (psa[i - 1][j - 1] + dp[i - 1][j]) % mod; 21 | for(int j = 0;j<=k;++j) { 22 | dp[i][j] = (psa[i - 1][j] - (j - a[i] - 1 < 0 ? 0 : psa[i - 1][j - a[i] - 1]) + mod) % mod; 23 | } 24 | } 25 | cout< 2 | using namespace std; 3 | int n, flower[104], ans = 0; 4 | 5 | bool isOk(int l, int r){ 6 | int total = 0; 7 | int num = r-l+1; 8 | for(int i = l;i<=r;i++){ 9 | total +=flower[i]; 10 | } 11 | if(total % num !=0) return false; 12 | int average = total / (r-l+1); 13 | for(int i = l;i<=r;i++){ 14 | if(flower[i]==average) return true; 15 | } 16 | return false; 17 | } 18 | 19 | void solve(){ 20 | for(int i = 1;i<=n;i++){ 21 | for(int j = i;j<=n;j++){ 22 | if(isOk(i,j)){ 23 | ans++; 24 | } 25 | } 26 | } 27 | } 28 | 29 | int main() { 30 | cin>>n; 31 | for(int i = 1;i<=n;i++){ 32 | cin>>flower[i]; 33 | } 34 | ans = 0; 35 | solve(); 36 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | 5 | string s; 6 | 7 | char dir[] = {'@', 'N', 'E', 'S', 'W', 'N'}; 8 | 9 | int getDir(char a, char b){ 10 | if(a == b) return 0; 11 | for(int i = 1;i <= 5; i++){ 12 | if(dir[i] == a && dir[i + 1] == b) return 1; 13 | } 14 | return -1; 15 | } 16 | 17 | int solve(){ 18 | cin>>s; 19 | int len = s.size(); 20 | s = "@" + s; 21 | int ans = 0; 22 | for(int i = 1;i>n; 32 | while(n --){ 33 | int ans = solve(); 34 | if(ans < 0) cout<<"CCW"< 2 | #define int long long 3 | using namespace std; 4 | const int N = 1e3 + 10; 5 | const int INF = 1e9; 6 | int n, m, K, dp[N][N]; 7 | 8 | signed main(){ 9 | cin>>n>>m>>K; 10 | for(int i = 1;i<=n;i++){ 11 | for(int j = 1;j<=n;j++){ 12 | dp[i][j] = INF; 13 | } 14 | } 15 | for(int i =1 ;i<=m;i++){ 16 | int a, b, c; 17 | cin>>a>>b>>c; 18 | dp[a][b] = min(dp[a][b], c); 19 | } 20 | for(int k = 1;k<=n;k++){ 21 | for(int i = 1;i<=n;i++){ 22 | for(int j = 1;j<=n;j++){ 23 | dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); 24 | } 25 | } 26 | } 27 | for(int i = 1;i<=K;i++){ 28 | int a, b; 29 | cin>>a>>b; 30 | if(dp[a][b] == INF) cout<<"Nothing to say!"< 2 | using namespace std; 3 | const int N = 32768; 4 | int phi[N + 5], p[N + 5], tot, vis[N + 5]; 5 | 6 | int main() { 7 | cin.tie(0)->ios::sync_with_stdio(0); 8 | #ifdef LOCAL 9 | freopen("in.txt", "r", stdin); 10 | freopen("out.txt", "w", stdout); 11 | #endif 12 | phi[1] = 1; 13 | for(int i = 2;i<=N;++i) { 14 | if(!vis[i]) p[++tot] = i, phi[i] = i - 1; 15 | for(int j = 1;j<=tot && p[j] * i <= N;++j) { 16 | vis[p[j] * i] = 1; 17 | if(i % p[j] == 0) { 18 | phi[i * p[j]] = phi[i] * p[j]; 19 | break; 20 | } else phi[i * p[j]] = phi[i] * phi[p[j]]; 21 | } 22 | } 23 | int t; cin>>t; 24 | while(t--) { 25 | int n; cin>>n; 26 | cout< 2 | using namespace std; 3 | const long long N = 1e10+4; 4 | long long n; 5 | long long getNum(long long x){ 6 | return x-x/3 -x/5+x/15; 7 | } 8 | 9 | long long isOk(int num){ 10 | if(num%5 == 0||num%3==0||num%15 ==0) return false; 11 | return true; 12 | } 13 | int main() { 14 | cin>>n; 15 | long long l = 1,r = N,mid; 16 | while(r>=l){ 17 | mid = (r + l)/2; 18 | long long num = getNum(mid); 19 | if(num == n){ 20 | while(true) { 21 | if(isOk(mid)){ 22 | cout<n){ 28 | r = mid-1; 29 | } else if(num 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define pf push_front 6 | #define f first 7 | #define s second 8 | #define mp make_pair 9 | 10 | using pii = pair; 11 | 12 | const int N = 2e5 + 10; 13 | const int INF = 0x3f3f3f3f; 14 | int dp[N], n, m; 15 | deque dq; 16 | 17 | int main(){ 18 | cin>>n>>m; 19 | int ans = INF; 20 | for(int i = 1;i<=n;i++) cin>>dp[i]; 21 | for(int i = 1;i<=n;i++) { 22 | if(i > m) dp[i] += dq.front().first; 23 | while(!dq.empty() && dq.back().f > dp[i]) 24 | dq.pop_back(); 25 | dq.pb(mp(dp[i], i)); 26 | 27 | if(i > n - m) ans = min(ans, dp[i]); 28 | if(!dq.empty() && dq.front().second <= i - m) 29 | dq.pop_front(); 30 | } 31 | cout< 9 | using namespace std; 10 | 11 | #define int long long 12 | const int N = 1e5 + 10; 13 | int n, a[N], dp[2][200][4]; 14 | 15 | signed main(){ 16 | cin>>n; 17 | for(int i = 1;i<=n;++i) cin>>a[i]; 18 | int ans = 0; 19 | for(int i = 1;i<=n;++i) { 20 | ans += dp[i & 1 ^ 1][a[i]][3]; 21 | dp[i & 1][a[i]][1] = 1; 22 | for(int k = 1;k<=3;++k) { 23 | for(int j = 1;j <= 100;++j) { 24 | dp[i & 1][j][k] += dp[i & 1 ^ 1][j][k]; 25 | if(j >= a[i]) dp[i & 1][j][k] += dp[i & 1 ^ 1][j - a[i]][k - 1]; 26 | } 27 | } 28 | memset(dp[i & 1 ^ 1], 0, sizeof(dp[i & 1 ^ 1])); 29 | } 30 | cout< 2 | using namespace std; 3 | 4 | #define mp make_pair 5 | #define fir first 6 | #define sec second 7 | 8 | using ll = long long; 9 | using pii = pair; 10 | 11 | int main(){ 12 | ios::sync_with_stdio(0); 13 | cin.tie(0); 14 | int n; cin>>n; 15 | ll ans = 0; 16 | stack stk; 17 | for(int i = 1;i<=n;i++) { 18 | int x; cin>>x; 19 | while(!stk.empty() && stk.top().fir < x) { 20 | ans += stk.top().sec; 21 | stk.pop(); 22 | } 23 | int cnt = 1; 24 | if(!stk.empty() && stk.top().fir == x) { 25 | cnt += stk.top().sec; 26 | ans += stk.top().sec; 27 | stk.pop(); 28 | } 29 | ans += !stk.empty(); 30 | stk.push(mp(x, cnt)); 31 | } 32 | cout<& nums, int k) { 6 | int j = -1; 7 | map cnt; 8 | ll ans = 0; 9 | ll mx = 0; 10 | for(int i = 0;i; 2 | using ll = long long; 3 | class Solution { 4 | public: 5 | long long maxScore(vector& nums1, vector& nums2, int k) { 6 | int n = nums1.size(); 7 | vector v; 8 | for(int i = 0;i()); 12 | ll ans = 0, tot = 0; 13 | multiset bst; 14 | for(int i = 0;i k) { 18 | tot -= *bst.begin(); 19 | bst.erase(bst.begin()); 20 | } 21 | if(bst.size() == k) 22 | ans = max(ans, 1ll * v[i].first * tot); 23 | } 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /algorithms/String algorithm/LLS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 1e6 + 5; 4 | string s; 5 | int n, K, fail[N]; 6 | 7 | int main() { 8 | cin.tie(0)->ios::sync_with_stdio(0); 9 | #ifdef LOCAL 10 | freopen("in.txt", "r", stdin); 11 | freopen("out.txt", "w", stdout); 12 | #endif 13 | cin>>s>>K; 14 | n = s.size(); 15 | s = '@' + s; 16 | int i = 1, j = 2; 17 | while(i <= n - K + 1 && j <= n - K + 1) { 18 | int k = 0; 19 | while(s[i + k] == s[j + k]) ++k; 20 | if(s[i + k] > s[j + k]) i += k + 1, swap(i, j); 21 | else j += k + 1; 22 | if(i == j) ++j; 23 | } 24 | j = n - K + 1; 25 | int k = 0; 26 | while(s[i + k] == s[j + k]) ++k; 27 | if(k <= K && s[j + k] < s[i + k]) swap(i, j); 28 | for(int k = 0;k 2 | #include 3 | #define endl "\n" 4 | using namespace std; 5 | const int N = 31; 6 | unordered_map Map; 7 | int n; 8 | string a[N]; 9 | int main() { 10 | ios::sync_with_stdio(0); 11 | cin.tie(0); 12 | cin>>n; 13 | for(int i = 1;i<=n;i++){ 14 | cin >> a[i]; 15 | } 16 | for(int i = 1;i<=n;i++){ 17 | if(Map[a[i]].empty()){ 18 | cin>>Map[a[i]]; 19 | if(Map[a[i]] == a[i]) { 20 | cout<<"bad"< 2 | #include 3 | using namespace std; 4 | const int N = 2e3+10; 5 | 6 | int f[N],n,a; 7 | 8 | bool isOk(int i, int j){ 9 | return i-j<=2000; 10 | } 11 | 12 | int main() { 13 | cin>>n; 14 | for(int i = 1;i<=n;i++){ 15 | cin>>a; 16 | f[a]++; 17 | } 18 | int ans = 0; 19 | int tim = 0; 20 | for(double i = 2;i<=4000;i++){ 21 | int res = 0; 22 | for(double j = 1;j<=i/2;j++){ 23 | if(isOk(i,j)){ 24 | if(j==i/2){ 25 | res +=f[(int)j]/2; 26 | } else res+= min(f[(int)j],f[(int)(i-j)]); 27 | } 28 | } 29 | if(res>ans){ 30 | ans = res; 31 | tim = 1; 32 | } else if(res == ans){ 33 | tim++; 34 | } 35 | } 36 | cout< 2 | using namespace std; 3 | 4 | const int N = 1e3 + 10; 5 | int a[N], b[N], n, dp[N][N]; 6 | string s, t; 7 | 8 | int main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | #ifdef LOCAL 12 | freopen("in.txt", "r", stdin); 13 | freopen("out.txt", "w", stdout); 14 | #endif 15 | cin>>n>>s; 16 | s = '@' + s; 17 | for(int i = 1;i<=n;++i) cin>>a[i]; 18 | cin>>t; 19 | t = '@' + t; 20 | for(int i = 1;i<=n;++i) cin>>b[i]; 21 | for(int i = 1;i<=n;++i) { 22 | for(int j = 1;j<=n;++j) { 23 | if((s[i] == 'W' && t[j] == 'L' && a[i] > b[j]) || (s[i] == 'L' && t[j] == 'W' && a[i] < b[j])) 24 | dp[i][j] = dp[i - 1][j - 1] + a[i] + b[j]; 25 | dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1])); 26 | } 27 | } 28 | cout< 2 | using namespace std; 3 | #define int long long 4 | const int N = 2e5 + 10; 5 | int n, m, a[N], b[N], f[N]; 6 | int mpa[N], mpb[N]; 7 | 8 | signed main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | #ifdef LOCAL 12 | freopen("in.txt", "r", stdin); 13 | freopen("out.txt", "w", stdout); 14 | #endif 15 | cin>>n>>m; 16 | for(int i = 1;i<=n;++i) { 17 | cin>>a[i]>>b[i]; 18 | mpa[a[i]]++; 19 | mpb[b[i]]++; 20 | } 21 | for(int i = 0;i<=2 * m;++i) { 22 | for(int j = 0;j<=i;++j) { 23 | f[i] += mpa[j] * (mpa[i - j]); 24 | f[i + 1] -= mpb[j] * (mpb[i - j]); 25 | } 26 | } 27 | for(int i = 1;i<=2 * m;++i) { 28 | f[i] += f[i - 1]; 29 | } 30 | for(int i = 0;i<=2 * m;++i) { 31 | cout< 2 | using namespace std; 3 | const int N = 1e6 + 10; 4 | string a, b; 5 | 6 | int fail[N]; 7 | 8 | int main() { 9 | ios::sync_with_stdio(0); 10 | cin.tie(0); 11 | #ifdef LOCAL 12 | freopen("in.txt", "r", stdin); 13 | freopen("out.txt", "w", stdout); 14 | #endif 15 | cin>>a>>b; 16 | fail[0] = -1; 17 | for(int i = 1;i<=b.size();++i) { 18 | int j = fail[i - 1]; 19 | while(j >= 0 && b[j + 1] != b[i]) j = fail[j]; 20 | if(b[j + 1] == b[i]) ++j; 21 | fail[i] = j; 22 | } 23 | int j = -1; 24 | int ans = 0; 25 | for(int i = 0;i= 0 && b[j + 1] != a[i]) j = fail[j]; 27 | if(b[j + 1] == a[i]) { 28 | ++j; 29 | if(j + 1 == b.size()) ans++; 30 | } 31 | } 32 | cout< 3 | using namespace std; 4 | const int N = 1e5 + 10; 5 | int n, a[N]; 6 | 7 | int main() { 8 | #ifdef LOCAL 9 | freopen("in.txt", "r", stdin); 10 | freopen("out.txt", "w", stdout); 11 | #endif 12 | scanf("%d", &n); 13 | for(int i = 1;i<=n;++i) scanf("%d", a + i); 14 | int x; 15 | for(int i = 1;i<=n;++i) scanf("%d", &x), a[i] -= x; 16 | int l = 0, ans = 0; 17 | while(l <= n) { 18 | while(l <= n && a[l] == 0) l++; 19 | if(l > n) break; 20 | int r = l; 21 | while((a[r] < 0 && a[l] < 0) || (a[r] > 0 && a[l] > 0) && r <= n) ++r; 22 | int c = a[l]; 23 | for(int i = l;i 0 ? min(c, a[i]) : max(c, a[i]); 24 | for(int i = l;i 2 | #define endl "\n" 3 | using namespace std; 4 | using ll = long long; 5 | const int N = 2e5 + 10; 6 | int n; 7 | vector c(N); 8 | vector pre(N); 9 | 10 | int lowbit(int x) {return x & (-x);} 11 | 12 | void update(int pos, int val){ 13 | for(int i = pos;i<=n;i+=lowbit(i)) c[i] += val; 14 | } 15 | 16 | int query(int pos){ 17 | ll ans = 0; 18 | for(int i = pos;i;i-=lowbit(i)) ans += c[i]; 19 | return ans; 20 | } 21 | 22 | int main(){ 23 | ios::sync_with_stdio(0); 24 | cin.tie(0); 25 | cout.tie(0); 26 | cin>>n; 27 | ll ans = 0; 28 | for(int i = 1;i<=n;i++) { 29 | int x; cin>>x; 30 | ans += query(i) - query(pre[x]); 31 | update(i, 1); 32 | if(pre[x]) update(pre[x], -1); 33 | pre[x] = i; 34 | } 35 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | using ll = long long; 5 | const int N = 2e5 + 10; 6 | const ll INF = 4e9; 7 | int n; 8 | ll c, a[N], b[N]; 9 | 10 | inline void solve(){ 11 | cin >> n >> c; 12 | ll ans = INF; 13 | for(int i = 1;i<=n;i++) cin>>a[i]; 14 | for(int i = 1;i>b[i]; 15 | ll k = 0; 16 | ll cur = 0; 17 | for(int i = 1;i<=n;i++){ 18 | ans = min(ans, (c - cur - 1) / a[i] + 1 + k); 19 | if(i == n) continue; 20 | ll days = 0; 21 | if(cur < b[i]) days = (b[i] - cur - 1) / a[i] + 1; 22 | cur = a[i] * (days++) + cur - b[i]; 23 | k += days; 24 | } 25 | cout<>t; 32 | while(t--){ 33 | solve(); 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /other solutions/最大子序和.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mp make_pair 4 | #define f first 5 | #define s second 6 | using vi = vector; 7 | using pii = pair; 8 | deque dq; 9 | 10 | int main(){ 11 | ios::sync_with_stdio(0); 12 | cin.tie(0); 13 | int n, m, ans = 0; 14 | cin>>n>>m; 15 | vi a(n + 1), dp(n + 1), sum(n + 1); // dp[i] means the maximum end and include at position i 16 | for(int i = 1;i<=n;i++) { 17 | cin>>a[i]; 18 | sum[i] = sum[i - 1] + a[i]; 19 | } 20 | dq.push_back(mp(0, 0)); 21 | for(int i = 1;i<=n;i++) { 22 | while(!dq.empty() && dq.back().f >= sum[i]) dq.pop_back(); 23 | dq.push_back(mp(sum[i], i)); 24 | dp[i] += sum[i] - dq.front().f; 25 | ans = max(dp[i], ans); 26 | if(dq.front().s <= i - m) dq.pop_front(); 27 | } 28 | cout< 2 | using namespace std; 3 | 4 | const int N = 5e3 + 5; 5 | int n, h[N], ans[N]; 6 | 7 | int main() { 8 | cin.tie(0)->ios::sync_with_stdio(0); 9 | #ifdef LOCAL 10 | freopen("in.txt", "r", stdin); 11 | freopen("out.txt", "w", stdout); 12 | #endif 13 | cin>>n; 14 | memset(ans, 0x3f, sizeof ans); 15 | for(int i = 1;i<=n;++i) cin>>h[i]; 16 | for(int i = 1;i<=n;++i) { 17 | int val = 0; 18 | for(int j = 0;i-j>0 && i + j<=n;++j) { 19 | val += abs(h[i - j] - h[i + j]); 20 | ans[j * 2 + 1] = min(ans[j * 2 + 1], val); 21 | } 22 | int val2 = 0; 23 | for(int j = 0;i-j>0 && i + j + 1<=n;++j) { 24 | val2 += abs(h[i - j] - h[i + j + 1]); 25 | ans[(j + 1) * 2] = min(ans[(j + 1) * 2], val2); 26 | } 27 | } 28 | for(int i = 1;i<=n;++i) cout< 2 | using namespace std; 3 | using ll = long long; 4 | const int N = 1e5 + 5; 5 | const ll INF = 1e18; 6 | int n; 7 | ll x[N], y[N], sx[N], sy[N]; 8 | 9 | int main() { 10 | cin.tie(0)->ios::sync_with_stdio(0); 11 | #ifdef LOCAL 12 | freopen("in.txt", "r", stdin); 13 | freopen("out.txt", "w", stdout); 14 | #endif 15 | cin>>n; 16 | for(int i = 1;i<=n;++i) cin>>x[i]>>y[i]; 17 | sort(x + 1, x + n + 1); 18 | sort(y + 1, y + n + 1); 19 | for(int i = 1;i<=n;++i) { 20 | sx[i] = sx[i - 1] + x[i]; 21 | sy[i] = sy[i - 1] + y[i]; 22 | } 23 | ll mnx = INF, mny = INF; 24 | for(int i = 1;i<=n;++i) { 25 | mnx = min(mnx, x[i] * i - sx[i] + (sx[n] - sx[i]) - x[i] * (n - i)); 26 | mny = min(mny, y[i] * i - sy[i] + (sy[n] - sy[i]) - y[i] * (n - i)); 27 | } 28 | cout< 3 | using namespace std; 4 | const int N = 1e6 + 5; 5 | #define fir first 6 | #define sec second 7 | int n, a[N], cnt[N]; 8 | double dp[N]; 9 | 10 | int main() { 11 | #ifdef LOCAL 12 | freopen("in.txt", "r", stdin); 13 | freopen("out.txt", "w", stdout); 14 | #endif 15 | scanf("%d", &n); 16 | for(int i = 1;i<=n;++i) scanf("%d", a + i); 17 | if(a[1] == a[n]) { 18 | puts("1"); 19 | return 0; 20 | } 21 | dp[a[1]] = 1; 22 | dp[a[n]] = 0; 23 | cnt[a[1]]++; 24 | cnt[a[n]]++; 25 | double ans = 0.5; 26 | for(int i = n - 1;i>1;--i) { 27 | if(cnt[a[i]] == 0) dp[a[i]] = ans; 28 | ans = (ans * (n - i + 1) + dp[a[i]]) / (n - i + 2); 29 | ++cnt[a[i]]; 30 | } 31 | ans = 0; 32 | for(int i = 1;i<=n;++i) ans += dp[i] * cnt[i] / n; 33 | printf("%.10f\n", ans); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /USACO/2022/Open/Silver/P2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | string s, t; 4 | bool ok[20][20]; 5 | 6 | int main() { 7 | cin.tie(0)->ios::sync_with_stdio(0); 8 | #ifdef LOCAL 9 | freopen("in.txt", "r", stdin); 10 | freopen("out.txt", "w", stdout); 11 | #endif 12 | cin>>s>>t; 13 | for(char i = 'a';i<='r';++i) { 14 | for(char j = 'a';j<='r';++j) { 15 | string x, y; 16 | for(auto c : s) if(c == i || c == j) x += c; 17 | for(auto c : t) if(c == i || c == j) y += c; 18 | ok[i][j] = x == y; 19 | } 20 | } 21 | int q; cin>>q; 22 | while(q--) { 23 | bool flag = 1; 24 | string x; cin>>x; 25 | for(auto c : x) { 26 | for(auto d : x) { 27 | flag &= ok[c][d]; 28 | } 29 | } 30 | putchar(flag ? 'Y' : 'N'); 31 | } 32 | putchar('\n'); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /algorithms/number theory/倍数问题.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | using ll = long long; 4 | string a; 5 | int b; 6 | const int N = 1e7 + 10; 7 | struct bigInt { 8 | int num[N], len = 0; 9 | void build(string& s) { 10 | len = s.size(); 11 | for(int i = 1;i<=s.size();++i) { 12 | num[i] = s[i - 1] - '0'; 13 | } 14 | } 15 | ll operator%(const int& a) const { 16 | ll ans = 0; 17 | for(int i = 1;i<=len;++i) { 18 | ans = ans * 10 % a + num[i]; 19 | ans %= a; 20 | } 21 | return ans; 22 | } 23 | } n; 24 | 25 | int main() { 26 | #ifdef LOCAL 27 | freopen("in.txt", "r", stdin); 28 | freopen("out.txt", "w", stdout); 29 | #endif 30 | ios::sync_with_stdio(0); 31 | cin.tie(0); 32 | cin>>a; 33 | cin>>b; 34 | n.build(a); 35 | cout<<(n % b == 0 ? "YES" : "NO")<<'\n'; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /algorithms/Dynamic Programming/getLIS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N =1e4+10; 4 | 5 | int n,a[N],dp[N]; 6 | int len; 7 | 8 | int binarySearch(int x){ 9 | int l = 1, r = len, mid; 10 | int ans; 11 | while(l<=r){ 12 | mid = (l + r)/2; 13 | if(dp[mid] < x) l = mid + 1; 14 | else { 15 | r = mid - 1; 16 | ans = mid; 17 | } 18 | } 19 | return ans; 20 | } 21 | 22 | int main() { 23 | cin>>n; 24 | for(int i = 1;i<=n;i++){ 25 | cin>>a[i]; 26 | } 27 | len = 1; 28 | dp[1] = a[1]; 29 | for(int i = 2;i<=n;i++){ 30 | if(a[i]>dp[len]) dp[++len] = a[i]; 31 | else{ 32 | int k = binarySearch(a[i]); 33 | dp[k] = a[i]; 34 | } 35 | } 36 | int ans; 37 | for(int i = 1;i<=n;i++){ 38 | if(dp[i]>0) ans = i; 39 | } 40 | cout< 2 | using namespace std; 3 | #define pb push_back 4 | using ull = unsigned long long; 5 | string a, b; 6 | 7 | ull base = 131; 8 | ull key = 0; 9 | 10 | vector p(1); 11 | vector h; 12 | 13 | int main() { 14 | ios::sync_with_stdio(0); 15 | cin.tie(0); 16 | #ifdef LOCAL 17 | freopen(".in", "r", stdin); 18 | freopen(".out", "w", stdout); 19 | #endif 20 | cin>>a>>b; 21 | p[0] = 1; 22 | h.resize(a.size() + 10); 23 | for(int i = 1;i<=max(a.size(), b.size());++i) p.pb(p[i - 1] * base); 24 | for(char c : b) key = key * base + c; 25 | a = '@' + a; 26 | int ans = 0; 27 | for(int i = 1;i 2 | using namespace std; 3 | const int N = 3e4 + 10; 4 | int n, a[N]; 5 | int solve(){ 6 | cin>>n; 7 | int c0 = 0, c1 = 0, c2 = 0; 8 | for(int i =1;i<=n;i++){ 9 | cin>>a[i]; 10 | if(a[i] %3 == 0) c0 ++; 11 | else if(a[i] % 3 == 1) c1 ++; 12 | else c2 ++; 13 | } 14 | int ans = 0; 15 | while(c0 != c1 || c1 != c2){ 16 | if(c0 > c1){ 17 | c0--; 18 | c1 ++; 19 | ans ++; 20 | } 21 | if(c1 > c2){ 22 | c1 --; 23 | c2 ++; 24 | ans++; 25 | } 26 | if(c2 > c0){ 27 | c2--; 28 | c0++; 29 | ans++; 30 | } 31 | } 32 | return ans; 33 | } 34 | 35 | int t; 36 | int main() { 37 | cin>>t; 38 | while(t--){ 39 | cout< 2 | using namespace std; 3 | const int N = 2e5 + 5; 4 | int n, a[N], b[N], ans; 5 | 6 | int main() { 7 | cin.tie(0)->ios::sync_with_stdio(0); 8 | #ifdef LOCAL 9 | freopen("in.txt", "r", stdin); 10 | freopen("out.txt", "w", stdout); 11 | #endif 12 | cin>>n; 13 | for(int i = 1;i<=n;++i) cin>>a[i]; 14 | for(int i = 1;i<=n;++i) cin>>b[i]; 15 | for(int i = 1;i<=n;++i) { 16 | if(a[i] == 1) { 17 | ans += (a[i - 1] != 1) + (a[i + 1] != 1); 18 | } 19 | if(b[i] == 1) { 20 | ans += (b[i - 1] != 1) + (b[i + 1] != 1); 21 | } 22 | } 23 | for(int i = 1;i<=n;++i) { 24 | if(i & 1 ^ 1) { 25 | ans += (a[i] == 1) + (b[i] == 1); 26 | } else { 27 | if(a[i] == 1) ans += b[i] != 1; 28 | if(b[i] == 1) ans += a[i] != 1; 29 | } 30 | } 31 | cout< 2 | #include 3 | using namespace std; 4 | struct p{ 5 | int f, s, time; 6 | p(int F, int S, int T){ 7 | f = F, s = S, time = T; 8 | } 9 | }; 10 | int w, h, n; 11 | void solve(){ 12 | queue

q; 13 | cin>>w>>h>>n; 14 | q.push(p(w, h,1)); 15 | int ans = 0; 16 | while(!q.empty()){ 17 | p cur = q.front(); q.pop(); 18 | if(cur.f %2 == 0){ 19 | cur.f /= 2; 20 | cur.time*= 2; 21 | q.push(cur); 22 | } else if( cur.s %2 == 0){ 23 | cur.s /=2; 24 | cur.time *=2; 25 | q.push(cur); 26 | } else { 27 | ans += cur.time; 28 | } 29 | } 30 | if(ans >= n) cout<<"YES"<>t; 36 | while(t--){ 37 | solve(); 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /USACO/2021/Jan/Bronze/Just Stalling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define endl "\n" 4 | using namespace std; 5 | typedef long long ll; 6 | const int N = 30; 7 | int n, a[N], b[N]; 8 | bool cmp(int x, int y){ 9 | return x > y; 10 | } 11 | 12 | int binarySearch(int x){ 13 | int l = 1, r = n, ans = 0; 14 | while(l <= r){ 15 | int mid = (l + r) >> 1; 16 | if(b[mid] >= x){ 17 | ans = mid; 18 | l = mid + 1; 19 | } else r = mid - 1; 20 | } 21 | return ans; 22 | } 23 | 24 | int main() { 25 | ios::sync_with_stdio(0); 26 | cin.tie(0); 27 | cin>>n; 28 | for(int i = 1;i<=n;i++) cin>>a[i]; 29 | for(int i = 1;i<=n;i++) cin>>b[i]; 30 | sort(b + 1, b + n + 1, cmp); 31 | sort(a + 1, a + n + 1, cmp); 32 | ll ans = 1; 33 | for(int i = 1;i<=n;i++){ 34 | ans *= binarySearch(a[i]) - i + 1; 35 | } 36 | cout< 2 | #include 3 | #define endl "\n" 4 | using namespace std; 5 | const int N = 2e3 + 10; 6 | const int INF = 1e5 + 10; 7 | int n, dp[N]; 8 | unordered_set vis; 9 | 10 | inline void solve(){ 11 | for(int i = 1;i>s; 15 | for(int j = 1;j<=s.size();j++){ 16 | for(int l = 1; l <= s.size(); l++){ 17 | int i = j - l + 1; 18 | if(i < 1) continue; 19 | if(vis.count(s.substr(i - 1, l))) dp[j] = min(dp[j], dp[i - 1] + 1); 20 | } 21 | } 22 | cout<>n; 29 | for(int i = 1;i<=n;i++){ 30 | string s; cin>>s; 31 | vis.insert(s); 32 | } 33 | for(int i = 1;i<=10;i++){ 34 | solve(); 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /CCO/17/P4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | using ll = long long; 5 | const int N = 505; 6 | const int MX = 25005; 7 | int n, a[N]; 8 | ll dp[2][MX]; 9 | 10 | int main() { 11 | cin.tie(0)->ios::sync_with_stdio(0); 12 | cin>>n; 13 | for(int i = 1;i<=n;++i) cin>>a[i]; 14 | sort(a + 1, a + n + 1, greater()); 15 | a[1] = 51; 16 | dp[1][0] = 1ll << a[1]; 17 | for(int i = 2;i<=n;++i) { 18 | for(int j = 0;j> k & 1) { 22 | if(j + k - a[i] < MX) 23 | dp[i&1][j + k - a[i]] |= dp[i&1^1][j]; 24 | } 25 | } 26 | memset(dp[i&1^1], 0, sizeof dp[i&1^1]); 27 | } 28 | for(int i = 0;i 10 | #define read for(int i = 1;i<=n;i++) cin>>a[i]; 11 | using namespace std; 12 | const int N = 3e2 + 10; 13 | int n,a[N],dp[N][N]; 14 | int main(){ 15 | cin>>n; 16 | read; 17 | for(int i = 1;i<=n;i++){ 18 | for(int j = 1;j<=n;j++){ 19 | dp[i][j] = j - i + 1; 20 | } 21 | } 22 | for(int l = 2;l<=n;l++){ 23 | for(int i = 1;i<=n;i++){ 24 | int j = l + i - 1; 25 | if(j > n) continue; 26 | if(a[i] == a[j]) dp[i][j] = min(dp[i + 1][j], dp[i][j - 1]); 27 | for(int k = i;k 0: 19 | if(tests[0][1] >= inputs[1]): 20 | tests.popleft() 21 | else: 22 | timesCan = inputs[1] - tests[0][1] #amount of marks you can get on specific test 23 | 24 | if(amount > timesCan): 25 | time += tests[0][0] * timesCan 26 | tests[0][1] += 1 * timesCan 27 | amount -= 1 * timesCan 28 | else: 29 | time += tests[0][0] * amount 30 | tests[0][1] += 1 * amount 31 | amount -= 1 * amount 32 | 33 | print(time) 34 | -------------------------------------------------------------------------------- /CCC/2015/15S3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | vector fre; 6 | 7 | int g, p; 8 | 9 | int binarySearch(int x){ 10 | int ans = -1, l = 0, r = fre.size() - 1, mid; 11 | while(l<=r){ 12 | mid = (l + r) / 2; 13 | if(fre[mid] == x) return mid; 14 | else if(fre[mid] < x){ 15 | ans = mid; 16 | l = mid +1; 17 | } else r = mid - 1; 18 | } 19 | return ans; 20 | } 21 | 22 | int main() { 23 | cin>>g>>p; 24 | int ans = 0; 25 | for(int i = 1;i<=g;i++){ 26 | fre.push_back(i); 27 | } 28 | int a; 29 | for(int i = 1;i<=p;i++){ 30 | cin>>a; 31 | int k = binarySearch(a); 32 | if(k == -1){ 33 | cout< 2 | using namespace std; 3 | 4 | using vi = vector; 5 | using ll = long long; 6 | 7 | const int N = 1e5 + 10; 8 | 9 | ll dp[N][2], a[N], sum[N]; 10 | int n, k; 11 | deque dq; 12 | 13 | int main(){ 14 | ios::sync_with_stdio(0); 15 | cin.tie(0); 16 | cin>>n>>k; 17 | for(int i = 1;i<=n;i++) { 18 | cin>>a[i]; 19 | sum[i] = sum[i - 1] + a[i]; 20 | } 21 | dq.push_back(0); 22 | for(int i = 1;i<=n;i++) { 23 | dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]); 24 | if(dq.empty()) dp[i][1] = sum[i]; 25 | else dp[i][1] = dp[dq.front()][0] - sum[dq.front()] + sum[i]; 26 | while(!dq.empty() && dp[dq.back()][0] - sum[dq.back()] < dp[i][0] - sum[i]) dq.pop_back(); 27 | if(!dq.empty() && dq.front() <= i - k) dq.pop_front(); 28 | dq.push_back(i); 29 | } 30 | cout< 2 | using namespace std; 3 | const int N = 5e4 + 10; 4 | #define pb push_back 5 | using pii = pair; 6 | #define fir first 7 | #define sec second 8 | vector e[N]; 9 | 10 | int n, siz[N], mx = 0, tot = 0; 11 | 12 | void dfs1(int u, int fa = 0, int dist = 0) { 13 | siz[u] = siz[fa] + dist; 14 | mx = max(mx, siz[u]); 15 | tot += dist; 16 | for(pii v : e[u]) { 17 | if(fa == v.fir) continue; 18 | dfs1(v.fir, u, v.sec); 19 | } 20 | tot += dist; 21 | } 22 | 23 | int main() { 24 | ios::sync_with_stdio(0); 25 | cin.tie(0); 26 | #ifdef LOCAL 27 | freopen("in.txt", "r", stdin); 28 | freopen("out.txt", "w", stdout); 29 | #endif 30 | cin>>n; 31 | for(int i = 1;i>u>>v>>w; 34 | e[u].pb({v, w}); 35 | e[v].pb({u, w}); 36 | } 37 | dfs1(1); 38 | cout< 3 | using namespace std; 4 | 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | using ull = unsigned long long; 9 | const int N = 5e6 + 5; 10 | const int base = 131; 11 | ull h[N], p[N]; 12 | string s; 13 | int n, k; 14 | gp_hash_table mp; 15 | 16 | int main() { 17 | cin.tie(0)->ios::sync_with_stdio(0); 18 | #ifdef LOCAL 19 | freopen("in.txt", "r", stdin); 20 | freopen("out.txt", "w", stdout); 21 | #endif 22 | cin>>s>>k; 23 | n = s.size(); 24 | s = '@' + s; 25 | p[0] = 1; 26 | for(int i = 1;i<=n;++i) { 27 | h[i] = h[i - 1] * base + s[i]; 28 | p[i] = p[i - 1] * base; 29 | } 30 | for(int i = k;i<=n;++i) { 31 | ++mp[h[i] - h[i - k] * p[k]]; 32 | } 33 | int ans = 0; 34 | for(auto x : mp) { 35 | ans = max(ans, x.second); 36 | } 37 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | 5 | int n, Map[3010][3010]; 6 | bool vis[3010][3010]; 7 | int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; 8 | 9 | int getComfortable(int x, int y){ 10 | int ans = 0; 11 | vis[x][y] = true; 12 | if(Map[x][y] == 3) ans ++; 13 | for(int i = 0;i<4;i++){ 14 | int a = x + dir[i][0]; 15 | int b = y + dir[i][1]; 16 | Map[a][b] ++; 17 | if(vis[a][b]){ 18 | if(Map[a][b] == 4) ans --; 19 | else if(Map[a][b] == 3) ans ++; 20 | } 21 | } 22 | return ans; 23 | } 24 | 25 | int main() { 26 | ios::sync_with_stdio(0); 27 | cin.tie(0); 28 | cin>>n; 29 | int ans = 0; 30 | while(n--){ 31 | int x, y; 32 | cin>>x>>y; 33 | x += 1000, y += 1000; 34 | ans += getComfortable(x, y); 35 | cout< 2 | using namespace std; 3 | const int N = 2e5 + 5; 4 | int c[N], o[N], w[N]; 5 | string s; 6 | int n; 7 | 8 | int main() { 9 | #ifdef LOCAL 10 | freopen("in.txt", "r", stdin); 11 | freopen("out.txt", "w", stdout); 12 | #endif 13 | cin>>s; 14 | n = s.size(); 15 | s = '@' + s; 16 | for(int i = 1;i<=n;++i) { 17 | c[i] = c[i - 1]; 18 | o[i] = o[i - 1]; 19 | w[i] = w[i - 1]; 20 | if(s[i] == 'C') c[i] ^= 1; 21 | if(s[i] == 'O') o[i] ^= 1; 22 | if(s[i] == 'W') w[i] ^= 1; 23 | } 24 | int q; cin>>q; 25 | while(q--) { 26 | int u, v; cin>>u>>v; 27 | int a = c[v] ^ c[u - 1]; 28 | int b = o[v] ^ o[u - 1]; 29 | int c = w[v] ^ w[u - 1]; 30 | if(a == 1 && b == 0 && c == 0) cout<<'Y'; 31 | else if(a == 0 && b == 1 && c == 1) cout<<'Y'; 32 | else cout<<'N'; 33 | } 34 | cout<<'\n'; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /algorithms/game_theory/ICG.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define pb push_back 4 | const int N = 2005; 5 | int n, m, k, sg[N]; 6 | bitset vis; 7 | vector e[N]; 8 | 9 | int dfs(int u) { 10 | if(vis[u]) return sg[u]; 11 | vis[u] = 1; 12 | sg[u] = 0; 13 | unordered_set tmp; 14 | for(int v : e[u]) tmp.insert(dfs(v)); 15 | while(tmp.count(sg[u])) ++sg[u]; 16 | return sg[u]; 17 | } 18 | 19 | int main() { 20 | #ifdef LOCAL 21 | freopen("in.txt", "r", stdin); 22 | freopen("out.txt", "w", stdout); 23 | #endif 24 | scanf("%d%d%d", &n, &m, &k); 25 | for(int i = 1;i<=m;++i) { 26 | int u, v; 27 | scanf("%d%d", &u, &v); 28 | e[u].pb(v); 29 | } 30 | for(int i = 1;i<=n;++i) dfs(i); 31 | int ans = 0; 32 | for(int i = 1;i<=k;++i) { 33 | int x; 34 | scanf("%d", &x); 35 | ans ^= sg[x]; 36 | } 37 | puts(ans ? "win" : "lose"); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /CSP/2022/J/T4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | using pii = pair; 4 | const int N = 505; 5 | int n, k, dp[N][N]; 6 | #define x(i) a[i].first 7 | #define y(i) a[i].second 8 | pii a[N]; 9 | 10 | int main() { 11 | cin.tie(0)->ios::sync_with_stdio(0); 12 | #ifdef LOCAL 13 | freopen("in.txt", "r", stdin); 14 | freopen("out.txt", "w", stdout); 15 | #endif 16 | cin>>n>>k; 17 | int ans = 0; 18 | for(int i = 1;i<=n;++i) { 19 | cin>>x(i)>>y(i); 20 | } 21 | sort(a + 1, a + n + 1); 22 | for(int i = 1;i<=n;++i) { 23 | dp[i][0] = 1; 24 | for(int j = 1;j= y(j)) { 25 | int dist = abs(x(i) - x(j)) + abs(y(i) - y(j)) - 1; 26 | for(int l = dist;l<=k;++l) { 27 | dp[i][l] = max(dp[i][l], dp[j][l - dist] + dist + 1); 28 | ans = max(dp[i][l] + k - l, ans); 29 | } 30 | } 31 | } 32 | cout< 2 | using namespace std; 3 | 4 | const int INF = 0x3f3f3f3f; 5 | const int N = 1e6 + 5; 6 | int a[N], n; 7 | vector stk; 8 | 9 | int main() { 10 | cin.tie(0)->ios::sync_with_stdio(0); 11 | #ifdef LOCAL 12 | freopen("in.txt", "r", stdin); 13 | freopen("out.txt", "w", stdout); 14 | #endif 15 | cin>>n; 16 | for(int i = 1;i<=n;++i) cin>>a[i]; 17 | a[0] = INF, a[n + 1] = INF; 18 | stk.push_back(0); 19 | long long ans = 0; 20 | for(int i = 1;i<=n;++i) { 21 | while(a[stk.back()] < a[i]) stk.pop_back(); 22 | ans += stk.back() == 0 ? 0 : i - stk.back() + 1; 23 | stk.push_back(i); 24 | } 25 | stk.clear(); 26 | stk.push_back(n + 1); 27 | for(int i = n;i;--i) { 28 | while(a[stk.back()] < a[i]) stk.pop_back(); 29 | ans += stk.back() == n + 1 ? 0 : stk.back() - i + 1; 30 | stk.push_back(i); 31 | } 32 | cout< 2 | #include 3 | #include 4 | #define endl "\n" 5 | using namespace std; 6 | const int N = 18; 7 | const int INF = 0x3f3f3f3f; 8 | int e[N][N], n, dp[N][1 << N], last; 9 | 10 | inline int dfs(int u, int mask){ 11 | if(dp[u][mask] >= INF){ 12 | if(mask == last) dp[u][mask] = e[u][0]; 13 | else { 14 | for(int v = 0;v<=n;v++){ 15 | if(mask & 1 << v) continue; 16 | dp[u][mask] = min(dp[u][mask], dfs(v, mask | 1 << v) + e[u][v]); 17 | } 18 | } 19 | } 20 | return dp[u][mask]; 21 | } 22 | 23 | int main(){ 24 | ios::sync_with_stdio(0); 25 | cin.tie(0); 26 | cout.tie(0); 27 | cin>>n; 28 | last = (1 << (n + 1)) - 1; 29 | memset(dp, 63, sizeof(dp)); 30 | for(int i = 0;i<=n;i++) 31 | for(int j = 0;j<=n;j++) 32 | cin>>e[i][j]; 33 | dfs(0, 1); 34 | cout< fv: 16 | f[fv] = fu 17 | else: 18 | f[fu] = fv 19 | 20 | for i in range(n): 21 | a = list(map(int, input().split(' '))) 22 | for j in range(n): 23 | if a[j] == 1: 24 | merge(i + 1, j + 1) 25 | 26 | for i in range(n): 27 | ans[Find(i + 1)].append(i + 1) 28 | for i in range(n): 29 | if len(ans[Find(i + 1)]) > 0 and not vis[Find(i + 1)]: 30 | vis[Find(i + 1)] = True 31 | ans[Find(i + 1)].sort() 32 | for j in range(len(ans[Find(i + 1)]) - 1): 33 | print(ans[Find(i + 1)][j], end=' ') 34 | print(ans[Find(i + 1)][len(ans[Find(i + 1)]) - 1]) 35 | -------------------------------------------------------------------------------- /USACO/2020/Jan/Silver/Loan_Repayment.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | using ll = long long; 4 | 5 | ll N, k, m; 6 | 7 | bool ok(ll x) { 8 | ll n = N; 9 | ll cnt = 0; 10 | while(n > 0) { 11 | ll y = n / x; 12 | if(y <= m) { 13 | cnt += (n - 1) / m + 1; 14 | n = 0; 15 | } else { 16 | cnt += (n - x * y + 1) / y + 1; 17 | n -= ((n - x * y + 1) / y + 1) * y; 18 | } 19 | } 20 | return cnt <= k; 21 | } 22 | 23 | int main() { 24 | ios::sync_with_stdio(0); 25 | cin.tie(0); 26 | freopen("loan.in", "r", stdin); 27 | freopen("loan.out", "w", stdout); 28 | cin>>N>>k>>m; 29 | ll l = 1, r = 1e12; 30 | ll ans = 0; 31 | while(l <= r) { 32 | ll mid = (l + r) >> 1; 33 | if(ok(mid)) { 34 | l = mid + 1; 35 | ans = mid; 36 | } 37 | else r = mid - 1; 38 | } 39 | cout< 2 | using namespace std; 3 | #define pb push_back 4 | const int N = 1e3 + 10; 5 | int n, m, cnt = 0; 6 | vector e[N]; 7 | bool vis[N], in[N], incycle[N]; 8 | 9 | void dfs(int u) { 10 | if(in[u]) { 11 | ++cnt; 12 | incycle[u] = 1; 13 | } 14 | if(vis[u]) return; 15 | in[u] = vis[u] = 1; 16 | for(int v : e[u]) { 17 | if(incycle[v]) ++cnt; 18 | dfs(v); 19 | incycle[u] |= incycle[v]; 20 | } 21 | in[u] = 0; 22 | } 23 | 24 | int main() { 25 | scanf("%d %d", &n, &m); 26 | while(m--) { 27 | int a, b; 28 | scanf("%d %d", &a, &b); 29 | e[a].pb(b); 30 | } 31 | for(int i = 1;i<=n;++i) { 32 | memset(incycle, 0, sizeof(in)); 33 | if(!vis[i]) dfs(i); 34 | } 35 | cnt > 0 ? (cnt > 1 ? printf("Infinite Loops Present") : printf("Infinite Loop Present")) : printf("No Infinite Loops"); 36 | printf("\n"); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /USACO/2020/Dec/Silver/Cowntagion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | const int N = 1e5+32; 6 | int n,ans = 0,a,b; 7 | 8 | vector road[N]; 9 | bool vis[N]; 10 | queue q; 11 | 12 | int main() { 13 | cin>>n; 14 | for(int i = 1;i<=n-1;i++){ 15 | cin>>a>>b; 16 | road[a].push_back(b); 17 | road[b].push_back(a); 18 | } 19 | q.push(1); 20 | vis[1] = true; 21 | int cur; 22 | while(!q.empty()){ 23 | 24 | cur = q.front(); q.pop(); 25 | vis[cur] = true; 26 | if(cur!=1) ans++; 27 | int cow =1; 28 | 29 | int total = road[cur].size(); 30 | for(int i = 0;i 2 | using namespace std; 3 | 4 | const int N = 300; 5 | int n, b, f[N]; 6 | 7 | int main() { 8 | freopen("snowboots.in", "r", stdin); 9 | freopen("snowboots.out", "w", stdout); 10 | cin>>n>>b; 11 | for(int i = 1;i<=n;++i) cin>>f[i]; 12 | int cur = 1; 13 | for(int i = 1;i<=b;++i) { 14 | int s, d; 15 | cin>>s>>d; 16 | int last = cur; 17 | while(f[cur] > s) --cur; 18 | while(cur < n) { 19 | bool flag = 0; 20 | int nxt = cur; 21 | for(int k = 1;k<=d;++k) { 22 | if(f[cur + k] <= s) { 23 | nxt = cur + k; 24 | flag = 1; 25 | } 26 | } 27 | cur = nxt; 28 | if(!flag) break; 29 | } 30 | if(last > cur) cur = last; 31 | if(cur >= n) { 32 | cout<<(i - 1)<<'\n'; 33 | return 0; 34 | } 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /algorithms/String algorithm/manacher.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // make sure 2e5 because of the hashtags 6 | const int N = 2e5 + 10; 7 | string s; 8 | int p[N]; 9 | 10 | int main() { 11 | ios::sync_with_stdio(0); 12 | cin.tie(0); 13 | #ifdef LOCAL 14 | freopen(".in", "r", stdin); 15 | freopen(".out", "w", stdout); 16 | #endif 17 | cin>>s; 18 | int n = s.size(); 19 | s = '@' + s; 20 | // replace with hash 21 | string tmp = "@#"; 22 | for(int i = 1;i<=n;++i) { 23 | tmp += s[i]; 24 | tmp += '#'; 25 | } 26 | s = tmp; 27 | n = s.size() - 1; 28 | int mid = 1, r = 1; 29 | int ans = 0; 30 | for(int i = 1;i<=n;++i) { 31 | if(i < r) p[i] = min(p[mid * 2 - i], r - i); 32 | while(i > p[i] + 1 && p[i] + 1 + i <= n && s[i - (p[i] + 1)] == s[i + p[i] + 1]) p[i]++; 33 | if(i + p[i] > r) mid = i, r = i + p[i]; 34 | ans = max(ans, p[i]); 35 | } 36 | cout< 2 | using namespace std; 3 | #define int long long 4 | using pii = pair; 5 | #define fir first 6 | #define sec second 7 | #define pb push_back 8 | #define mp make_pair 9 | const int N = 1e5 + 10; 10 | int n, c, k, a[N], ctot[N], ktot[N], ans; 11 | vector e[N]; 12 | 13 | void dfs(int u, int fa, int dist) { 14 | ctot[u] = a[u]; 15 | ktot[u] = dist; 16 | for(pii v : e[u]) { 17 | if(v.fir == fa) continue; 18 | dfs(v.fir, u, v.sec); 19 | ctot[u] += ctot[v.fir]; 20 | ktot[u] += ktot[v.fir]; 21 | } 22 | } 23 | 24 | signed main() { 25 | cin>>n>>c>>k; 26 | for(int i = 1;i<=n;++i) cin>>a[i]; 27 | for(int i = 1;i>u>>v>>w; 30 | e[u].pb(mp(v, w)); 31 | e[v].pb(mp(u, w)); 32 | } 33 | dfs(1, 0, 0); 34 | for(int i = 2;i<=n;++i) { 35 | if(ctot[i] >= c && ktot[i] <= k) ans++; 36 | } 37 | cout<= lcm[m]: 12 | res = m 13 | l = m + 1 14 | else: 15 | r = m - 1 16 | 17 | if lcm[res] == t: 18 | if res == n: 19 | return -1 20 | else: 21 | return res + 1 22 | else: 23 | return find(math.gcd(t, lcm[res]), 0, res) 24 | 25 | n, q = map(int, input().split()) 26 | l = list(map(int, input().split())) 27 | 28 | lcm = [1 for i in range(n + 1)] 29 | for i in range(n): 30 | g = math.gcd(lcm[i], l[i]) 31 | lcm[i + 1] = (lcm[i] // g) * l[i] 32 | if lcm[i + 1] > 10**9: 33 | lcm[i + 1] = 10**9 + 1 34 | 35 | for i in range(q): 36 | t = int(input()) 37 | 38 | if t == 0: 39 | print(-1) 40 | continue 41 | print(find(t, 0, n)) -------------------------------------------------------------------------------- /CCO/17/P5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 2e5 + 5; 4 | #define fir first 5 | #define sec second 6 | using pii = pair; 7 | pii a[N]; 8 | int n, buy, ans; 9 | 10 | priority_queue, greater > heap; 11 | 12 | int main() { 13 | cin.tie(0)->ios::sync_with_stdio(0); 14 | #ifdef LOCAL 15 | freopen("in.txt", "r", stdin); 16 | freopen("out.txt", "w", stdout); 17 | #endif 18 | cin>>n; 19 | for(int i = 1;i<=n;++i) { 20 | int u, v; 21 | cin>>u>>v; 22 | a[i] = {u, v}; 23 | } 24 | sort(a + 1, a + n + 1, [&](pii& a, pii& b) { 25 | return a.fir == b.fir ? a.sec > b.sec : a.fir < b.fir; 26 | }); 27 | for(int i = n;i;--i) { 28 | auto[u, v] = a[i]; 29 | u -= buy + i - 1; 30 | heap.push(v); 31 | if(u <= 0) continue; 32 | while(u--) { 33 | ans += heap.top(); heap.pop(); 34 | ++buy; 35 | } 36 | } 37 | cout< 2 | using namespace std; 3 | const int N = 15; 4 | using ll = long long; 5 | ll n, a[N], b[N], u[N], v[N], p = 1, ans; 6 | 7 | ll exgcd(ll a, ll b, ll& x, ll& y) { 8 | if(a * b == 0) { 9 | x = 1, y = 0; 10 | return a + b; 11 | } 12 | ll gcd = exgcd(b, a % b, x, y); 13 | ll tmpy = x - a / b * y; 14 | x = y; 15 | y = tmpy; 16 | return gcd; 17 | } 18 | 19 | int main() { 20 | cin.tie(0)->ios::sync_with_stdio(0); 21 | #ifdef LOCAL 22 | freopen("in.txt", "r", stdin); 23 | freopen("out.txt", "w", stdout); 24 | #endif 25 | cin>>n; 26 | for(int i = 1;i<=n;++i) { 27 | cin>>a[i]>>b[i]; 28 | p *= a[i]; 29 | } 30 | for(int i = 1;i<=n;++i) { 31 | u[i] = p / a[i]; 32 | ll y; 33 | exgcd(u[i], a[i], v[i], y); 34 | v[i] = (v[i] + a[i]) % a[i]; 35 | ans = (ans + b[i] * u[i] % p * v[i] % p) % p; 36 | } 37 | cout< 2 | #include 3 | using namespace std; 4 | const int N = 1e6; 5 | int n,m,a,b,c,Fa,Fb; 6 | long long ans; 7 | int f[N]; 8 | struct point{ 9 | int a,b,c; 10 | }e[N]; 11 | bool cmp(point a, point b){ 12 | return a.c 2 | using namespace std; 3 | 4 | #define pb push_back 5 | template using vec = vector; 6 | using vi = vec; 7 | 8 | const int N = 1e6 + 10; 9 | 10 | template struct Stack { 11 | T a[N]; 12 | int tot = 0; 13 | void push(T x) {a[++tot] = x;} 14 | T pop() {return a[tot--];} 15 | T top() {return a[tot];} 16 | T get(int pos) {return tot >= pos ? a[tot - pos + 1] : -1;}; 17 | bool empty() {return tot == 0;} 18 | }; 19 | 20 | Stack stk, ans; 21 | 22 | int main(){ 23 | ios::sync_with_stdio(0); 24 | cin.tie(0); 25 | int n; cin>>n; 26 | vi h(n + 1), j(n + 1); 27 | for(int i = 1;i<=n;i++) cin>>h[i]; 28 | for(int i = 1;i<=n;i++) cin>>j[i]; 29 | for(int i = n;i>0;i--) { 30 | while(!stk.empty() && stk.top() <= h[i]) stk.pop(); 31 | ans.push(stk.get(j[i])); 32 | stk.push(h[i]); 33 | } 34 | while(!ans.empty()) cout< 2 | using namespace std; 3 | #define ll long long 4 | #define pb push_back 5 | #define pii pair 6 | 7 | string f(string x, string y){ 8 | string res=""; 9 | for(int i=0; i>r>>l; 21 | set a,b; 22 | char c[r][l]; 23 | for(int i=0; i>c[i][j]; 26 | } 27 | } 28 | string s[r]; 29 | for(int i=0; i 2 | using namespace std; 3 | 4 | #define mp make_pair 5 | #define fir first 6 | #define sec second 7 | const int N = 5e5 + 10; 8 | int e[660][660], o[660][660]; 9 | using pii = pair; 10 | pii a[N]; 11 | 12 | int main(){ 13 | ios::sync_with_stdio(0); 14 | cin.tie(0); 15 | int n; cin>>n; 16 | 17 | for(int i = 1;i<=n;i++) { 18 | int x, y; 19 | cin>>x>>y; 20 | x++, y++; 21 | a[i] = mp(x, y); 22 | e[x][y] ++; 23 | o[x][y] ++; 24 | } 25 | for(int i = 1;i<=651;i++) { 26 | for(int j = 1;j<=651;j++) { 27 | e[i][j] += e[i][j - 1] + e[i - 1][j] - e[i - 1][j - 1]; 28 | } 29 | } 30 | for(int i = 1;i<=n;i++) { 31 | int x = a[i].fir; 32 | int y = a[i].sec; 33 | int tot = (x == 651 ? o[1][y] : 0) + (y == 651 ? o[x][1] : 0); 34 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | const int N = 1e5 + 10; 6 | int t, n, a,b[N]; 7 | stack stk; 8 | 9 | 10 | int main() { 11 | cin>>t; 12 | while(t--){ 13 | cin>>n; 14 | vector ans; 15 | int need = 1; 16 | for(int i = n;i>=1;i--){ 17 | cin>>b[i]; 18 | } 19 | for(int i = 1;i<=n;i++){ 20 | a = b[i]; 21 | bool take = false; 22 | if(a == need){ 23 | ans.push_back(a); 24 | need ++; 25 | take = true; 26 | } 27 | while(!stk.empty()){ 28 | if(stk.top() == need) { 29 | ans.push_back(stk.top()); stk.pop(); 30 | need ++; 31 | } else {break;} 32 | } 33 | if(!take) stk.push(a); 34 | } 35 | 36 | if(ans.size() >= n) cout<<"Y\n"; 37 | else cout<<"N\n"; 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /NOIP/过河卒.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define int long long 3 | using namespace std; 4 | const int N = 1e3; 5 | int dir[8][2] = {{1,2},{2,1},{-1,2},{2,-1},{-2,1},{1,-2},{-1,-2},{-2,-1}}; 6 | int x,y,a,b; 7 | int Map[N][N]; 8 | bool mark[N][N]; 9 | 10 | int isOk(int i, int j){ 11 | return i>=0 && j>=0; 12 | } 13 | 14 | signed main() { 15 | cin>>x>>y>>a>>b; 16 | mark[a][b] = true; 17 | for(int i = 0;i<8;i++){ 18 | int c = a + dir[i][0]; 19 | int d = b + dir[i][1]; 20 | if(isOk(c,d)) mark[c][d] = true; 21 | } 22 | for(int i = 0;i 2 | using namespace std; 3 | #define pb push_back 4 | const int N = 1e3 + 10; 5 | int n, m, c, a[N], dp[N][N]; 6 | bool vis[N][N]; 7 | vector e[N]; 8 | 9 | int dfs(int t, int u) { 10 | if(vis[t][u]) return dp[t][u]; 11 | vis[t][u] = 1; 12 | if(t == 0 && u != 1) return -1e9; 13 | for(int v : e[u]) { 14 | dp[t][u] = max(dp[t][u], dfs(t - 1, v) + a[u]); 15 | } 16 | return dp[t][u]; 17 | } 18 | 19 | int main() { 20 | ios::sync_with_stdio(0); 21 | cin.tie(0); 22 | freopen("time.in", "r", stdin); 23 | freopen("time.out", "w", stdout); 24 | cin>>n>>m>>c; 25 | for(int i = 1;i<=n;++i) cin>>a[i]; 26 | for(int i = 1;i<=m;++i) { 27 | int u, v; 28 | cin>>u>>v; 29 | e[v].pb(u); 30 | } 31 | memset(dp, -0x3f, sizeof(dp)); 32 | vis[0][1] = 1; 33 | dp[0][1] = 0; 34 | int ans = 0; 35 | for(int i = 0;i<=1e3 + 1;++i) ans = max(ans, dfs(i, 1) - i * i * c); 36 | cout< 2 | using namespace std; 3 | const int N = 1e6 + 5; 4 | const int inf = 0x3f3f3f3f; 5 | int n, m, k, a[N], b[N]; 6 | 7 | bool ok(int x) { 8 | for(int i = 1, j = 1;j<=m;++j) { 9 | if(a[i] < b[j]) break; 10 | int cnt = 0; 11 | while(i <= n && cnt < x && a[i] <= b[j] + k) ++cnt, ++i; 12 | if(i == n + 1) return 1; 13 | } 14 | return 0; 15 | } 16 | 17 | int main() { 18 | cin.tie(0)->ios::sync_with_stdio(0); 19 | #ifdef LOCAL 20 | freopen("in.txt", "r", stdin); 21 | freopen("out.txt", "w", stdout); 22 | #endif 23 | cin>>n>>m>>k; 24 | for(int i = 1;i<=n;++i) cin>>a[i]; 25 | for(int i = 1;i<=m;++i) cin>>b[i]; 26 | sort(a + 1, a + n + 1); 27 | sort(b + 1, b + m + 1); 28 | int lo = 1, hi = inf, ans = -1; 29 | ok(1); 30 | while(lo <= hi) { 31 | int mid = lo + hi >> 1; 32 | if(ok(mid)) ans = mid, hi = mid - 1; 33 | else lo = mid + 1; 34 | } 35 | cout< 2 | #include 3 | using namespace std; 4 | const int N = 1e3+10; 5 | const int VIS = 1e7; 6 | int m,n; 7 | int Map[N][N]; 8 | int vis[VIS]; 9 | queue q; 10 | 11 | bool isOk(int a, int b){ 12 | return 00 && b<=n; 13 | } 14 | 15 | int main() { 16 | cin>>m>>n; 17 | for(int i = 1;i<=m;i++){ 18 | for(int j = 1;j<=n;j++){ 19 | cin>>Map[i][j]; 20 | } 21 | } 22 | q.push(Map[1][1]); 23 | int cur; 24 | while(!q.empty()){ 25 | cur = q.front(); q.pop(); 26 | if(vis[cur]) continue; 27 | vis[cur] = true; 28 | for(int i = 1;i*i<=cur;i++){ 29 | if(cur%i == 0){ 30 | if(isOk(i,cur/i)) q.push(Map[i][cur/i]); 31 | if(isOk(cur/i,i)) q.push(Map[cur/i][i]); 32 | } if((i == m && cur/i == n) || (cur/i == m && i == n)){ 33 | cout<<"yes"; 34 | return 0; 35 | } 36 | } 37 | } 38 | cout<<"no"; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /CCC/Mock_22_2/S1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int N = 1e4 + 10; 5 | 6 | int n, k, a[N][20], key[20], ans = 0; 7 | 8 | void dfs(int u) { 9 | if(u == k + 1) { 10 | int cur = 0x3f3f3f3f; 11 | for(int i = 1;i<=n;++i) { 12 | int tot = 0; 13 | for(int j = 1;j<=k;++j) { 14 | tot += a[i][j] == key[j]; 15 | } 16 | cur = min(cur, tot); 17 | } 18 | ans = max(cur, ans); 19 | return; 20 | } 21 | key[u] = 1; 22 | dfs(u + 1); 23 | key[u] = 0; 24 | dfs(u + 1); 25 | } 26 | 27 | int main() { 28 | ios::sync_with_stdio(0); 29 | cin.tie(0); 30 | #ifdef LOCAL 31 | freopen("in.txt", "r", stdin); 32 | freopen("out.txt", "w", stdout); 33 | #endif 34 | cin>>n>>k; 35 | for(int i = 1;i<=n;++i) { 36 | string s; cin>>s; 37 | for(int j = 1;j<=k;++j) { 38 | a[i][j] = s[j - 1] == 'T'; 39 | } 40 | } 41 | dfs(1); 42 | cout< 2 | using namespace std; 3 | #include 4 | #include 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag,tree_order_statistics_node_update>; 9 | 10 | const int N = 1e5 + 10; 11 | int n, a[N], pos; 12 | ordered_set bst; 13 | 14 | int main() { 15 | ios::sync_with_stdio(0); 16 | cin.tie(0); 17 | freopen("sleepy.in", "r", stdin); 18 | freopen("sleepy.out", "w", stdout); 19 | cin>>n; 20 | for(int i = 1;i<=n;++i) cin>>a[i]; 21 | bst.insert(a[n]); 22 | for(int i = n - 1;i;--i) { 23 | if(a[i] > a[i + 1]) { 24 | pos = i; 25 | break; 26 | } 27 | bst.insert(a[i]); 28 | } 29 | cout< 2 | using namespace std; 3 | 4 | const int N = 1e4 + 5; 5 | const int D = 105; 6 | const int mod = 1e9 + 7; 7 | string s; 8 | int n, d, dp[N][D]; 9 | 10 | int main() { 11 | cin.tie(0)->ios::sync_with_stdio(0); 12 | #ifdef LOCAL 13 | freopen("in.txt", "r", stdin); 14 | freopen("out.txt", "w", stdout); 15 | #endif 16 | cin>>s; 17 | n = s.size(); 18 | s = '@' + s; 19 | cin>>d; 20 | dp[0][0] = 1; 21 | for(int i = 1;i<=n;++i) { 22 | for(int j = 0;j<=d;++j) { 23 | for(int k = 0;k<=9;++k) { 24 | dp[i][j] += dp[i - 1][(j - k + d * 10) % d]; 25 | dp[i][j] %= mod; 26 | } 27 | } 28 | } 29 | int ans = 0, cnt = 0; 30 | for(int i = 1;i<=n;++i) { 31 | for(int j = 0;j 2 | using namespace std; 3 | 4 | const int N = 1e3 + 10; 5 | int k, m, n, e[N][N], match[N], vis[N]; 6 | 7 | bool Find(int x) { // x is the weapon, f[x] is the person 8 | for(int i = 1; i <= n; i++) { // enumerate right point 9 | if(e[x][i] == 1 && vis[i] == 0) { 10 | vis[i] = 1; 11 | if(match[i] == 0 || Find(match[i])) { 12 | match[i] = x; 13 | return true; 14 | } 15 | } 16 | } 17 | return false; 18 | } 19 | 20 | int main(){ 21 | ios::sync_with_stdio(0); 22 | cin.tie(0); 23 | cin>>k>>m>>n; 24 | for(int i = 1;i<=k;i++) { 25 | int a, b; 26 | cin>>a>>b; 27 | e[a][b] = 1; 28 | } 29 | int ans = 0; 30 | for(int i = 1; i <= m;i++) { // enumerate left point 31 | memset(vis, 0, sizeof(vis)); 32 | if(Find(i)) { 33 | ans++; 34 | } 35 | } 36 | cout << ans << "\n"; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /other solutions/游戏.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define pb push_back 4 | using ll = long long; 5 | int n; 6 | const int N = 1e4 + 10; 7 | bool np[100001] = {0}; 8 | vector p; 9 | ll dp[N][2001]; // add together is n, using only first p prime numbers 10 | 11 | void seive() { 12 | np[1] = 1; 13 | for(int i = 2;i<=n;++i) { 14 | if(np[i]) continue; 15 | p.pb(i); 16 | for(int j = i + i;j<=n;j += i) np[j] = 1; 17 | } 18 | } 19 | 20 | int main() { 21 | #ifndef ONLINE_JUDGE 22 | freopen(".in", "r", stdin); 23 | freopen(".out", "w", stdout); 24 | #endif 25 | ios::sync_with_stdio(0); 26 | cin.tie(0); 27 | cin>>n; 28 | seive(); 29 | for(int i = 0;i<=n;++i) dp[i][0] = 1; 30 | for(int j = 0;j 2 | using namespace std; 3 | 4 | int dp[100][30], n, ans[30]; 5 | 6 | int get(char c) { 7 | return c - 'A' > 25 ? 26 : c - 'A'; 8 | } 9 | 10 | char put(int x) { 11 | return x == 26 ? '_' : x + 'A'; 12 | } 13 | 14 | int main() { 15 | ios::sync_with_stdio(0); 16 | cin.tie(0); 17 | #ifdef LOCAL 18 | freopen("in.txt", "r", stdin); 19 | freopen("out.txt", "w", stdout); 20 | #endif 21 | for(int i = 0;i<=26;++i) { 22 | char c; cin>>c; 23 | dp[0][i] = get(c); 24 | } 25 | for(int i = 1;i<=50;++i) { 26 | for(int j = 0;j<=26;++j) { 27 | dp[i][j] = dp[i - 1][dp[i - 1][j]]; 28 | } 29 | } 30 | cin>>n; 31 | for(int i = 0;i<30;++i) ans[i] = i; 32 | for(int i = 40;i >= 0 && n > 0;i--) { 33 | if(n >= (1ll << i)) { 34 | n -= 1ll << i; 35 | for(int j = 0;j<=26;++j) ans[j] = dp[i][ans[j]]; 36 | } 37 | } 38 | string s; cin>>s; 39 | for(auto& c : s) c = put(ans[get(c)]); 40 | cout< 2 | using namespace std; 3 | const int N = 31632; 4 | const int M = 5e6 + 5; 5 | #define pb push_back 6 | bitset p; 7 | bitset fail; 8 | set pm; 9 | using ll = long long; 10 | int n, m; 11 | 12 | int main() { 13 | cin.tie(0)->ios::sync_with_stdio(0); 14 | #ifdef LOCAL 15 | freopen("in.txt", "r", stdin); 16 | freopen("out.txt", "w", stdout); 17 | #endif 18 | for(int i = 2;i>n>>m; 24 | if(n == 1) ++n; 25 | for(int i = n;i<=m;++i) { 26 | set vis; 27 | for(auto c : pm) { 28 | if((ll)c * c > i) break; 29 | if(i % c == 0 && i / c != 1) { 30 | for(int j = i; j <= m; j += c) fail[j - n] = 1; 31 | vis.insert(c); 32 | } 33 | } 34 | for(auto c : vis) pm.erase(c); 35 | } 36 | for(int i = 0;i<=m - n;++i) if(!fail[i]) cout< 2 | using namespace std; 3 | 4 | int n; 5 | deque ans; 6 | vector a, b; 7 | 8 | void get() { 9 | int c = 0; 10 | while((1 << c) < n) ++c; 11 | --c; 12 | if(c == -1) return; 13 | cout<<"$ "<>x; 18 | ans.push_front(++x); 19 | for(int i = 0;i>tmp; 22 | if(tmp) b.push_back(a[i]); 23 | } 24 | a.clear(); 25 | for(auto x : b) a.push_back(x); 26 | b.clear(); 27 | n = a.size(); 28 | get(); 29 | } 30 | 31 | int main() { 32 | cin.tie(0)->ios::sync_with_stdio(0); 33 | cin>>n; 34 | for(int i = 1;i<=n;++i) a.push_back(i); 35 | get(); 36 | cout<<"! "< 2 | using namespace std; 3 | 4 | const int N = 1e5 + 10; 5 | int n, a[N], pos; 6 | 7 | struct fenwick { 8 | int c[N]; 9 | void update(int a, int b) { 10 | for(int i = a;i>n; 25 | for(int i = 1;i<=n;++i) cin>>a[i]; 26 | fen.update(a[n], 1); 27 | for(int i = n - 1;i;--i) { 28 | if(a[i] > a[i + 1]) { 29 | pos = i; 30 | break; 31 | } 32 | fen.update(a[i], 1); 33 | } 34 | cout< 2 | using namespace std; 3 | #define pb push_back 4 | using ll = long long; 5 | int n, m; 6 | const int N = 1e4 + 10; 7 | bool np[100001] = {0}; 8 | vector p; 9 | ll dp[N][2001]; // add together is n, using only first p prime numbers 10 | 11 | void seive() { 12 | np[1] = 1; 13 | for(int i = 2;i<=n;++i) { 14 | if(np[i]) continue; 15 | p.pb(i % m); 16 | for(int j = i + i;j<=n;j += i) np[j] = 1; 17 | } 18 | } 19 | 20 | int main() { 21 | freopen("exercise.in", "r", stdin); 22 | freopen("exercise.out", "w", stdout); 23 | ios::sync_with_stdio(0); 24 | cin.tie(0); 25 | cin>>n>>m; 26 | seive(); 27 | for(int i = 0;i<=n;++i) dp[i][0] = 1; 28 | for(int j = 0;j 2 | using namespace std; 3 | const int N = 1e5 + 10; 4 | using ll = long long; 5 | 6 | struct fenwick { 7 | int st[N]; 8 | int lowbit(int x) {return x & (-x);} 9 | void update(int a, int b) { 10 | for(int i = a;i>n; 30 | for(int i = 1;i<=n;i++) { 31 | int x; cin>>x; 32 | ++x; 33 | a[x] += st.query(x + 1, N - 1); 34 | st.update(x, 1); 35 | } 36 | ll ans = 0; 37 | for(int i = 1;i<=n;i++) { 38 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | const int N = 5e5 + 10; 5 | typedef long long ll; 6 | int n, len = 0; 7 | ll w; 8 | ll dp[N], sum1[N], sum2[N]; 9 | 10 | inline int binarySearch(){ 11 | int l = 0, r = len - 1, ans = len; 12 | while(l <= r){ 13 | int mid = (l + r) >> 1; 14 | if(sum1[len] - sum1[mid] <= w){ 15 | ans = mid; 16 | r = mid - 1; 17 | } else l = mid + 1; 18 | } 19 | return ans; 20 | } 21 | 22 | int main(){ 23 | ios::sync_with_stdio(0); 24 | cin.tie(0); 25 | cin>>n>>w; 26 | while(n--){ 27 | char opt; 28 | cin>>opt; 29 | ll p, c; 30 | if(opt == 'D'){ 31 | len--; 32 | continue; 33 | } 34 | cin>>p>>c; 35 | len++; 36 | sum1[len] = sum1[len - 1] + p; 37 | sum2[len] = sum2[len - 1] + c; 38 | int k = binarySearch(); 39 | dp[len] = max(dp[len - 1], sum2[len] - sum2[k]); 40 | cout< 2 | using namespace std; 3 | 4 | namespace fast_io { 5 | int read() { 6 | int x = 0, f = 0; char ch = getchar(); 7 | while (!isdigit(ch)) f |= ch == '-', ch = getchar(); 8 | while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar(); 9 | return f ? -x : x; 10 | } 11 | void read(int& x) {x = read();} 12 | template void print(T x) { 13 | if (x < 0) putchar('-'), x = -x; 14 | if (x >= 10) print(x / 10); 15 | putchar(x % 10 + '0'); 16 | } 17 | template void print(T x, char let) { 18 | print(x), putchar(let); 19 | } 20 | } 21 | using namespace fast_io; 22 | const int N = 1e5 + 10; 23 | int h[N], dp[N], n, k; 24 | 25 | int main() { 26 | n = read(), k = read(); 27 | for(int i = 1;i<=n;++i) { 28 | read(h[i]); 29 | if(i > 1) dp[i] = 0x3f3f3f3f; 30 | for(int j = max(1, i - k); j 2 | using namespace std; 3 | const int N = 1e4 + 5; 4 | int k, m, s[N], a[N], n, sg[N]; 5 | bitset vis; 6 | 7 | void solve() { 8 | memset(sg, 0, sizeof sg); 9 | for(int i = 1;i<=k;++i) scanf("%d", s + i); 10 | for(int i = 1;i= s[j]) vis[sg[i - s[j]]] = 1; 12 | while(vis[sg[i]]) ++sg[i]; 13 | for(int j = 1;j<=k;++j) if(i >= s[j]) vis[sg[i - s[j]]] = 0; 14 | } 15 | scanf("%d", &m); 16 | for(int i = 1;i<=m;++i) { 17 | int tot = 0; 18 | scanf("%d", &n); 19 | for(int j = 1;j<=n;++j) { 20 | int x; scanf("%d", &x); 21 | tot ^= sg[x]; 22 | } 23 | putchar(tot == 0 ? 'L' : 'W'); 24 | } 25 | putchar('\n'); 26 | } 27 | 28 | int main() { 29 | #ifdef LOCAL 30 | freopen("in.txt", "r", stdin); 31 | freopen("out.txt", "w", stdout); 32 | #endif 33 | while(1) { 34 | scanf("%d", &k); 35 | if(k == 0) return 0; 36 | solve(); 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /USACO/2022/Dec/Silver/P3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 305; 4 | const int inf = 0x3f3f3f3f; 5 | int n, r[N][N], ans[N]; 6 | 7 | int main() { 8 | cin.tie(0)->ios::sync_with_stdio(0); 9 | #ifdef LOCAL 10 | freopen("in.txt", "r", stdin); 11 | freopen("out.txt", "w", stdout); 12 | #endif 13 | cin>>n; 14 | for(int i = 1;i<=n;++i) { 15 | for(int j = i;j<=n;++j) { 16 | cin>>r[i][j]; 17 | } 18 | } 19 | auto ok = [&](int n) { 20 | if(ans[n] > inf || ans[n] < -inf) return false; 21 | int mx = ans[n], mn = ans[n]; 22 | bool flag = 1; 23 | for(int i = n - 1;i;--i) { 24 | mx = max(mx, ans[i]); 25 | mn = min(mn, ans[i]); 26 | flag &= mx - mn == r[i][n]; 27 | } 28 | return flag; 29 | }; 30 | for(int i = 2;i<=n;++i) { 31 | ans[i] = ans[i - 1] - r[i - 1][i]; 32 | if(ok(i)) continue; 33 | ans[i] = ans[i - 1] + r[i - 1][i]; 34 | } 35 | for(int i = 1;i<=n;++i) cout< 2 | #include 3 | #define endl "\n" 4 | using namespace std; 5 | using ll = long long; 6 | ll n; 7 | 8 | inline bool isPrime(ll x){ 9 | for(ll i = 2;i * i <= x;i++){ 10 | if(x % i == 0) return false; 11 | } 12 | return true; 13 | } 14 | 15 | inline ll solve(){ 16 | cin>>n; 17 | ll a = -1, b = -1, c = -1; 18 | ll ans; 19 | ll m = ll(sqrt(n)); 20 | ll i = m + 1; 21 | while(i--){ 22 | if(isPrime(i)){ 23 | if(a == -1) a = i; 24 | else b = i; 25 | } 26 | if(a != -1 && b != -1) break; 27 | } 28 | i = m; 29 | while(i ++){ 30 | if(isPrime(i)){ 31 | c = i; 32 | break; 33 | } 34 | } 35 | ans = max(a * b, a * c <= n ? a * c : 0); 36 | 37 | return ans; 38 | } 39 | 40 | int main() { 41 | ios::sync_with_stdio(false); 42 | cin.tie(nullptr); 43 | int t; cin>>t; 44 | for(int i = 1;i<=t;i++){ 45 | cout<<"Case #" << i << ": "<< solve() << endl; 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Leetcode/medium/make-number-of-distinct-characters-equal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isItPossible(string word1, string word2) { 4 | map mp, mp2; 5 | for(auto x : word1) ++mp[x]; 6 | for(auto x : word2) ++mp2[x]; 7 | int x = mp.size(), y = mp2.size(); 8 | for(auto[u, v] : mp) { 9 | for(auto[a, b] : mp2) { 10 | if(v > 0 && b > 0) { 11 | int xx = x; 12 | int yy = y; 13 | if(v == 1) --xx; 14 | mp[u]--; 15 | if(mp[a] == 0) ++xx; 16 | mp[a]++; 17 | if(b == 1) --yy; 18 | mp2[a]--; 19 | if(mp2[u] == 0) ++yy; 20 | mp2[u]++; 21 | cout< 2 | using namespace std; 3 | #define pb push_back 4 | using ll = long long; 5 | int n; 6 | const int N = 1e4 + 10; 7 | bool np[100001] = {0}; 8 | vector p; 9 | ll dp[N][2001]; // add together is n, using only first p prime numbers 10 | 11 | void seive() { 12 | np[1] = 1; 13 | for(int i = 2;i<=n;++i) { 14 | if(np[i]) continue; 15 | p.pb(i); 16 | for(int j = i + i;j<=n;j += i) np[j] = 1; 17 | } 18 | } 19 | 20 | int main() { 21 | #ifndef ONLINE_JUDGE 22 | freopen(".in", "r", stdin); 23 | freopen(".out", "w", stdout); 24 | #endif 25 | ios::sync_with_stdio(0); 26 | cin.tie(0); 27 | cin>>n; 28 | seive(); 29 | for(int i = 0;i<=n;++i) dp[i][0] = 1; 30 | for(int j = 0;j 3 | using namespace std; 4 | 5 | const int N = 1e5 + 5; 6 | const int K = 25; 7 | char c[N]; 8 | int n, k, mx[K][3]; 9 | 10 | int main() { 11 | cin.tie(0)->ios::sync_with_stdio(0); 12 | // freopen("hps.in", "r", stdin); 13 | // freopen("hps.out", "w", stdout); 14 | cin>>n>>k; 15 | ++k; 16 | auto convert = [&](char c) { 17 | switch(c) { 18 | case 'H': return 1; 19 | case 'P': return 2; 20 | case 'S': return 0; 21 | } 22 | return 0; 23 | }; 24 | int ans = 0; 25 | for(int i = 1;i<=n;++i) { 26 | cin>>c[i]; 27 | int x = convert(c[i]); 28 | for(int j = k;j;--j) { 29 | ++mx[j - 1][x]; 30 | int tmp = max({mx[j - 1][0], mx[j - 1][1], mx[j - 1][2]}); 31 | ans = max(ans, tmp); 32 | mx[j][0] = max(mx[j][0], tmp); 33 | mx[j][1] = max(mx[j][1], tmp); 34 | mx[j][2] = max(mx[j][2], tmp); 35 | } 36 | } 37 | cout< 2 | using namespace std; 3 | 4 | #define fir first 5 | #define sec second 6 | typedef pair pii; 7 | const int N = 300; // 300 cause below i put 2 * n 8 | pii a[N]; 9 | int n; 10 | // dp[i][j] from i to j the maximum solution 11 | int dp[N][N]; 12 | 13 | int main() { 14 | ios::sync_with_stdio(0); 15 | cin.tie(0); 16 | cin>>n; 17 | cin>>a[1].fir; 18 | a[n].sec = a[1].fir; 19 | for(int i = 2;i<=n;i++) { 20 | cin>>a[i].fir; 21 | a[i - 1].sec = a[i].fir; 22 | } 23 | for(int i = n + 1;i<=n * 2;i++) { 24 | a[i] = a[i - n]; 25 | } 26 | int ans = 0; 27 | for(int l = 2;l<=n;l++) { 28 | for(int i = 1;i + l - 1 <= (n << 1);i++) { 29 | int j = i + l - 1; 30 | for(int k = i;k 2 | using namespace std; 3 | const int N = 35; 4 | const int way[5][4] = { 5 | {2, 1, 0, 2}, 6 | {1, 1, 1, 1}, 7 | {0, 0, 2, 1}, 8 | {0, 3, 0, 0}, 9 | {1, 0, 0, 1} 10 | }; 11 | int n; 12 | bool dp[N][N][N][N], vis[N][N][N][N]; 13 | 14 | bool dfs(int a, int b, int c, int d) { 15 | if(vis[a][b][c][d]) return dp[a][b][c][d]; 16 | vis[a][b][c][d] = 1; 17 | dp[a][b][c][d] = 1; 18 | for(auto x : way) { 19 | int na = a - x[0]; 20 | int nb = b - x[1]; 21 | int nc = c - x[2]; 22 | int nd = d - x[3]; 23 | if(min({na, nb, nc, nd}) >= 0) { 24 | dp[a][b][c][d] &= dfs(na, nb, nc, nd) ^ 1; 25 | } 26 | } 27 | return dp[a][b][c][d]; 28 | } 29 | 30 | int main() { 31 | #ifdef LOCAL 32 | freopen("in.txt", "r", stdin); 33 | freopen("out.txt", "w", stdout); 34 | #endif 35 | scanf("%d", &n); 36 | for(int i = 1;i<=n;++i) { 37 | int a, b, c, d; 38 | scanf("%d%d%d%d", &a, &b, &c, &d); 39 | puts(dfs(a, b, c, d) ? "Roland" : "Patrick"); 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /USACO/2022/Open/Silver/P1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 1e5 + 5; 4 | const int inf = 0x3f3f3f3f; 5 | int v[N], a[N], n; 6 | long long ans; 7 | bitset vis, vis2; 8 | 9 | void dfs(int u) { 10 | if(vis[u]) return; 11 | if(vis2[u]) { 12 | int x = u; 13 | int mn = v[x]; 14 | x = a[x]; 15 | while(x != u) { 16 | mn = min(mn, v[x]); 17 | x = a[x]; 18 | } 19 | ans -= mn; 20 | return; 21 | } 22 | vis2[u] = 1; 23 | dfs(a[u]); 24 | vis2[u] = 0; 25 | } 26 | 27 | void dfs2(int u) { 28 | if(vis[u]) return; 29 | vis[u] = 1; 30 | dfs2(a[u]); 31 | } 32 | 33 | int main() { 34 | cin.tie(0)->ios::sync_with_stdio(0); 35 | #ifdef LOCAL 36 | freopen("in.txt", "r", stdin); 37 | freopen("out.txt", "w", stdout); 38 | #endif 39 | cin>>n; 40 | for(int i = 1;i<=n;++i) { 41 | cin>>a[i]>>v[i]; 42 | ans += v[i]; 43 | } 44 | for(int i = 1;i<=n;++i) if(!vis[i]) { 45 | dfs(i); 46 | dfs2(i); 47 | } 48 | cout< 2 | using namespace std; 3 | using ll = long long; 4 | const int D = 5e3 + 5; 5 | int a[D], k, q, d, m, tot; 6 | bool flag; 7 | int ans[1000005]; 8 | bool tmp; 9 | set vis; 10 | 11 | void solve(ll x) { 12 | if(flag) return; 13 | if(x == 0 && tot) { 14 | for(int i = tot;i;--i) cout<ios::sync_with_stdio(0); 29 | #ifdef LOCAL 30 | freopen("in.txt", "r", stdin); 31 | freopen("out.txt", "w", stdout); 32 | #endif 33 | cin>>k>>q>>d>>m; 34 | bool flagg = 0; 35 | for(int i = 1;i<=d;++i) cin>>a[i], flagg |= a[i] == 0; 36 | for(int i = 1;i<=q;++i) { 37 | vis.clear(); 38 | ll x; cin>>x; 39 | solve(x); 40 | if(!flag) cout<<"IMPOSSIBLE\n"; 41 | flag = 0; 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /codeforces/div 2/740/D2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Andy Zhu 3 | * @date 2021-08-24 07:32:32 4 | * @version 1.0.0 5 | */ 6 | 7 | //include 8 | #include 9 | using namespace std; 10 | 11 | #define OJ ONLINE_JUDGE 12 | using ll = long long; 13 | int MOD = 1e9 + 7; 14 | template inline T lowbit(T x){return x & (- x);} 15 | 16 | const int N = 4e6 + 10; 17 | int n, sum[N]; 18 | int main(){ 19 | ios::sync_with_stdio(0); 20 | cin.tie(0); 21 | cin>>n>>MOD; 22 | sum[n] = 1; 23 | 24 | for(int i = n - 1;i>0;i--) { 25 | int cur = 0; 26 | cur += sum[i + 1]; 27 | for(int j = 2; j * i <= n;j++) { 28 | cur += (sum[i * j] - sum[min(i * j + j, n + 1)] + MOD) % MOD; 29 | cur %= MOD; 30 | } 31 | sum[i] = (sum[i + 1] + cur) % MOD; 32 | } 33 | cout<<(sum[1] - sum[2] + MOD) % MOD<<'\n'; 34 | } 35 | 36 | 37 | /* stuff you should look for 38 | * int overflow, array bounds 39 | * special cases (n=1?) 40 | * do smth instead of nothing and stay organized 41 | * WRITE STUFF DOWN 42 | * DON'T GET STUCK ON ONE APPROACH 43 | */ 44 | -------------------------------------------------------------------------------- /USACO/2021/Dec/bronze/P1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string s; 5 | const int N = 5e5 + 10; 6 | using ll = long long; 7 | int n, a[N]; 8 | ll siz[N]; 9 | 10 | int main() { 11 | ios::sync_with_stdio(0); 12 | cin.tie(0); 13 | #ifdef LOCAL 14 | freopen("in.txt", "r", stdin); 15 | freopen("out.txt", "w", stdout); 16 | #endif 17 | cin>>n>>s; 18 | for(int i = 0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define endl "\n" 7 | using namespace std; 8 | typedef long long ll; 9 | const int N = 2e5 + 10; 10 | int n, b[N]; 11 | map pos; // previous position of b[i] 12 | unordered_set cnt; // whether one value appeared before 13 | vector pos2; 14 | int main() { 15 | cin>>n; 16 | for(int i = 1;i<=n;i++){ 17 | cin>>b[i]; 18 | } 19 | ll ans = 0; 20 | for(int i = 1;i<=n;i++){ 21 | ll prev = ans; 22 | if(pos[b[i]] == 0) ans += cnt.size() - cnt.count(b[i]); 23 | else { 24 | vector :: iterator it; 25 | it = lower_bound(pos2.begin(), pos2.end(), pos[b[i]]); 26 | 27 | ans += pos2.size() - (it - pos2.begin() + 1); 28 | if(!pos2.empty()) 29 | pos2.erase(it); 30 | } 31 | //cout< 2 | using namespace std; 3 | 4 | const int N = 1e6 + 10; 5 | 6 | string a[N]; 7 | 8 | struct Trie { 9 | struct node { 10 | int e[26] = {0}; 11 | } pos[N]; 12 | int tot = 1, cnt[N]; 13 | bool update(int rt, string& a, int cur) { 14 | if(cnt[rt]) return 1; 15 | if(cur == a.size()) { 16 | cnt[rt]++; 17 | return 0; 18 | } 19 | if(!pos[rt].e[a[cur] - '0']) { 20 | pos[rt].e[a[cur] - '0'] = ++tot; 21 | } 22 | return update(pos[rt].e[a[cur] - '0'], a, cur + 1); 23 | } 24 | } trie; 25 | 26 | int main() { 27 | #ifdef LOCAL 28 | freopen("in.txt", "r", stdin); 29 | freopen("out.txt", "w", stdout); 30 | #endif 31 | ios::sync_with_stdio(0); 32 | cin.tie(0); 33 | int n; cin>>n; 34 | for(int i = 1;i<=n;++i) cin>>a[i]; 35 | sort(a + 1, a + n + 1); 36 | for(int i = 1;i<=n;++i) { 37 | if(trie.update(1, a[i], 0)) { 38 | cout<<"YES"<<'\n'; 39 | return 0; 40 | } 41 | } 42 | cout<<"NO"<<'\n'; 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /other solutions/Marcia_and_maze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 1e3; 4 | int sum[N][N], n; 5 | 6 | int cnt(int i, int j, int x) { 7 | int k = i + x; 8 | int w = j + x; 9 | return sum[k][w] - sum[k][j - 1] - sum[i - 1][w] + sum[i - 1][j - 1]; 10 | } 11 | 12 | bool ok(int x) { 13 | for(int i = 1;i<=n - x;++i) { 14 | for(int j = 1;j<=n - x;++j) { 15 | if(x == 1 && cnt(i, j, x) == 0) { 16 | return 1; 17 | } 18 | if(cnt(i, j, x) == cnt(i + 1, j + 1, x - 2)) { 19 | return 1; 20 | } 21 | } 22 | } 23 | return 0; 24 | } 25 | 26 | int main() { 27 | ios::sync_with_stdio(0); 28 | cin.tie(0); 29 | cin>>n; 30 | for(int i = 1;i<=n;++i) { 31 | string s; cin>>s; 32 | for(int j = 1;j<=n;++j) { 33 | sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1] + (s[j - 1] == '#'); 34 | } 35 | } 36 | int ans = 0; 37 | for(int i = 1;i<=n - 1;++i) { 38 | if(ok(i)) ans = i; 39 | } 40 | 41 | cout< 2 | 3 | using namespace std; 4 | const int N = 1e6 + 5; 5 | const int base = 131; 6 | const int base2 = 13331; 7 | const int mod = 1e9 + 7; 8 | using ull = unsigned long long; 9 | ull p[N], h[N], key; 10 | ull p2[N], h2[N], key2; 11 | string s, t; 12 | 13 | int main() { 14 | ios::sync_with_stdio(0); 15 | cin>>s>>t; 16 | int n = s.size(), m = t.size(); 17 | s = '@' + s; 18 | t = '@' + t; 19 | p[0] = p2[0] = 1; 20 | for(int i = 1;i<=n;++i) { 21 | h[i] = h[i - 1] * 131 + s[i]; 22 | p[i] = p[i - 1] * 131; 23 | h2[i] = (h2[i - 1] * base2 % mod + s[i]) % mod; 24 | p2[i] = p2[i - 1] * base2 % mod; 25 | } 26 | for(int i = 1;i<=m;++i) { 27 | key = key * 131 + t[i]; 28 | key2 = (key2 * base2 + t[i]) % mod; 29 | } 30 | for(int i = 1;i + m - 1<=n;++i) { 31 | int j = i + m - 1; 32 | if(h[j] - h[i - 1] * p[m] == key && (h2[j] - h2[i - 1] * p2[m] % mod + mod) % mod == key2) { 33 | cout< 2 | using namespace std; 3 | const int N = 1002; 4 | const int M = 202; 5 | const int mod = 1e9 + 7; 6 | using ll = long long; 7 | // uses K substrings, first i in A match first j in B 8 | int n, m, K; 9 | // dp don't end current, f end current 10 | ll dp[2][M][M], f[2][M][M]; 11 | string s, t; 12 | 13 | int main() { 14 | cin.tie(0)->ios::sync_with_stdio(0); 15 | #ifdef LOCAL 16 | freopen("in.txt", "r", stdin); 17 | freopen("out.txt", "w", stdout); 18 | #endif 19 | cin>>n>>m>>K>>s>>t; 20 | s = '@' + s; 21 | t = '@' + t; 22 | dp[0][0][0] = 1; 23 | for(int i = 1;i<=n;++i) { 24 | dp[i & 1][0][0] = 1; 25 | for(int j = 1;j<=m;++j) { 26 | for(int k = 1;k<=K;++k) { 27 | if(s[i] == t[j]) f[i & 1][j][k] = (f[i & 1 ^ 1][j - 1][k] + dp[i & 1 ^ 1][j - 1][k - 1]) % mod; 28 | else f[i & 1][j][k] = 0; 29 | dp[i & 1][j][k] = (dp[i & 1 ^ 1][j][k] + f[i & 1][j][k]) % mod; 30 | } 31 | } 32 | memset(dp[i & 1 ^ 1], 0, sizeof(dp[i & 1 ^ 1])); 33 | } 34 | cout< 2 | using namespace std; 3 | 4 | const int N = 1e4 + 10; 5 | int n, k, a[30][30], dist[30], ans = 1; 6 | string s[N]; 7 | 8 | void dfs(int u) { 9 | for(int v = 1;v<=k;++v) { 10 | if(a[u][v]) { 11 | dist[v] = dist[u] + 1; 12 | ans = max(ans, dist[v]); 13 | dfs(v); 14 | } 15 | } 16 | } 17 | 18 | int main() { 19 | ios::sync_with_stdio(0); 20 | cin.tie(0); 21 | #ifdef LOCAL 22 | freopen("in.txt", "r", stdin); 23 | freopen("out.txt", "w", stdout); 24 | #endif 25 | cin>>n>>k; 26 | memset(a, -1, sizeof(a)); 27 | for(int i = 1;i<=k;++i) { 28 | a[i][i] = 0; 29 | } 30 | for(int i = 1;i<=n;++i) { 31 | cin>>s[i]; 32 | s[i] = '@' + s[i]; 33 | for(int j = 1;j<=k;++j) { 34 | for(int l = 1;l<=j;++l) { 35 | a[s[i][j] - 'A' + 1][s[i][l] - 'A' + 1] = 0; 36 | } 37 | } 38 | 39 | } 40 | for(int i = 1;i<=k;++i) { 41 | memset(dist, 0, sizeof(dist)); 42 | dist[i] = 1; 43 | dfs(i); 44 | } 45 | cout< 2 | using namespace std; 3 | 4 | const int N = 1e3 + 10; 5 | int n, k, b[N], c[N]; 6 | 7 | bool cmp(int a, int b) { 8 | return a > b; 9 | } 10 | 11 | int main(){ 12 | freopen("berries.in", "r", stdin); 13 | freopen("berries.out", "w", stdout); 14 | ios::sync_with_stdio(0); 15 | cin.tie(0); 16 | cin>>n>>k; 17 | for(int i = 1;i<=n;i++) { 18 | cin>>b[i]; 19 | } 20 | int ans = 0; 21 | for(int x = 1;x x) { 26 | c[i] -= x; 27 | cnt++; 28 | } 29 | } 30 | sort(c + 1, c + n + 1, cmp); 31 | for(int i = 1;i<=n;i++) c[i] += c[i - 1]; 32 | if(cnt >= k) ans = max(ans, (k >> 1) * x); 33 | else { 34 | cnt -= k / 2; 35 | if(cnt > 0) ans = max(ans, x * cnt + c[(k >> 1) - cnt]); 36 | else ans = max(ans, c[(k >> 1) - cnt] - c[-cnt]); 37 | } 38 | } 39 | cout< 2 | using namespace std; 3 | 4 | namespace fast_io { 5 | int read() { 6 | int x = 0, f = 0; char ch = getchar(); 7 | while (!isdigit(ch)) f |= ch == '-', ch = getchar(); 8 | while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar(); 9 | return f ? -x : x; 10 | } 11 | void read(int& x) {x = read();} 12 | template void print(T x) { 13 | if (x < 0) putchar('-'), x = -x; 14 | if (x >= 10) print(x / 10); 15 | putchar(x % 10 + '0'); 16 | } 17 | template void print(T x, char let) { 18 | print(x), putchar(let); 19 | } 20 | } 21 | using namespace fast_io; 22 | const int N = 300; 23 | int n, k, dp[N][N][N], vis[N][N][N]; 24 | 25 | int solve(int i, int j, int l) { 26 | if(vis[i][j][l] || i == 0 || j == 0) return dp[i][j][l]; 27 | vis[i][j][l] = 1; 28 | for(int cnt = l;cnt <=j;++cnt) dp[i][j][l] += solve(i - 1, j - cnt, cnt); 29 | return dp[i][j][l]; 30 | } 31 | 32 | int main() { 33 | read(n), read(k); 34 | for(int i = 0;i<=n;++i) dp[0][0][i] = 1; 35 | print(solve(k, n, 1), '\n'); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /other solutions/最小弹药数.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define pb push_back 3 | using namespace std; 4 | using vi = vector; 5 | 6 | const int N = 510; 7 | vi e[N], row; 8 | int vis[N], vis2[N], match[N]; 9 | int n, k, tot; 10 | 11 | bool Find(int u) { 12 | for(int v : e[u]) { 13 | if(!vis[v]) { 14 | vis[v] = 1; 15 | if(match[v] == 0 || Find(match[v])) { 16 | match[v] = u; 17 | return true; 18 | } 19 | } 20 | } 21 | return false; 22 | } 23 | 24 | 25 | int main(){ 26 | ios::sync_with_stdio(0); 27 | cin.tie(0); 28 | cin>>n>>k; 29 | for(int i = 1;i<=k;i++) { 30 | int r, c; 31 | cin>>r>>c; 32 | e[r].pb(c); 33 | if(!vis[r]) row.pb(r); 34 | vis[r] ++; 35 | vis2[c] ++; 36 | } 37 | int ans = 0; 38 | for(int i = 1;i<=n;i++) { 39 | if(vis[i]) ans++; 40 | if(vis2[i]) ans++; 41 | } 42 | ans = 0; 43 | for(int i : row) { 44 | memset(vis, 0, sizeof(vis)); 45 | if(Find(i)) { 46 | ans++; 47 | } 48 | } 49 | cout< 2 | using namespace std; 3 | 4 | const int N = 2000; 5 | const int dir[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; 6 | int dp[N][N], a[N][N]; 7 | int n; 8 | 9 | bool ok(int x, int y) { 10 | return(x > 0 && x <= n && y > 0 && y <= n); 11 | } 12 | 13 | 14 | int dfs(int x, int y) { 15 | if(dp[x][y] != -1) return dp[x][y]; 16 | for(int i = 0;i<4;++i) { 17 | int nxtx = x + dir[i][0]; 18 | int nxty = y + dir[i][1]; 19 | if(!ok(nxtx, nxty)) continue; 20 | if(a[nxtx][nxty] > a[x][y]) dp[x][y] = max(dp[x][y], dfs(nxtx, nxty) + 1); 21 | } 22 | if(dp[x][y] == -1) dp[x][y] = 0; 23 | return dp[x][y]; 24 | } 25 | 26 | int main() { 27 | ios::sync_with_stdio(0); 28 | cin.tie(0); 29 | cin>>n; 30 | memset(dp, -1, sizeof(dp)); 31 | for(int i = 1;i<=n;++i) { 32 | for(int j = 1;j<=n;++j) { 33 | cin>>a[i][j]; 34 | } 35 | } 36 | int ans = 0; 37 | for(int i = 1;i<=n;++i) { 38 | for(int j = 1;j<=n;++j) { 39 | ans = max(ans, dfs(i, j)); 40 | } 41 | } 42 | cout< 2 | using namespace std; 3 | const int N = 1e4 + 10; 4 | const int INF= 1e9 + 10; 5 | int n, m; 6 | struct path{ 7 | int from, to, cost; 8 | }e[N]; 9 | int dist[N]; 10 | int main(){ 11 | for(int i = 1;i>n>>m; 14 | for(int i = 1;i<=m;i++){ 15 | int a, b, c; 16 | cin>>a>>b>>c; 17 | e[i].from = a; 18 | e[i].to = b; 19 | e[i].cost = c; 20 | } 21 | 22 | for(int i = 1;i<=n;i++){ 23 | bool update = false; 24 | for(path x: e){ 25 | int u = x.from; 26 | int v = x.to; 27 | int w = x.cost; 28 | if(dist[v] > dist[u] + w){ 29 | dist[v] = dist[u] + w; 30 | update = true; 31 | } 32 | if(dist[u] > dist[v] + w){ 33 | dist[u] = dist[v] + w; 34 | update = true; 35 | } 36 | } 37 | if(!update){ 38 | cout< 2 | using namespace std; 3 | using ll = long long; 4 | const int N = 1e3 + 5; 5 | const int M = 2e4 + 5; 6 | 7 | int n, e, a[N], b[N]; 8 | ll dp[N][M]; 9 | 10 | template 11 | void ckmax(T& a, T b) { 12 | a = max(a, b); 13 | } 14 | 15 | int main() { 16 | cin.tie(0)->ios::sync_with_stdio(0); 17 | #ifdef LOCAL 18 | freopen("in.txt", "r", stdin); 19 | freopen("out.txt", "w", stdout); 20 | #endif 21 | cin>>n>>e; 22 | for(int i = 1;i<=n;++i) cin>>a[i]>>b[i]; 23 | for(int i = 0;i= a[i - 1] + b[i - 1]) ckmax(dp[i][j], dp[i - 1][j]); 29 | else if(j >= b[i - 1]) ckmax(dp[i][j], dp[i - 1][j - b[i - 1]] + b[i - 1]); 30 | else if(j + a[i - 1] < M) ckmax(dp[i][j], dp[i - 1][j + a[i - 1]]); 31 | if(j < b[i - 1]) ckmax(dp[i][j], mx + j); 32 | } 33 | } 34 | ll ans = 0; 35 | for(auto x : dp[n]) ckmax(ans, x); 36 | cout< 2 | using namespace std; 3 | 4 | const int N = 1e3 + 10; 5 | int w, b; 6 | 7 | double dp[N][N]; 8 | 9 | double cal(int w, int b) { 10 | if(w == 0 && b == 0) return 0; 11 | if(dp[w][b] != 0.0) return dp[w][b]; 12 | // get a white mice 13 | dp[w][b] = (double)(w) / (double)(w + b); 14 | // get a black mice 15 | double tmp = (double)(b) / (double)(w + b); 16 | // a black mice came out and the dragon got a black mice 17 | if(b >= 3) 18 | dp[w][b] += tmp * (double)(b - 1) / (double)(w + b - 1) * (double)(b - 2) / (double)(w + b - 2) * cal(w, b - 3); 19 | // a white mice came out and dragon got a black mice 20 | if(b >= 2 && w >= 1) 21 | dp[w][b] += tmp * (double)(w) / (double)(w + b - 1) * (double)(b - 1) / (double)(w + b - 2) * cal(w - 1, b - 2); 22 | return dp[w][b]; 23 | } 24 | 25 | int main() { 26 | ios::sync_with_stdio(0); 27 | cin.tie(0); 28 | #ifdef LOCAL 29 | freopen("in.txt", "r", stdin); 30 | freopen("out.txt", "w", stdout); 31 | #endif 32 | dp[1][0] = 1; 33 | cin>>w>>b; 34 | cout< 2 | using namespace std; 3 | template using pq = priority_queue; 4 | template using pqg = priority_queue, greater >; 5 | pq fir; 6 | pqg sec; 7 | int n; 8 | 9 | int main() { 10 | ios::sync_with_stdio(0); 11 | cin.tie(0); 12 | cin>>n; 13 | while(n--) { 14 | string s; cin>>s; 15 | if(s == "query") { 16 | if(fir.empty()) cout<<"empty"<<'\n'; 17 | else cout<>x; 21 | if(fir.empty()) fir.push(x); 22 | else if(x > fir.top()) { 23 | sec.push(x); 24 | if(sec.size() > fir.size()) { 25 | fir.push(sec.top()); 26 | sec.pop(); 27 | } 28 | 29 | } 30 | else { 31 | fir.push(x); 32 | if(fir.size() > sec.size() + 1) { 33 | sec.push(fir.top()); 34 | fir.pop(); 35 | } 36 | } 37 | } 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /CCC/2022/S4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | using ll = long long; 4 | const int N = 2e6 + 5; 5 | int n, c, per[N], a[N], psa[N]; 6 | ll ans, cnt[N]; 7 | 8 | int main() { 9 | #ifdef LOCAL 10 | freopen("in.txt", "r", stdin); 11 | freopen("out.txt", "w", stdout); 12 | #endif 13 | scanf("%d%d", &n, &c); 14 | int m = c / 2; 15 | for(int i = 0;i<=c;++i) per[i] = m++; 16 | for(int i = 1;i<=n;++i) { 17 | scanf("%d", a + i); 18 | ++psa[a[i]]; 19 | ++psa[a[i] + c]; 20 | ++cnt[a[i]]; 21 | } 22 | for(int i = 1;i<=c * 2;++i) psa[i] += psa[i - 1]; 23 | for(int i = 0;i<=c;++i) { 24 | ans += cnt[i] * (cnt[i] - 1) * (cnt[i] - 2) / 6; 25 | ll tmp = psa[per[i]] - psa[i]; 26 | ans += cnt[i] * tmp * (tmp - 1) / 2; 27 | ans += cnt[i] * (cnt[i] - 1) / 2 * tmp; 28 | } 29 | if(per[per[0]] == c) { 30 | for(int i = 0;i 2 | using namespace std; 3 | #define pb push_back 4 | #define all(v) v.begin(), v.end() 5 | #define fir first 6 | #define sec second 7 | using ll = long long; 8 | using pll = pair; 9 | const int N = 1e5 + 5; 10 | int n, ans[N], tot; 11 | set bst; 12 | struct triple {ll u, v; int id;}; 13 | vector a; 14 | 15 | int main() { 16 | cin.tie(0)->ios::sync_with_stdio(0); 17 | #ifdef LOCAL 18 | freopen("in.txt", "r", stdin); 19 | freopen("out.txt", "w", stdout); 20 | #endif 21 | cin>>n; 22 | for(int i = 1;i<=n;++i) { 23 | int u, v; cin>>u>>v; 24 | a.pb({u - v, u + v, i}); 25 | } 26 | sort(all(a), [&](triple& a, triple& b) { 27 | if(a.u != b.u) return a.u < b.u; 28 | return a.v > b.v; 29 | }); 30 | for(auto[u, v, id] : a) { 31 | auto x = bst.lower_bound({v, 0}); 32 | if(x == bst.end()) ans[id] = ++tot; 33 | else { 34 | ans[id] = ans[(*x).sec]; 35 | bst.erase(x); 36 | } 37 | bst.insert({v, id}); 38 | } 39 | cout< 2 | using namespace std; 3 | #define pb push_back 4 | #define all(v) v.begin(), v.end() 5 | const int N = 1e5 + 10; 6 | int n, color[N], last[N]; 7 | vector e[N]; 8 | unordered_set vis[N]; 9 | 10 | inline void dfs(int u, int fa = 0) { 11 | int c = last[fa] + 1; 12 | while(vis[fa].count(c)) ++c; 13 | vis[fa].insert(c); 14 | vis[u].insert(color[fa]); 15 | vis[u].insert(c); 16 | color[u] = c; 17 | last[fa] = c; 18 | for(int v : e[u]) { 19 | if(v == fa) continue; 20 | dfs(v, u); 21 | } 22 | } 23 | 24 | int main() { 25 | ios::sync_with_stdio(0); 26 | cin.tie(0); 27 | #ifdef LOCAL 28 | freopen("in.txt", "r", stdin); 29 | freopen("out.txt", "w", stdout); 30 | #else 31 | freopen("planting.in", "r", stdin); 32 | freopen("planting.out", "w", stdout); 33 | #endif 34 | cin>>n; 35 | for(int i = 1;i>u>>v; 38 | e[u].pb(v); 39 | e[v].pb(u); 40 | } 41 | dfs(1); 42 | int ans = 0; 43 | for(int i = 1;i<=n;++i) ans = max(ans, color[i]); 44 | cout< 2 | using namespace std; 3 | const int N = 4e4 + 5; 4 | const int inf = 0x3f3f3f3f; 5 | struct edge{int u, v, nxt;} e[N]; 6 | int head[N], n, tot, sz[N], ans = inf, ret; 7 | 8 | void dfs(int u = 1, int fa = 0) { 9 | sz[u] = 0; 10 | int tmp = 0; 11 | for(int i = head[u];i;i=e[i].nxt) { 12 | int v = e[i].v; 13 | if(fa == v) continue; 14 | dfs(v, u); 15 | sz[u] += sz[v]; 16 | tmp = max(tmp, sz[v]); 17 | } 18 | ++sz[u]; 19 | tmp = max(tmp, n - sz[u]); 20 | if(tmp == ans) ret = min(ret, u); 21 | if(tmp < ans) { 22 | ret = u; 23 | ans = tmp; 24 | } 25 | } 26 | 27 | int main() { 28 | cin.tie(0)->ios::sync_with_stdio(0); 29 | #ifdef LOCAL 30 | freopen("in.txt", "r", stdin); 31 | freopen("out.txt", "w", stdout); 32 | #endif 33 | cin>>n; 34 | for(int i = 1;i>u>>v; 37 | e[++tot] = {u, v, head[u]}; 38 | head[u] = tot; 39 | e[++tot] = {v, u, head[v]}; 40 | head[v] = tot; 41 | } 42 | dfs(); 43 | cout< 2 | using namespace std; 3 | #define f first 4 | #define s second 5 | int main(){ 6 | ios::sync_with_stdio(false); 7 | cin.tie(0); 8 | int n,t; 9 | cin>>n>>t; 10 | vector>> v(n+1); 11 | while(t--){ 12 | int a,b,c; 13 | cin>>a>>b>>c; 14 | v[a].push_back({b,c}); 15 | v[b].push_back({a,c}); 16 | } 17 | vector> pencils; 18 | int k; 19 | cin>>k; 20 | while(k--){ 21 | int x,y; 22 | cin>>x>>y; 23 | pencils.push_back({x,y}); 24 | } 25 | int d; 26 | cin>>d; 27 | int dist[n+1]; 28 | for(int i=0; i<=n; i++) dist[i]=1e9; 29 | dist[d]=0; 30 | queue> q; 31 | q.push({d,0}); 32 | while(!q.empty()){ 33 | int node=q.front().f; 34 | q.pop(); 35 | for(auto p:v[node]){ 36 | if(dist[node]+p.s < dist[p.f]){ 37 | dist[p.f] = dist[node]+p.s; 38 | q.push(p); 39 | } 40 | } 41 | } 42 | int ans=1e9; 43 | for(auto p:pencils){ 44 | ans=min(ans,dist[p.f]+p.s); 45 | } 46 | cout< 2 | using namespace std; 3 | 4 | struct edge { 5 | int u, v, w; 6 | }; 7 | vector e; 8 | int n, k; 9 | const int N = 1e5 + 5; 10 | int f[N], sz[N]; 11 | 12 | int Find(int x) {return f[x] == x ? x : f[x] = Find(f[x]);} 13 | void merge(int u, int v) { 14 | int fu = Find(u); 15 | int fv = Find(v); 16 | f[fu] = fv; 17 | sz[fv] += sz[fu]; 18 | } 19 | 20 | int main() { 21 | cin.tie(0)->ios::sync_with_stdio(0); 22 | #ifdef LOCAL 23 | freopen("in.txt", "r", stdin); 24 | freopen("out.txt", "w", stdout); 25 | #endif 26 | cin>>n>>k; 27 | iota(f, f + n + 1, 0); 28 | long long ans = 0; 29 | for(int i = 1;i>u>>v>>w; 32 | e.push_back({u, v, w}); 33 | ans += w; 34 | } 35 | for(int i = 1;i<=k;++i) { 36 | int x; cin>>x; 37 | sz[x] = 1; 38 | } 39 | sort(e.begin(), e.end(), [&](edge& a, edge& b) { 40 | return a.w > b.w; 41 | }); 42 | for(auto[u, v, w] : e) if(!(sz[Find(u)] && sz[Find(v)])) { 43 | merge(u, v); 44 | ans -= w; 45 | } 46 | cout< 2 | using namespace std; 3 | const int L = 15; 4 | const int N = 1e5 + 5; 5 | 6 | int mx, cnt; 7 | 8 | struct num { 9 | int a[L], n; 10 | num(int x) { 11 | n = x; 12 | int len = 0; 13 | memset(a, 0, sizeof(a)); 14 | while(x) { 15 | a[++len] = x % 10; 16 | x /= 10; 17 | } 18 | mx = max(len, mx); 19 | } 20 | bool operator<(const num& o) const { 21 | int i = cnt; 22 | while(a[i] == o.a[i] && i > 1) { 23 | --i; 24 | } 25 | return a[i] < o.a[i]; 26 | } 27 | }; 28 | vector a; 29 | 30 | int main() { 31 | cin.tie(0)->ios::sync_with_stdio(0); 32 | #ifdef LOCAL 33 | freopen("in.txt", "r", stdin); 34 | freopen("out.txt", "w", stdout); 35 | #endif 36 | int n; cin>>n; 37 | for(int i = 1;i<=n;++i) { 38 | int x; cin>>x; 39 | a.push_back(num(x)); 40 | } 41 | cnt = 2; 42 | for(cnt = 1;cnt<=mx;++cnt) { 43 | sort(a.begin(), a.end()); 44 | for(int i = 1;i<=n;++i) { 45 | cout< 2 | using namespace std; 3 | using ll = long long; 4 | const int N = 1e5 + 5; 5 | ll n, a[N], p[N]; 6 | vector l, r; 7 | 8 | int main() { 9 | cin.tie(0)->ios::sync_with_stdio(0); 10 | #ifdef LOCAL 11 | freopen("in.txt", "r", stdin); 12 | freopen("out.txt", "w", stdout); 13 | #endif 14 | cin>>n; 15 | ll on = n; 16 | p[0] = 1; 17 | int len = 0; 18 | while(n > 0) { 19 | a[++len] = n % 3; 20 | n /= 3; 21 | p[len] = p[len - 1] * 3; 22 | } 23 | for(int i = 1;i<=len + 1;++i) { 24 | a[i + 1] += a[i] / 3; 25 | a[i] %= 3; 26 | if(a[i] == 2) { 27 | r.push_back(p[i - 1]); 28 | a[i] = 0; 29 | ++a[i + 1]; 30 | } 31 | if(a[i] == 1) { 32 | l.push_back(p[i - 1]); 33 | } 34 | } 35 | sort(l.begin(), l.end()); 36 | sort(r.begin(), r.end()); 37 | for(int i = 0;i 2 | using namespace std; 3 | using pii = pair; 4 | #define pb push_back 5 | #define x first 6 | #define y second 7 | int n, t; 8 | vector a; 9 | 10 | int main() { 11 | cin.tie(0)->ios::sync_with_stdio(0); 12 | #ifdef LOCAL 13 | freopen("in.txt", "r", stdin); 14 | freopen("out.txt", "w", stdout); 15 | #endif 16 | cin>>n>>t; 17 | for(int i = 1;i<=t;++i) { 18 | int x, y; 19 | cin>>x>>y; 20 | a.pb({x, y}); 21 | } 22 | a.pb({0, 0}); 23 | a.pb({n + 1, n + 1}); 24 | sort(a.begin(), a.end(), [&](pii& a, pii& b) { 25 | return a.x < b.x; 26 | }); 27 | int ans = 0; 28 | for(int i = 0;i b; 30 | b.pb(0); 31 | b.pb(n + 1); 32 | for(int j = i + 1;j 2 | using namespace std; 3 | #define int long long 4 | const int MOD = 1e9 + 7; 5 | const int N = 5e4 + 10; 6 | int n, m, f[N][22]; 7 | 8 | template 9 | T1 fast_pow(T1 a, T2 b) { 10 | if(b == 0) return 1; 11 | T1 tmp = fast_pow(a, b >> 1); 12 | if(b & 1) return tmp * tmp % MOD * a % MOD; 13 | else return tmp * tmp % MOD; 14 | } 15 | 16 | signed main() { 17 | ios::sync_with_stdio(0); 18 | cin.tie(0); 19 | #ifdef LOCAL 20 | freopen("in.txt", "r", stdin); 21 | freopen("out.txt", "w", stdout); 22 | #endif 23 | cin>>n>>m; 24 | int ans = 0; 25 | for(int i = 1;i<=n;++i) { 26 | f[i][1] = i; 27 | int x = i; 28 | for(int j = 2;j<=22;++j) { 29 | f[i][j] = (f[i][j - 1] * x % MOD + f[i][j - 1]) % MOD; 30 | x = x * x % MOD; 31 | } 32 | int M = m; 33 | for(int j = 22;j>=0;--j) { 34 | if(M >= (1ll << j)) { 35 | M -= 1ll << j; 36 | ans += fast_pow(i, M) * f[i][j + 1] % MOD; 37 | ans %= MOD; 38 | } 39 | } 40 | } 41 | cout< 0) { 19 | solve(); 20 | t--; 21 | } 22 | } 23 | static void solve() throws IOException{ 24 | long a = readInt(); 25 | long b = readInt(); 26 | long c = readInt(); 27 | long ans1 = Math.max(Math.max(b, c) - a + 1, 0); 28 | long ans2 = Math.max(Math.max(a, c) - b + 1, 0); 29 | long ans3 = Math.max(Math.max(b, a) - c + 1, 0); 30 | System.out.println(ans1 + " " + ans2 + " " + ans3); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /CCC/2001/01S3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | const int N = 1e3; 5 | string s[N]; 6 | int a[N][2], f[N]; 7 | vector ans; 8 | 9 | inline int Find(int x){ 10 | if(x == f[x]) return x; 11 | return f[x] = Find(f[x]); 12 | } 13 | 14 | inline void Union(int x, int y){ 15 | f[x] = y; 16 | } 17 | 18 | int main(){ 19 | int n = 0; 20 | while(true){ 21 | cin>>s[++n]; 22 | if(s[n] == "**") break; 23 | a[n][0] = s[n][0] - 'A' + 1; 24 | a[n][1] = s[n][1] - 'A' + 1; 25 | } 26 | for(int i = 1;i<=n;i++){ 27 | for(int j = 1;j 2 | using namespace std; 3 | 4 | string s[5]; 5 | unordered_set key = {'a', 'e', 'i', 'o', 'u', ' '}; 6 | 7 | void solve() { 8 | for(int i = 1;i<=4;++i) { 9 | getline(cin, s[i]); 10 | for(auto& c : s[i]) if(c <= 'Z' && c != ' ') c -= 'A', c += 'a'; 11 | int j = s[i].size() - 1; 12 | while(j >= 0 && s[i][j] != ' ') --j; 13 | s[i] = j + 1 == s[i].size() ? "" : s[i].substr(j + 1); 14 | if(s[i].size() == 0) continue; 15 | j = s[i].size() - 1; 16 | while(j > 0 && !key.count(s[i][j])) --j; 17 | s[i] = s[i].substr(j); 18 | } 19 | if(s[1] == s[2] && s[2] == s[3] && s[3] == s[4]) puts("perfect"); 20 | else if(s[1] == s[2] && s[3] == s[4]) puts("even"); 21 | else if(s[1] == s[3] && s[2] == s[4]) puts("cross"); 22 | else if(s[1] == s[4] && s[2] == s[3]) puts("shell"); 23 | else puts("free"); 24 | } 25 | 26 | int main() { 27 | cin.tie(0)->ios::sync_with_stdio(0); 28 | #ifdef LOCAL 29 | freopen("in.txt", "r", stdin); 30 | freopen("out.txt", "w", stdout); 31 | #endif 32 | int t; cin>>t; 33 | getline(cin, s[1]); 34 | while(t--) solve(); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /CCO/98/P1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct big { 5 | int n[105]; 6 | big operator+(const big& other) const { 7 | big ans; 8 | memset(ans.n, 0, sizeof(ans.n)); 9 | int carry = 0; 10 | for(int i = 1;i<=100;++i) { 11 | ans.n[i] = n[i] + other.n[i] + carry; 12 | carry = ans.n[i] / 10; 13 | ans.n[i] %= 10; 14 | } 15 | return ans; 16 | } 17 | void print() { 18 | int i; 19 | for(i = 100;i;--i) { 20 | if(n[i]) break; 21 | } 22 | string ans; 23 | for(int j = i;j;--j) { 24 | ans += ('0' + n[j]); 25 | } 26 | cout<ios::sync_with_stdio(0); 32 | #ifdef LOCAL 33 | freopen("in.txt", "r", stdin); 34 | freopen("out.txt", "w", stdout); 35 | #endif 36 | f[1].n[1] = f[2].n[1] = 1; 37 | for(int i = 3;i<=205;++i) { 38 | f[i] = f[i - 1] + f[i - 2]; 39 | } 40 | int n; cin>>n; 41 | while(1) { 42 | f[n].print(); 43 | cin>>n; 44 | if(n == 0) break; 45 | } 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Mock_USACO/矩阵行走.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 105; 4 | int n, dp[N][N][N], a[N][N]; 5 | int dir[2][2] = {{-1, 0}, {0, -1}}; 6 | 7 | template void ckmax(T& a, T b) { a = max(a, b); } 8 | bool ok(int x, int y) {return x > 0 && y > 0;} 9 | 10 | int dfs(int A, int b, int c, int d) { 11 | if(dp[A][b][c] != -1) return dp[A][b][c]; 12 | for(int i = 0;i<2;++i) { 13 | for(int j = 0;j<2;++j) { 14 | int x = A + dir[i][0]; 15 | int y = b + dir[i][1]; 16 | int z = c + dir[j][0]; 17 | int w = d + dir[j][1]; 18 | if(ok(x, y) && ok(z, w)) { 19 | ckmax(dp[A][b][c], dfs(x, y, z, w) + abs(a[A][b] - a[c][d])); 20 | } 21 | } 22 | } 23 | return dp[A][b][c]; 24 | } 25 | 26 | int main() { 27 | cin.tie(0)->ios::sync_with_stdio(0); 28 | #ifdef LOCAL 29 | freopen("in.txt", "r", stdin); 30 | freopen("out.txt", "w", stdout); 31 | #endif 32 | memset(dp, -1, sizeof(dp)); 33 | dp[1][1][1] = 0; 34 | cin>>n; 35 | for(int i = 1;i<=n;++i) 36 | for(int j = 1;j<=n;++j) cin>>a[i][j]; 37 | cout< 7 | #include 8 | #include 9 | using namespace std; 10 | using ll = long long; 11 | unordered_map f; 12 | int n; 13 | 14 | inline void FindTree(int a){ 15 | if(a == 1 || a == 2) return; 16 | // the next possible number of trees which the weight will differ 17 | int nxt; 18 | for(int k = a;k>=2;k = nxt){ 19 | // if already computed, use the result 20 | if(f[a/k] == 0) FindTree(a/k); 21 | // calculate the next nxt 22 | // anything smaller than nxt has subtrees of weights that are different 23 | // a / k + 1 indicate the next weight of subtrees 24 | nxt = a / (a / k + 1); 25 | // we know that from k to k - nxt, the weight of subtrees are the same so we just multiply 26 | f[a] += f[a/k] * (k - nxt); 27 | } 28 | } 29 | 30 | int main() { 31 | ios::sync_with_stdio(false); 32 | scanf("%d", &n); 33 | // initializing 34 | f[1] = 1; 35 | f[2] = 1; 36 | FindTree(n); 37 | printf("%lld", f[n]); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /USACO/2019/Open/Gold/P1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 4e2 + 10; 4 | const int INF = 0x3f3f3f3f; 5 | int n, K, st[N][20], dp[N][N], sum[N]; 6 | 7 | int query(int a, int b) { 8 | int len = log2(b - a + 1); 9 | return max(st[a][len], st[b - (1 << len) + 1][len]); 10 | } 11 | 12 | int main() { 13 | freopen("snakes.in", "r", stdin); 14 | freopen("snakes.out", "w", stdout); 15 | scanf("%lld %lld", &n, &K); 16 | ++K; 17 | for(int i = 1;i<=n;++i) scanf("%lld", &st[i][0]), sum[i] = sum[i - 1] + st[i][0]; 18 | for(int j = 1;(1 << j)<=n;++j) { 19 | for(int i = 1;i + (1 << j) - 1 <= n;++i) { 20 | st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); 21 | } 22 | } 23 | memset(dp, 0x3f, sizeof(dp)); 24 | dp[0][0] = 0; 25 | for(int i = 1;i<=n;++i) { // first i 26 | for(int j = 1;j<=K;++j) { // change size by j 27 | for(int k = 0;k 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define ins insert 6 | #define lb lower_bound 7 | using vi = vector; 8 | using pii = pair; 9 | 10 | const int N = 1034; 11 | int n, cnt[N], e[N][N]; 12 | vi ans; 13 | 14 | 15 | void dfs(int u) { 16 | for(int v = 0; v <= 500;v++) { 17 | if(!e[u][v]) continue; 18 | e[u][v] --; 19 | e[v][u] --; 20 | dfs(v); 21 | } 22 | ans.pb(u); 23 | } 24 | 25 | int main(){ 26 | ios::sync_with_stdio(0); 27 | cin.tie(0); 28 | cin>>n; 29 | for(int i = 1;i<=n;i++) { 30 | int a, b; 31 | cin>>a>>b; 32 | e[a][b]++; 33 | e[b][a]++; 34 | cnt[a] ++; 35 | cnt[b] ++; 36 | } 37 | int start = -1; 38 | for(int i = 1;i<=500;i++) { 39 | if(cnt[i] & 1) { 40 | start = i; 41 | break; 42 | } 43 | } 44 | if(start == -1) { 45 | for(int i = 1;i<=500;i++) { 46 | start = i; 47 | break; 48 | } 49 | } 50 | dfs(start); 51 | reverse(begin(ans), end(ans)); 52 | for(int i : ans) cout< 2 | #define endl "\n" 3 | using namespace std; 4 | using ll = unsigned long long; 5 | ll MOD = 1e9 + 7; 6 | ll n; 7 | 8 | struct mat{ 9 | ll a[3][3]; 10 | mat operator * (mat& other){ 11 | mat ans; 12 | memset(ans.a, 0, sizeof(ans.a)); 13 | for(int i = 1;i<=2;i++){ 14 | for(int j = 1;j<=2;j++){ 15 | for(int k = 1;k<=2;k++){ 16 | ans.a[i][j] += a[i][k] % MOD * other.a[k][j] % MOD; 17 | ans.a[i][j] %= MOD; 18 | } 19 | } 20 | } 21 | return ans; 22 | } 23 | }; 24 | 25 | mat fast_pow(mat a, ll k){ 26 | if(k == 1) return a; 27 | mat tmp = fast_pow(a, k >> 1); 28 | if(k & 1) return tmp * tmp * a; 29 | else return tmp * tmp; 30 | } 31 | 32 | int main(){ 33 | cin>>n; 34 | mat matr; 35 | matr.a[1][1] = 1; 36 | matr.a[1][2] = 1; 37 | matr.a[2][1] = 1; 38 | matr.a[2][2] = 0; 39 | mat b; 40 | b.a[1][1] = 1; 41 | b.a[1][2] = 0; 42 | b.a[2][1] = 1; 43 | b.a[2][2] = 0; 44 | mat ans = fast_pow(matr, n - 2) * b; 45 | cout< 2 | #include 3 | #define pushup(rt) Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]); 4 | #define endl "\n" 5 | using namespace std; 6 | const int N = 1e5 + 10; 7 | int Max[N << 2]; 8 | int n, m, a[N]; 9 | 10 | inline void build(int rt, int l, int r){ 11 | if(l == r){ 12 | cin >> Max[rt]; 13 | return; 14 | } 15 | int mid = (l + r) >> 1; 16 | build(rt << 1, l, mid); 17 | build(rt << 1 | 1, mid + 1, r); 18 | pushup(rt); 19 | } 20 | 21 | inline int query(int rt, int l, int r, int x, int y){ 22 | if(l == x && y == r){ 23 | return Max[rt]; 24 | } 25 | int mid = (l + r) >> 1; 26 | if(y <= mid) return query(rt << 1, l, mid, x, y); 27 | else if(mid < x) return query(rt << 1 | 1, mid + 1, r, x, y); 28 | else return max(query(rt << 1, l, mid, x, mid), query(rt << 1 | 1, mid + 1, r, mid + 1, y)); 29 | } 30 | 31 | int main() { 32 | ios::sync_with_stdio(0); 33 | cin.tie(0); cout.tie(0); 34 | cin>>n>>m; 35 | build(1, 1, n); 36 | for(int i = 1;i<=m;i++){ 37 | int a, b; 38 | cin>>a>>b; 39 | cout< 2 | using namespace std; 3 | 4 | const int N = 2e5 + 5; 5 | int n, a[N], b[N], ret[N]; 6 | set bst; 7 | 8 | int c[N]; 9 | 10 | void update(int a, int b) { 11 | for(int i = a;i<=n;i+=i&(-i)) c[i] += b; 12 | } 13 | 14 | int query(int a) { 15 | int ans = 0; 16 | for(int i = a;i;i-=i&(-i)) ans += c[i]; 17 | return ans; 18 | } 19 | 20 | int query(int a, int b) { 21 | return query(b) - query(a - 1); 22 | } 23 | 24 | int main() { 25 | cin.tie(0)->ios::sync_with_stdio(0); 26 | #ifdef LOCAL 27 | freopen("in.txt", "r", stdin); 28 | freopen("out.txt", "w", stdout); 29 | #endif 30 | cin>>n; 31 | for(int i = 1;i<=n;++i) cin>>a[i], b[i] = a[i]; 32 | sort(b + 1, b + n + 1); 33 | for(int i = 1;i<=n;++i) if(b[i] < i) { 34 | cout<<-1<<'\n'; 35 | return 0; 36 | } 37 | for(int i = 1;i<=n;++i) bst.insert(i); 38 | for(int i = n;i;--i) { 39 | int x = *prev(bst.upper_bound(a[i])); 40 | bst.erase(x); 41 | ret[x] = i; 42 | } 43 | long long ans = 0; 44 | for(int i = 1;i<=n;++i) { 45 | ans += query(ret[i], n); 46 | update(ret[i], 1); 47 | } 48 | cout< 2 | using namespace std; 3 | using ll = long long; 4 | using pll = pair; 5 | const int N = 3e3 + 5; 6 | ll dp[N]; // time it takes for the first i to grow 7 | ll h, mx[N]; 8 | pll p[N]; 9 | int n; 10 | 11 | ll dist(int l, int r) { 12 | return max((p[r].first - p[l].first) * 3, mx[r]); 13 | } 14 | 15 | int main() { 16 | cin.tie(0)->ios::sync_with_stdio(0); 17 | #ifdef LOCAL 18 | freopen("in.txt", "r", stdin); 19 | freopen("out.txt", "w", stdout); 20 | #endif 21 | cin>>n>>h; 22 | for(int i = 1;i<=n;++i) cin>>p[i].first>>p[i].second; 23 | memset(dp, 0x3f, sizeof(dp)); 24 | dp[0] = 0; 25 | sort(p + 1, p + n + 1); 26 | for(int i = 1;i<=n;++i) { 27 | memset(mx, 0, sizeof(mx)); 28 | mx[i] = p[i].second; 29 | dp[i] = min(dp[i], dp[i - 1] + p[i].second + p[i].first - p[i - 1].first); 30 | for(int j = i + 1;j<=n;++j) { 31 | mx[j] = max(mx[j - 1] + p[j].first - p[j - 1].first, p[j].second + p[j].first - p[i].first); 32 | dp[j] = min(dp[j], dp[i - 1] + dist(i, j) + p[i].first - p[i - 1].first); 33 | } 34 | } 35 | cout< 2 | using namespace std; 3 | const int N = 1e3+10; 4 | const long long INF = 3e9; 5 | int n, m; 6 | int Map[N][N]; 7 | long long dp[N][N][3]; // 0 left, 1 up, 2 down 8 | 9 | int main() { 10 | cin>>n>>m; 11 | for(int i = 1;i<=n;i++){ 12 | for(int j = 1;j<=m;j++){ 13 | cin>>Map[i][j]; 14 | dp[i][j][0] = dp[i][j][1] = dp[i][j][2] = -INF; 15 | } 16 | } 17 | for(int i = 0;i<=n + 2;i++){ 18 | for(int j = 0;j<=m + 2;j++){ 19 | dp[i][j][0] = dp[i][j][1] = dp[i][j][2] = -INF; 20 | } 21 | } 22 | dp[1][1][0] = dp[1][1][1] = dp[1][1][2] = Map[1][1]; 23 | for(int j = 1;j<=m;j++){ 24 | for(int i = 1;i<=n;i++){ 25 | if(j == 1 && i == 1) continue; 26 | dp[i][j][1] = max(dp[i - 1][j][0], dp[i - 1][j][1]) + Map[i][j]; 27 | dp[i][j][0] = max(dp[i][j - 1][2], max(dp[i][j - 1][0],dp[i][j - 1][1])) + Map[i][j]; 28 | } 29 | 30 | for(int i = n;i>=1;i--){ 31 | if(j == 1 && i == 1) continue; 32 | dp[i][j][2] = max(dp[i + 1][j][0], dp[i + 1][j][2]) + Map[i][j]; 33 | } 34 | } 35 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | 5 | const int N = 12; 6 | const int MOD = 4399; 7 | int n, k; 8 | 9 | struct mat{ 10 | int a[N][N]; 11 | mat operator *(mat& other){ 12 | mat ans; 13 | memset(ans.a, 0, sizeof(ans.a)); 14 | for(int i = 1;i<=n;i++){ 15 | for(int j = 1;j<=n;j++){ 16 | for(int k = 1;k<=n;k++){ 17 | ans.a[i][j] += a[i][k] * other.a[k][j] % MOD; 18 | } 19 | } 20 | } 21 | return ans; 22 | } 23 | } a; 24 | 25 | mat fast_pow(mat a, int k){ 26 | if(k == 1) return a; 27 | mat tmp = fast_pow(a, k >> 1); 28 | if(k & 1) { 29 | return tmp * tmp * a; 30 | } else { 31 | return tmp * tmp; 32 | } 33 | } 34 | 35 | int main(){ 36 | cin>>n>>k; 37 | for(int i = 1;i<=n;i++){ 38 | for(int j = 1;j<=n;j++){ 39 | cin>>a.a[i][j]; 40 | } 41 | } 42 | mat res = a * a; 43 | res = fast_pow(a, k); 44 | int ans = 0; 45 | for(int i = 1;i<=n;i++){ 46 | ans += res.a[i][i]; 47 | ans %= MOD; 48 | } 49 | cout< 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define pf push_front 6 | #define mp make_pair 7 | 8 | using pii = pair; 9 | 10 | const int N = 1e5 + 10; 11 | int a[N], n, k; 12 | deque dq, dq2; 13 | vector b; 14 | 15 | int main(){ 16 | cin>>n>>k; 17 | for(int i = 1;i<=n;i++) cin>>a[i]; 18 | for(int i = 1;i<=n;i++) { 19 | while(!dq.empty() && dq.front().second <= i - k){ 20 | dq.pop_front(); 21 | } 22 | while(!dq.empty() && dq.back().first <= a[i]) { 23 | dq.pop_back(); 24 | } 25 | while(!dq2.empty() && dq2.front().second <= i - k) { 26 | dq2.pop_front(); 27 | } 28 | while(!dq2.empty() && dq2.back().first >= a[i]) { 29 | dq2.pop_back(); 30 | } 31 | dq.push_back(mp(a[i], i)); 32 | dq2.push_back(mp(a[i], i)); 33 | if(i >= k) { 34 | if(i != n) cout<