A
保证乘积不为 (0),所以要将所有 (0) 加 (1),然后看看变换后的数列和是否为 (0),如果是再加一个 (1) 即可。
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '
'
#define debug(x) cerr << #x << ": " << x << endl
#define pb push_back
#define eb emplace_back
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define ceil(a,b) (a+(b-1))/(b)
#define all(x) (x).begin(), (x).end()
#define SUM(a) accumulate(all(a), 0LL)
#define MIN(a) (*min_element(all(a)))
#define MAX(a) (*max_element(all(a)))
#define lb(a, x) distance(begin(a), lower_bound(all(a), (x)))
#define ub(a, x) distance(begin(a), upper_bound(all(a), (x)))
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
using pii = pair<int, int>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vpii = vector<pii>;
using ll = long long;
using ull = unsigned long long;
inline void read(int &x) {
int s=0;x=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
x*=s;
}
const int N=110;
int w[N];
int main(){
int T; cin>>T;
while(T--){
int n; cin>>n;
int res=0, s=0;
rep(i,1,n){
read(w[i]);
if(!w[i]) res++, w[i]++;
s+=w[i];
}
if(!s) res++;
cout<<res<<endl;
}
return 0;
}
B
将数组排序后,取中间两个数的差即可。
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '
'
#define debug(x) cerr << #x << ": " << x << endl
#define pb push_back
#define eb emplace_back
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define ceil(a,b) (a+(b-1))/(b)
#define all(x) (x).begin(), (x).end()
#define SUM(a) accumulate(all(a), 0LL)
#define MIN(a) (*min_element(all(a)))
#define MAX(a) (*max_element(all(a)))
#define lb(a, x) distance(begin(a), lower_bound(all(a), (x)))
#define ub(a, x) distance(begin(a), upper_bound(all(a), (x)))
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
using pii = pair<int, int>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vpii = vector<pii>;
using ll = long long;
using ull = unsigned long long;
inline void read(int &x) {
int s=0;x=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
x*=s;
}
int main(){
int T; cin>>T;
while(T--){
int n; cin>>n;
vi w;
rep(i,1,2*n){
int v; read(v);
w.pb(v);
}
sort(all(w));
cout<<w[n]-w[n-1]<<endl;
}
return 0;
}
C
注意到当我们决定了一个数为开头时,剩下的数怎么摆都是同样的结果,所以我们枚举一下作为开头的数并统计答案。
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '
'
#define debug(x) cerr << #x << ": " << x << endl
#define pb push_back
#define eb emplace_back
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define ceil(a,b) (a+(b-1))/(b)
#define all(x) (x).begin(), (x).end()
#define SUM(a) accumulate(all(a), 0LL)
#define MIN(a) (*min_element(all(a)))
#define MAX(a) (*max_element(all(a)))
#define lb(a, x) distance(begin(a), lower_bound(all(a), (x)))
#define ub(a, x) distance(begin(a), upper_bound(all(a), (x)))
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
using pii = pair<int, int>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vpii = vector<pii>;
using ll = long long;
using ull = unsigned long long;
inline void read(int &x) {
int s=0;x=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
x*=s;
}
const int N=1e5+5;
int w[N];
int p[N], q[N];
int main(){
int n; cin>>n;
rep(i,1,n) read(w[i]), p[i]=p[i-1]|w[i];
dwn(i,n,1) q[i]=q[i+1]|w[i];
int pos=0, res=0;
rep(i,1,n){
int t=p[i-1]|q[i+1];
int val=(w[i]|t)-t;
if(val>res){
res=val;
pos=i;
}
}
if(pos) cout<<w[pos]<<' ';
rep(i,1,n) if(i!=pos) cout<<w[i]<<' ';
cout<<endl;
return 0;
}
D
题目可以转化为判断这个凸多边形是不是中心对称,但这个转化却不好想 qwq。
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '
'
#define debug(x) cerr << #x << ": " << x << endl
#define pb push_back
#define eb emplace_back
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define ceil(a,b) (a+(b-1))/(b)
#define all(x) (x).begin(), (x).end()
#define SUM(a) accumulate(all(a), 0LL)
#define MIN(a) (*min_element(all(a)))
#define MAX(a) (*max_element(all(a)))
#define lb(a, x) distance(begin(a), lower_bound(all(a), (x)))
#define ub(a, x) distance(begin(a), upper_bound(all(a), (x)))
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
using pii = pair<int, int>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vpii = vector<pii>;
using ll = long long;
using ull = unsigned long long;
inline void read(int &x) {
int s=0;x=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
x*=s;
}
#define x first
#define y second
const int N=1e5+5;
pii q[N];
int main(){
ios::sync_with_stdio(false);
int n; cin>>n;
rep(i,1,n){
int x, y; cin>>x>>y;
q[i]={2*x, 2*y};
}
if(n&1) cout<<"NO"<<endl;
else{
set<pii> st;
rep(i,1,n/2){
int x=q[i].x+q[i+n/2].x>>1, y=q[i].y+q[i+n/2].y>>1;
st.insert({x, y});
}
if(st.size()==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
E
单调栈。
注意到我们应该使靠前的数尽量小,所以考虑维护一个栈,栈中元素有属性:左右端点,区间和。
我们将于位置 (i) 读入的值 (v) 当作是一个新的元素:{i, i, v}
,然后让这个元素和前面的元素合并,合并的规则是:只要栈顶元素和栈顶下面一个元素共同构成的区间的平均值比栈顶下面一个元素小,就合并。
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '
'
#define debug(x) cerr << #x << ": " << x << endl
#define pb push_back
#define eb emplace_back
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define ceil(a,b) (a+(b-1))/(b)
#define all(x) (x).begin(), (x).end()
#define SUM(a) accumulate(all(a), 0LL)
#define MIN(a) (*min_element(all(a)))
#define MAX(a) (*max_element(all(a)))
#define lb(a, x) distance(begin(a), lower_bound(all(a), (x)))
#define ub(a, x) distance(begin(a), upper_bound(all(a), (x)))
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
using pii = pair<int, int>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vpii = vector<pii>;
using ll = long long;
using ull = unsigned long long;
#define int ll
inline void read(int &x) {
int s=0;x=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
x*=s;
}
const int N=1e6+5;
struct Node{
int l, r;
int sum;
};
Node stk[N];
int top;
signed main(){
int n; read(n);
int v; read(v);
stk[++top]={1, 1, v};
rep(i,2,n){
read(v);
stk[++top]={i, i, v};
// merge
while(top>1){
auto x=stk[top-1], y=stk[top];
int lenx=x.r-x.l+1, leny=y.r-y.l+1;
if((x.sum+y.sum)*lenx<x.sum*(lenx+leny)) stk[--top]={x.l, y.r, x.sum+y.sum};
else break;
}
}
rep(i,1,top) rep(j,stk[i].l,stk[i].r){
double t=(double)(stk[i].sum*1.0/(stk[i].r-stk[i].l+1));
printf("%.12lf
", t);
}
return 0;
}