Advertisement
dvjsharma

Mercari OA

Mar 10th, 2025
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.63 KB | Source Code | 0 0
  1. // lcs
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. int longestCommonSubsequence(string& str1, string& str2, int maxCost) {
  7.     int len1 = str1.size();
  8.     int len2 = str2.size();
  9.     vector<vector<vector<int>>> dp(len1 + 1, vector<vector<int>>(len2 + 1, vector<int>(maxCost + 1, 0)));
  10.     for (int i = 1; i <= len1; ++i) {
  11.         for (int j = 1; j <= len2; ++j) {
  12.             if (str1[i - 1] == str2[j - 1])
  13.                 for(int cost=maxCost; cost >=0; cost-- ){
  14.                     dp[i][j][cost] = dp[i - 1][j - 1][cost] + 1;
  15.                 }
  16.             else{
  17.                 for(int cost=maxCost; cost>=0; cost-- ){
  18.                     dp[i][j][cost] = max(dp[i - 1][j][cost], dp[i][j - 1][cost]);
  19.                     int diff = abs((int)str1[i-1] - (int)str2[j-1]);
  20.                     diff = min(diff, 26 - diff);
  21.                     if(cost+diff<=maxCost){
  22.                         dp[i][j][cost] = max(dp[i][j][cost], dp[i-1][j-1][cost+diff] + 1);
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.     }
  28.     return *max_element(dp[len1][len2].begin(), dp[len1][len2].end());
  29. }
  30.  
  31. int main(){
  32.     int k;
  33.     string s1, s2;
  34.     cin>>s1;
  35.     cin>>s2;
  36.     cin>>k;
  37.     cout<<longestCommonSubsequence(s1, s2, k);
  38.     return 0;
  39. }
  40.  
  41. // process tree:
  42. #include <bits/stdc++.h>
  43. using namespace std;
  44.  
  45. int solve(int n) {
  46.     int p = 1, child = 2;
  47.     while(child<n) {
  48.         p++;
  49.         child+=p;
  50.     }
  51.     return p;
  52. }
  53. int main(){
  54.     int n; cin>>n;
  55.     cout<<solve(n)<<endl;
  56.     return 0;
  57. }
  58.  
  59. // optimal subarray:
  60.  
  61. #include <bits/stdc++.h>
  62. using namespace std;
  63.  
  64. long long int find(int n, vector<int> a){
  65.     int count = 0;
  66.     vector<int> v;
  67.     for(int i = 2; i<=200; i++) {
  68.         bool f = false;
  69.         for(int j = 2; j*j<=i; j++) {
  70.             if(i%j==0) {
  71.                 f=true;
  72.                 break;
  73.             }
  74.         }
  75.         if(f)continue;
  76.         count++;
  77.         v.push_back(i);
  78.     }
  79.  
  80.  
  81.     int ans = 0;
  82.     bool f = true;
  83.     map<long long int , int> m;
  84.     string s;
  85.     for(int i = 0; i<count; i++)s.push_back('0');
  86.     m[0]++;
  87.     for(int i=0; i < n; i++){
  88.         for(int j = 0; j<count; j++) {
  89.             int cnt = 0;
  90.             while(a[i]%v[j]==0) {
  91.                 cnt++;
  92.                 a[i]/=v[j];
  93.             }
  94.             if(s[j]=='1')cnt++;
  95.             s[j] = cnt%2+'0';
  96.         }
  97.         long long int temp = 0;
  98.         for(auto &x : s) {
  99.             temp*=2;
  100.             if(x=='1')temp++;
  101.         }
  102.         ans+=m[temp];
  103.         m[temp]++;
  104.     }
  105.     return ans;
  106. }
  107. int main(){
  108.     int n;
  109.     cin>>n;
  110.     vector<int> a(n);
  111.     for(auto &x : a)cin>>x;
  112.     cout<<find(n, a);
  113.     return 0;
  114. }
  115.  
  116. // #include <bits/stdc++.h>
  117. // using namespace std;
  118.  
  119. // long long int countGoodSubarrays(int n, vector<int>& arr) {
  120. //     vector<int> primes;
  121. //     for (int i = 2; i <= 200; i++) {
  122. //         bool isPrime = true;
  123. //         for (int j = 2; j * j <= i; j++) {
  124. //             if (i % j == 0) {
  125. //                 isPrime = false;
  126. //                 break;
  127. //             }
  128. //         }
  129. //         if (isPrime) {
  130. //             primes.push_back(i);
  131. //         }
  132. //     }
  133.  
  134. //     int primeCount = primes.size();
  135. //     map<long long int, int> bitmaskFrequency;
  136. //     string bitmask(primeCount, '0');
  137. //     bitmaskFrequency[0]++;
  138.  
  139. //     long long int goodSubarrays = 0;
  140.  
  141. //     for (int i = 0; i < n; i++) {
  142. //         for (int j = 0; j < primeCount; j++) {
  143. //             int exponent = 0;
  144. //             while (arr[i] % primes[j] == 0) {
  145. //                 exponent++;
  146. //                 arr[i] /= primes[j];
  147. //             }
  148.  
  149. //             if (bitmask[j] == '1') exponent++;
  150. //             bitmask[j] = (exponent % 2) + '0';
  151. //         }
  152.  
  153. //         long long int bitmaskValue = 0;
  154. //         for (auto& bit : bitmask) {
  155. //             bitmaskValue *= 2;
  156. //             if (bit == '1') bitmaskValue++;
  157. //         }
  158.  
  159. //         goodSubarrays += bitmaskFrequency[bitmaskValue];
  160. //         bitmaskFrequency[bitmaskValue]++;
  161. //     }
  162.  
  163. //     return goodSubarrays;
  164. // }
  165.  
  166. // int main() {
  167. //     int n;
  168. //     cin >> n;
  169. //     vector<int> arr(n);
  170. //     for (auto& x : arr) cin >> x;
  171.  
  172. //     cout << countGoodSubarrays(n, arr);
  173. //     return 0;
  174. // }
  175.  
  176.  
  177. // ttl cache:
  178. #include <bits/stdc++.h>
  179. using namespace std;
  180.  
  181. vector<int> processEventIntervals(vector<vector<int>> &data, vector<int> &queries){
  182.     map<int, int> m;
  183.     for(auto &x : data){
  184.         m[x[0]]++;
  185.         m[x[1]+x[0]+1]--;
  186.     }
  187.     int curr=0;
  188.     vector<int> keys;
  189.     for(auto &x : m){
  190.         keys.push_back(x.first);
  191.         curr+=x.second;
  192.         m[x.first]=curr;
  193.     }
  194.     vector<int> ans;
  195.     for(auto &x : queries){
  196.         int lo=0 , hi=keys.size()-1 , mid;
  197.         while(lo<=hi)
  198.         {
  199.             mid=(lo+hi)/2;
  200.             if(keys[mid]<=x)
  201.             {
  202.                 lo=mid+1;
  203.             }
  204.             else
  205.             {
  206.                 hi=mid-1;
  207.             }
  208.         }  
  209.         if(hi==-1){
  210.             ans.push_back(0);
  211.         }else{
  212.             ans.push_back(m[keys[hi]]);
  213.         }
  214.     }
  215.    
  216.     // cout<<m[keys.back()-2];
  217.     return ans;
  218. }
  219. int main(){
  220.     int n;
  221.     cin>>n;
  222.     vector<vector<int>> data(n, vector<int>(2));
  223.     for(auto &x : data){
  224.         cin>>x[0]>>x[1];
  225.     }
  226.     int m;
  227.     cin>>m;
  228.     vector<int> q(m);
  229.     for(auto &x : q)cin>>x;
  230.     auto ans =processEventIntervals(data, q);
  231.     for(auto &x : ans)cout<<x<<" ";
  232.     return 0;
  233. }
  234.  
  235.  
  236. // Women in tech
  237. // C++ program for the above approach
  238.  
  239. #include <bits/stdc++.h>
  240. using namespace std;
  241.  
  242. // Function to count minimum number
  243. // of bits required to be flipped
  244. // to make all array elements equal
  245. int makeEqual(int* arr, int n)
  246. {
  247.     // Stores the count of unset bits
  248.     int fre0[33] = { 0 };
  249.  
  250.     // Stores the count of set bits
  251.     int fre1[33] = { 0 };
  252.  
  253.     // Traverse the array
  254.     for (int i = 0; i < n; i++) {
  255.  
  256.         int x = arr[i];
  257.  
  258.         // Traverse the bit of arr[i]
  259.         for (int j = 0; j < 33; j++) {
  260.  
  261.             // If current bit is set
  262.             if (x & 1) {
  263.  
  264.                 // Increment fre1[j]
  265.                 fre1[j] += 1;
  266.             }
  267.  
  268.             // Otherwise
  269.             else {
  270.  
  271.                 // Increment fre0[j]
  272.                 fre0[j] += 1;
  273.             }
  274.  
  275.             // Right shift x by 1
  276.             x = x >> 1;
  277.         }
  278.     }
  279.  
  280.     // Stores the count of total moves
  281.     int ans = 0;
  282.  
  283.     // Traverse the range [0, 32]
  284.     for (int i = 0; i < 33; i++) {
  285.  
  286.         // Update the value of ans
  287.         ans += min(fre0[i], fre1[i]);
  288.     }
  289.  
  290.     // Return the minimum number of
  291.     // flips required
  292.     return ans;
  293. }
  294.  
  295. // Driver Code
  296. int main()
  297. {
  298.     int arr[] = { 3, 5 };
  299.     int N = sizeof(arr) / sizeof(arr[0]);
  300.     cout << makeEqual(arr, N);
  301.  
  302.     return 0;
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement