A - One and Two
相当于找第一个位置前后2的个数相同·
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10,mod=998244353;
#define int long long
typedef long long LL;
typedef pair<int, int> PII;
const long long inf=1e17;
int n,m,k;
int a[N],b[N];
int gcd(int a,int b){return b?gcd(b,a%b):a;
}
void solve()
{cin>>n;for(int i=1;i<=n;i++) cin>>a[i];vector<int> s(n+10);int cnt=0;for(int i=1;i<=n;i++){s[i]=s[i-1]+(a[i]==2);cnt+=(a[i]==2);}for(int i=1;i<=n;i++){if(s[i]==cnt-s[i]){cout<<i<<"\n";return ;}}cout<<"-1\n";}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int t=1;cin>>t;while(t--) solve();
}
B - Sum of Two Numbers
每一个数位单独考虑,如果是偶数就分一半,否则交换多1
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10,mod=998244353;
#define int long long
typedef long long LL;
typedef pair<int, int> PII;
const long long inf=1e17;
int n,m,k;
int a[N],b[N];
int gcd(int a,int b){return b?gcd(b,a%b):a;
}
void solve()
{cin>>n;if(n%2==0){cout<<"No\n";return ;}int s=(2*n)*(n*2+1)/2;int st=(s-(n*(n-1))/2)/n;int idx=st-(n+1);vector<bool> v(st,n+10);cout<<"Yes\n";for(int i=1;i<=n;i++){cout<<idx<<" "<<st-idx<<"\n";idx--;st++;if(idx==0) idx=n;}cout<<"\n";}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int t=1;cin>>t;while(t--) solve();
}
C:
首先我们是能计算出第一个数的和
然后就手玩一下就得出这个规律了
比如 n=5
第一个数=9 3 6
第二个数=10 2 8
第三个数=11 1 10
第四个数=12 5 7
第五个数=13 4 9
emmm
我也不知道理由纯猜的
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10,mod=998244353;
#define int long long
typedef long long LL;
typedef pair<int, int> PII;
const long long inf=1e17;
int n,m,k;
int a[N],b[N];
int gcd(int a,int b){return b?gcd(b,a%b):a;
}
void solve()
{cin>>n;if(n%2==0){cout<<"No\n";return ;}int s=(2*n)*(n*2+1)/2;int st=(s-(n*(n-1))/2)/n;int idx=st-(n+1);vector<bool> v(st,n+10);cout<<"Yes\n";for(int i=1;i<=n;i++){cout<<idx<<" "<<st-idx<<"\n";idx--;st++;if(idx==0) idx=n;}cout<<"\n";}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int t=1;cin>>t;while(t--) solve();
}
D - Moving Dots
首先因为坐标不同,不同的两个点的位置肯定是不同的,所以直接枚举两个点即可
然后还要满足这两个点中间没数,和左右满足条件不改变这两个点合并即可
如果两个点中间有位置,那么他们的位置其实是中间的点构成的位置
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10,mod=1e9+7;
#define int long long
typedef long long LL;
typedef pair<int, int> PII;
const long long inf=1e17;
int n,m,k;
int a[N],b[N];
int x[N];
int p[N];
void solve()
{cin>>n;for(int i=1;i<=n;i++) cin>>x[i];p[0]=1;for(int i=1;i<=n;i++) p[i]=p[i-1]*2%mod;int res=0;for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){int d=x[j]-x[i];int l=1,r=n;int ans=0;while(l<r){int mid=l+r+1>>1;if(x[i]-x[mid]>d) l=mid;else r=mid-1;}if(x[i]-x[l]>d) ans+=l;l=1,r=n;while(l<r){int mid=l+r>>1;if(x[mid]-x[j]>=d) r=mid;else l=mid+1;}if(x[l]-x[j]>=d) ans+=n-r+1;res+=p[ans];res%=mod;}}cout<<res<<"\n";
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int t=1;// cin>>t;while(t--) solve();
}
E - Sum Over Zero
很典
不解释了,感觉做了很多次
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10,mod=1e9+7;
#define int long long
typedef long long LL;
typedef pair<int, int> PII;
const long long inf=1e17;
int n,m,k;
class BitTree {public:vector<int> tree;int n;BitTree(int _n) : n(_n) {tree.resize(n+1);fill(tree.begin(),tree.end(),-2e18);}inline int lowbit(int x) { return x&-x; }inline void Modify(int x,int v) {for(;x<=n;x+=lowbit(x)) tree[x]=max(v,tree[x]);}inline int q(int x) {int ret=-2e18;if(x<=0) return 0;for(;x;x-=lowbit(x)) ret=max(ret,tree[x]);return ret;}inline int Query(int l,int r) {return q(r)-q(l-1);}
};
vector<int> nums;
int find(int x){return lower_bound(nums.begin(),nums.end(),x)-nums.begin()+1;
}
void solve()
{cin>>n;vector<int> f(n+10),a(n+10),s(n+10);nums.clear();for(int i=1;i<=n;i++) cin>>a[i];for(int i=1;i<=n;i++){s[i]=s[i-1]+a[i];nums.push_back(s[i]);//cout<<s[i]<<" ";}nums.push_back(0);sort(nums.begin(),nums.end());nums.erase(unique(nums.begin(),nums.end()),nums.end());BitTree tr(N);tr.Modify(find(0),0);for(int i=1;i<=n;i++){f[i]=f[i-1];f[i]=max(f[i],tr.q(find(s[i]))+i);tr.Modify(find(s[i]),f[i]-i);// for(int j=0;j<=i;j++){// if(s[i]-s[j]>=0)// f[i]=max(f[i],f[j]-j+i);// }}cout<<*max_element(f.begin(),f.end());
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int t=1;//cin>>t;while(t--) solve();
}