檐之 level
获赞
26
粉丝
5
关注
0
看过 TA
0
北京邮电大学
2020
Java
IP属地:未知
暂未填写个人简介
私信
关注
2019-08-18 09:53
已编辑
北京邮电大学 Java
求第二题思路 1. 基本leetcode 394原题,没什么好说的 2. 求思路!!! 3. 贪心,按照起始点排序以后,每次遍历起点小于等于当前区间终点,取终点最大的那个。边界条件一个是中间发生不能覆盖的情况,一个是遍历完最后小于所求区间 package tencent.c0817.c; import java.util.Arrays; import java.util.Scanner; public class Main {     public static void main(String[] args) {         Scanner scanne...
(*^__^*)……:第二题ac代码: #include <bits/stdc++.h> using namespace std; long long f[30], e[30]; int a[1050000]; void divide(int l, int r,int x); int main(){     int n, m;     scanf("%d", &n);     m = pow(2,n);          for (int i=0; i<m; ++i){         scanf("%d", &a[i]);     }          divide(0,m,n);          int t, p;     long long ans;     scanf("%d", &t);     while (t--){         scanf("%d", &p);         for (int i=1; i<=p; ++i)             f[i] = pow(2,i+n-2) - e[i] - f[i];         ans = 0;         for (int i=1; i<=n; ++i){             ans += f[i];         }         cout << ans << endl;     }      } void divide(int l, int r,int x){     if (x==0){         return;     }           int mid = (l+r) / 2;     divide(l,mid,x-1);     divide(mid,r,x-1);          int nowp = mid;     for (int i=l; i<mid; i++){         while (nowp < r && a[nowp] < a[i]) nowp++;         f[x] += (nowp-mid);     }          int i=l, j=mid, ri, rj;     while (i<mid && j<r){         if (a[i] == a[j]){             ri = rj = 1;             while (ri+i < mid && a[ri+i] == a[i]) ri++;             while (rj+j < r && a[rj+j] == a[j]) rj++;             e[x] += ri * rj;             i += ri;             j += rj;             continue;         }         if (a[i] > a[j]) j++;         else i++;     }          vector<int> v;     i=l;      j=mid;     while (i<mid || j<r){         if (i==mid) {             v.push_back(a[j++]);             continue;         }         if (j==r) {             v.push_back(a[i++]);             continue;         }         if (a[i] < a[j])             v.push_back(a[i++]);         else             v.push_back(a[j++]);     }     for (int i=l; i<r; ++i)         a[i] = v[i-l];          }
投递腾讯等公司10个岗位 >
0 点赞 评论 收藏
分享
关注他的用户也关注了:
牛客网
牛客企业服务