倍增 奇怪的tle

大概是因为用的数组的原因和其他加减法,导致的超时

题目 acwing 天才ACM

 

 1 #include<bits/stdc++.h>
 2 
 3 const int MAXN = 5e5 + 10;
 4 using namespace std;
 5 typedef long long LL;
 6 
 7 int TT;
 8 int n, m;
 9 LL t;
10 LL a[MAXN];
11 int poi, p;
12 int ans;
13 LL b[MAXN];
14 LL sor[MAXN];
15 LL c[MAXN];
16 
17 LL check(int l, int r, int len)
18 {
19     LL res = 0;
20     int top = 0;
21     for(int i = r + 1;i <= r + len;i++) c[i] = a[i];
22     sort(c + r + 1, c + r + len + 1);
23     int ll = l, rr = r + 1;
24     while(ll <= r && rr <= r + len)
25     {
26         if(b[ll] <= c[rr]) sor[++top] = b[ll++];
27         else sor[++top] = c[rr++];
28     }
29     while(ll <= r) sor[++top] = b[ll++];
30     while(rr <= r + len) sor[++top] = c[rr++];
31 
32     for(int i = 1, j = top;i <= m && i < j;i++,j--)
33         res += (sor[j] - sor[i]) * (sor[j] - sor[i]);
34     return res;
35 }
36 
37 int main()
38 {
39     ios::sync_with_stdio(false);
40     cin.tie(0);
41     
42     cin >> TT;
43     while(TT--)
44     {
45         cin >> n >> m >> t;
46         for(int i = 1;i <= n;i++) cin >> a[i];
47         poi = 1, p = 1;
48         ans = 0;
49         int end;
50         b[1] = a[1];
51         while(poi <= n)
52         {
53             p = 1;
54             end = poi;
55             int nex = 0;
56             while(p)
57             {
58                 if(end + p <= n && check(poi, end, p) <= t){
59                     end += p, p <<= 1;
60                     for(int i = poi;i <= end;i++) b[i] = sor[i-poi+1];    
61                 }
62                 else p >>= 1;
63             }
64             poi = end + 1;
65             ans++;
66         }
67 //        memset(sor, 0, sizeof sor);
68 //        memset(b, 0, sizeof b);
69         cout << ans << '\n';
70     }
71     return 0;
72 }
73 //为什么不对呢,因为对于 1 2 4 5来说,1 和 2 恰好符合,但是 4 和 5是不符合的,如果用在 c
74 //数组中,那么由于 check 返回 0,那么是不会更新 b 数组,当 poi 指向 4 的时候,此时 b 数组对应为 0 
错误代码
 1 #include<bits/stdc++.h>
 2 
 3 const int MAXN = 5e5 + 10;
 4 using namespace std;
 5 typedef long long LL;
 6 
 7 int TT;
 8 int n, m;
 9 LL t;
10 LL a[MAXN];
11 int poi, p;
12 int ans;
13 LL b[MAXN];
14 LL sor[MAXN];
15 LL c[MAXN];
16 
17 LL check(int l, int r, int len)
18 {
19     LL res = 0;
20     int top = 0;
21     for(int i = r + 1;i <= r + len;i++) b[i] = a[i];
22     sort(b + r + 1, b + r + len + 1);
23     int ll = l, rr = r + 1;
24     while(ll <= r && rr <= r + len)
25     {
26         if(b[ll] <= b[rr]) sor[++top] = b[ll++];
27         else sor[++top] = b[rr++];
28     }
29     while(ll <= r) sor[++top] = b[ll++];
30     while(rr <= r + len) sor[++top] = b[rr++];
31 
32     for(int i = 1, j = top;i <= m && i < j;i++,j--)
33         res += (sor[j] - sor[i]) * (sor[j] - sor[i]);
34     return res;
35 }
36 
37 int main()
38 {
39     ios::sync_with_stdio(false);
40     cin.tie(0);
41     
42     cin >> TT;
43     while(TT--)
44     {
45         cin >> n >> m >> t;
46         for(int i = 1;i <= n;i++) cin >> a[i];
47         poi = 1, p = 1;
48         ans = 0;
49         int end;
50         b[1] = a[1];
51         while(poi <= n)
52         {
53             p = 1;
54             end = poi;
55             int nex = 0;
56             while(p)
57             {
58                 if(end + p <= n && check(poi, end, p) <= t){
59                     end += p, p <<= 1;
60                     for(int i = poi;i <= end;i++) b[i] = sor[i-poi+1];    
61                 }
62                 else p >>= 1;
63             }
64             poi = end + 1;
65             ans++;
66         }
67 //        memset(sor, 0, sizeof sor);
68 //        memset(b, 0, sizeof b);
69         cout << ans << '\n';
70     }
71     return 0;
72 }
正确代码

 

posted @ 2024-03-05 21:59  是谁不可理喻  阅读(12)  评论(0)    收藏  举报