Flipping Range
You are given an array
You can make the following operation on
- Select some
x such thatx appears inB . - Select an interval from array
a of sizex and multiply by−1 every element in the interval. Formally, selectl andr such that1≤l≤r≤n andr−l+1=x , then assignai:=−ai for everyi such thatl≤i≤r .
Consider the following example, let
[0,6,−2,−1,4,5] is obtained after choosing size2 andl=4 ,r=5 .[0,6,2,−1,4,5] is obtained after choosing size1 andl=3 ,r=3 .
Find the maximum
The input consists of multiple test cases. The first line contains a single integer
The first line of each test case contains two integers
The second line of each test case contains
The third line of each test case contains
It's guaranteed that the sum of
For each test case print a single integer — the maximum possible sum of all
In the first test, you can apply the operation
In the second test, you can apply the operation
Solution:
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- #define OJ \
- freopen("input.txt", "r", stdin); \
- freopen("output.txt", "w", stdout);
- ll bp(ll a,ll b,ll mod=LONG_LONG_MAX){
- a%=mod;
- ll res=1;
- while(b>0){
- if(b&1) res=res*a%mod;
- a=a*a%mod;
- b/=2;
- }
- return res;
- }
- int c(int x,int n){
- return x^(n-1);
- }
- void solve(){
- int n,k;
- cin>>n>>k;
- vector<int> a(n/2),b(n/2);
- if(k==0){
- for(int i=0;i<n/2;i++){
- a[i]=i,b[i]=c(i,n);
- }
- }
- else if(k>0 && k<n-1){
- // k & n-1 0 & c(k)
- for(int i=0;i<n/2;i++){
- if(i==0) a[i]=0,b[i]=c(k,n);
- else if(i==k || i==c(k,n)) a[i]=k,b[i]=n-1;
- else a[i]=i,b[i]=c(i,n);
- }
- }
- else if(n==4){
- cout<<-1<<"\n";
- return;
- }
- else {
- a[0]=n-1,b[0]=n-2;
- a[1]=n-3,b[1]=1;
- a[2]=0,b[2]=2;
- for(int i=3;i<n/2;i++){
- a[i]=i,b[i]=c(i,n);
- }
- }
- for(int i=0;i<n/2;i++) cout<<a[i]<<" "<<b[i]<<"\n";
- }
- int main(){
- cin.tie(0);ios_base::sync_with_stdio(0);
- int ttt=1;
- cin>>ttt;
- while(ttt--){
- solve();
- }
- }
No comments:
Post a Comment