第 72 场双周赛【5999. 统计数组中好三元组数目:树状数组+等价转换】

 

 

 1 class Solution {
 2 public:
 3     int countPairs(vector<int>& nums, int k) {
 4         int s=0;
 5         for(int i=0;i<nums.size();i++){
 6             for(int j=i+1;j<nums.size();j++){
 7                 if(!((i*j)%k)){
 8                     if(nums[i]==nums[j])
 9                         s++;
10                 }
11             }
12         }
13         return s;
14     }
15 };

 

 

 1 class Solution {
 2 public:
 3     vector<long long> sumOfThree(long long num) {
 4         vector<long long> ans;
 5         if(num%3){
 6             return ans;
 7         }
 8         long long t=num/3;
 9         ans.push_back(num/3-1),ans.push_back(num/3),ans.push_back(num/3+1);
10         return ans;
11     }
12 };

 

 

奇数肯定不行。偶数的话从最小的偶数2开始尝试,如果最后减到相同的话就累计到最后一位即可。

 

 1 #define pb push_back
 2 map<int,int> mp;
 3 class Solution {
 4 public:
 5     vector<long long> maximumEvenSplit(long long finalSum) {
 6         long long num=2;
 7         mp.clear();
 8         vector<long long> ans;
 9         if(finalSum%2) return ans;
10         while(1){
11             if(finalSum>=num){
12                 if(!mp[num]){
13                     ans.pb(num);
14                     mp[num]=1;
15                 }else{
16                     ans[ans.size()-1]+=num;
17                     mp[ans[ans.size()-1]]=1;
18                 }
19                 finalSum-=num;
20                 num+=2;
21                 // cout<<"1: "<<ans[ans.size()-1]<<endl;
22             }else{
23                 if(!finalSum) break;
24                 if(mp[finalSum]) ans[ans.size()-1]+=finalSum;
25                 else ans.pb(finalSum);
26                 // cout<<"2: "<<ans[ans.size()-1]<<endl;
27                 break;
28             }
29         }
30         return ans;
31     }
32 };
33 /*
34 28
35 14   14
36 12   16
37 6 6 8 8
38 
39 4 8 6 10
40 
41 12
42 6 6
43 4 8
44 4 2 6
45 
46 16
47 2 4 6 4
48 */

 

 

等价转换 + 树状数组
https://leetcode-cn.com/problems/count-good-triplets-in-an-array/solution/

 

 1 #define ll long long 
 2 #define lowbit(x) (x&(-x))
 3 #define N 666666
 4 
 5 //******************树状数组
 6 ll tree[N];
 7 void add(int x,int y){
 8     for(int i=x;i<N;i+=lowbit(i)) tree[i]+=y;
 9     return ;
10 }
11 ll ask(int x){
12     ll sum=0;
13     for(int i=x;i;i-=lowbit(i)) sum+=tree[i];
14     return sum;
15 }
16 ll query(int x,int y){
17     return ask(y)-ask(x-1);
18 }
19 map<ll,ll> mp;
20 class Solution {
21 public:
22     long long goodTriplets(vector<int>& nums1, vector<int>& nums2) {
23         mp.clear();
24         memset(tree,0,sizeof(tree));
25         //转化NB
26         for(int i=0;i<nums1.size();i++) mp[nums1[i]]=i;
27         for(int i=0;i<nums2.size();i++){
28             nums2[i]=mp[nums2[i]]+1;
29         }
30         ll ans=0;
31         int len=nums1.size();
32         for(int i=0;i<nums2.size();i++){
33             ll num=nums2[i];
34             ll sum=query(1,num);
35             ans=ans+sum*(len-1-i-(num-sum)+1)*(1ll);
36             add(num,1);
37         }
38         return ans;
39     }
40 };

 

posted @ 2022-02-20 22:36  pengge666  阅读(35)  评论(0编辑  收藏  举报