You're given an array 𝑎 initially containing 𝑛 integers. In one operation, you must do…
- User
You're given an array 𝑎 initially containing 𝑛 integers. In one operation, you must do the following:
Choose a position 𝑖 such that 1<𝑖≤|𝑎| and 𝑎𝑖=|𝑎|+1−𝑖, where |𝑎| is the current size of the array. Append 𝑖−1 zeros onto the end of 𝑎. After performing this operation as many times as you want, what is the maximum possible length of the array 𝑎?
Input Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤1000). The description of the test cases follows.
The first line of each test case contains 𝑛 (1≤𝑛≤3⋅105) — the length of the array 𝑎.
The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤1012).
It is guaranteed that the sum of 𝑛 over all test cases does not exceed 3⋅105.
Output For each test case, output a single integer — the maximum possible length of 𝑎 after performing some sequence of operations.
Example inputCopy 4 5 2 4 6 2 5 5 5 4 4 5 1 4 6 8 2 3 1 1 outputCopy 10 11 10 1 Note In the first test case, we can first choose 𝑖=4, since 𝑎4=5+1−4=2. After this, the array becomes [2,4,6,2,5,0,0,0]. We can then choose 𝑖=3 since 𝑎3=8+1−3=6. After this, the array becomes [2,4,6,2,5,0,0,0,0,0], which has a length of 10. It can be shown that no sequence of operations will make the final array longer.
In the second test case, we can choose 𝑖=2, then 𝑖=3, then 𝑖=4. The final array will be [5,4,4,5,1,0,0,0,0,0,0], with a length of 11. The following solution gives time limit exceeded please fix it #include #include #include #include
#define ll long long
using namespace std;
void solve() { int n; cin >> n;
// maintain a map of value to set of indices map<int, set> mp; for (int i = 0; i < n; i++) { int x; cin >> x; mp[x].insert(i); }
ll best = n; // backtracking auto dfs = [&] (auto &&dfs, ll size, ll& best) { if (mp.empty()) return; for (int i = 0; i < n; i++) { if (mp[size - i].count(i)) { mp[size - i].erase(i); if (mp[size - i].empty()) mp.erase(size - i); best = max(best, size + i);
codedfs(dfs, size + i, best); mp[size - i].insert(i); } }};
dfs(dfs, n, best); std::cout << best << endl; }
int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) solve(); }
- Assistant