题目链接
2018-2019 ACM-ICPC, NEERC, Southern Subregional Contest, Qualification Stage
我做了4个队友做了5个感觉一堆模拟、、
ps vjude 体验巨差,总是提交失败
贴一下代码、
A pre数组优化,emmm我也不知道叫啥的优化,emmm不太清楚直接写会不会t
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+7;
int n,m,d,cnt;
int pre[maxn],ans[maxn];
pair<int,int> a[maxn];
int main(){
scanf("%d%d%d",&n,&m,&d);
for(int i=0;i<n;i++)scanf("%d",&a[i].first),a[i].second = i;
sort(a,a+n);
int last = 0;
pre[0] = -1;
for(int i=1;i<n;i++)
pre[i] = (a[i].first - a[last].first>d)?last++:last-1;
for(int i=n-1;~i;i--)if(a[i].first){
cnt++,a[i].first = 0;
for(int j=pre[i];~j;j=pre[j])
a[j].first = 0,ans[a[j].second] = cnt;
ans[a[i].second] = cnt;
}
printf("%d\n",cnt);
for(int i=0;i<n;i++)printf("%d%c",ans[i],i==n-1?'\n':' ');
return 0;
}
B 前缀和+二分
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+7;
int n,h;
ll sum[maxn],ans;
pair<int,int> a[maxn];
int main(){
scanf("%d%d",&n,&h);
for(int i=1;i<=n;i++)scanf("%d%d",&a[i].first,&a[i].second);
//sort(a+1,a+1+n);
for(int i=2;i<=n;i++)sum[i] = sum[i-1]+a[i].first-a[i-1].second;
for(int i=1;i<n;i++){
int l = i,r = n,mid,sul = -1;
while(l<=r){
mid = (l+r)/2;
if(sum[mid]-sum[i]<=h)
l = mid+1,sul = mid;
else
r = mid-1;
}
if(sum[sul]-sum[i] == h)
ans = max(ans,1ll*a[sul].first-a[i].first);
else{
//printf("sul : %d\n",sul);
ll t = 1ll*a[sul].second-a[i].first + h-(sum[sul]-sum[i]);
ans = max(ans,t);
//cout<<"h :"<<h<<" "<<(sum[sul]-sum[i])<<endl;
}
//printf("%lld\n",ans);
}
if(n == 1)ans = a[1].second-a[1].first + h;
printf("%lld\n",ans);
return 0;
}
F 树状数组优化,其实只有一点,把1e7优化到1e6,感觉1e7应该也能过
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6+7;
int n,m,d,cnt;
int Fun(int x,int ans = 0){
for(int i=0;i<6;i++,x/=10)
ans += i<3?x%10:-(x%10);
return abs(ans);
}
int tree[30],ans[maxn],a[maxn];
void add(int pos,int x){
while(pos<30)
tree[pos] += x,pos += pos&-pos;
}
int query(int pos,int ans = 0){
while(pos)ans += tree[pos],pos -= pos&-pos;
return ans;
}
int main(){
for(int i=0;i<1e6;i++)
a[i] = Fun(i);
for(int i=0;i<1e6;i++)
add(a[i]+1,1),ans[i] = query(a[i]);
int n,x;
scanf("%d",&n);
while(n--){
scanf("%d",&x);
printf("%d\n",ans[x]);
}
return 0;
}
H 氵题
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int n,m;
pair<int,int> l,r;
cin>>n>>m;
cin>>l.first>>l.second>>r.first>>r.second;
ll ans = 0;
if(m&1) ans += (l.first-1) + (n - r.first);
if((l.second-1)&1)ans += r.first - l.first + 1;
if((m-r.second)&1)ans += r.first - l.first + 1;
cout<<(ans+1)/2<<endl;
return 0;
}