10.12-10.18题解
Love_Ti_fjx · · 个人记录
::::info[P7502]
link
第一眼看到题目会认为这是一道要用集合的题目,但是观察完数据范围
首先看到此集合有三种操作,
看到有
首先我们考虑到
于是我们开始考虑按照什么因素建 trie 树。
由于通过移项可以发现相同的高位要满足
从而操作
那么查询我们从根节点开始从高位往下查找,当前处理第
因为我们按照
然后把它们累加到答案里去,继续往低位寻找,如果没有子节点了说明没有这一位还在保持相同的数对,那么直接退出。
*tips:由于建树在
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=2e7+10;
int ans,tot;
int tr[N][2],cnt[N][2][2];
void add(int a,int b,int v){
int u=0;
for(int i=62;i>=0;i--){
int a1=(a>>i)&1,b1=(b>>i)&1;
cnt[u][a1][b1]+=v;
int c=a1^b1;
if(!tr[u][c]) tr[u][c]=++tot;
u=tr[u][c];
}
return ;
}
void query(int x,int y){
int u=0;
for(int i=62;i>=0;i--){
int a1=1-((x>>i)&1),b1=(y>>i)&1;
ans+=cnt[u][a1][b1];
int c=((x>>i)&1)^((y>>i)&1);
if(!tr[u][c]) break;
u=tr[u][c];
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int m;
cin>>m;
while(m--){
int op,x,y;
cin>>op>>x>>y;
if(op==1) add(x,y,1);
else if(op==2) add(x,y,-1);
else query(x,y),cout<<ans<<'\n',ans=0;
}
return 0;
}
::::
::::info[P11412]
link
由于上题用了 01trie 还去查看了一眼 trie 板子,从而再来一题练练手。
其实看完题这题的算法框架就已经显现出来了。字符串前缀的判断可以用 hash 和 trie。然后其余变量用一个结构体去维护一下即可。
但是此题的 trie 是一类变种,由于它还要记录其深度才能判断它能不能加,于是我们在
然后最后在主函数内通过前缀的判断,然后遍历对应的记忆,最终累加获得答案。
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=4e5+10;
int n,m;
struct node{int r,v;string s;}c[N];
int tr[N][65],tot;
vector<int> cnt[N];
int get(char c){return c-'a';}
void add(string &s,int id){
int u=0;
for(char c:s){
int v=get(c);
if(!tr[u][v]) tr[u][v]=++tot;
u=tr[u][v];
cnt[u].push_back(id);
}
}
int query(string &s){
int u=0;
for(char c:s){
int v=get(c);
if(!tr[u][v]) return 0;
u=tr[u][v];
}
return u;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>c[i].r>>c[i].v>>c[i].s,add(c[i].s,i);
for(int i=1;i<=m;i++){
int d;
string q;
cin>>d>>q;
int k=query(q),ans=0;
for(auto i:cnt[k]) if(c[i].r<=d) ans+=c[i].v;
cout<<ans<<'\n';
}
return 0;
}
::::
::::info[P6786]
link
首先看到这个数据范围,能够看出这道题
通过一个手玩以及打表,得出了一个结论:当
并不会证明所以跳过了(悲)。
那么我们根据这个结论可以发现当我们将原数列排序去重之后,相邻的两个元素成
于是我们只需要枚举最小值然后用 map 即可做出此题。
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=3e5+10;
int n,ans;
int a[N];
map<int,int> ma;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i],ma[a[i]]++;
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
int w=a[i],sum=0;
while(ma[w]){
sum+=w*ma[w];
ma[w]=0;
if(w%2==0) w=w/2*3;
else break;
}
ans=max(ans,sum);
}
cout<<ans;
return 0;
}
::::
::::info[P13369]
link
超级毒瘤题,给了不超过误差还不给样例,导致一堆锅调不出来/ll。
由于
然后观察整道题的题意,可以发现答案是绝对具有单调性的,于是可以进行一个二分答案。
然后
还有一个最重要的就是输出在
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
int len;
vector<int> a;
bool check(double x,int d){
double w=a[0]-x;
for(int i=1;i<len;i++){
if(w>=a[i]-x-d) w+=d;
else w=a[i]-x;
if(w>a[i]+x) return false;
}
return true;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int T;
cin>>T;
int t=0;
while(T--){
t++;
len=0;
a.clear();
int c,d;
cin>>c>>d;
for(int i=1;i<=c;i++){
int p,v;
cin>>p>>v;
for(int j=1;j<=v;j++) a.push_back(p),len++;
}
sort(a.begin(),a.end());
double l=0,r=1e13,ans=0;
for(int i=1;i<=75;i++){
double mid=(l+r)/2.0;
if(check(mid,d)) ans=mid,r=mid;
else l=mid;
}
cout<<"Case #"<<t<<": ";
cout<<fixed<<setprecision(10)<<ans<<'\n';
}
return 0;
}
::::
::::info[P5963]
link
tips:此题数据范围不清楚,反正开
看完题目可以想到两种思路,一个是贪心,一个是 DP,但是 DP 十分明显的是时间复杂度会超时,那么我们直接往贪心想,如果贪心是假的后面再来考虑 DP 优化。
列举出两组数,有两种情况:
-
-x_i+y_j -
-x_j+y_i
假设第一种成立,那么
于是我们得出了当一组数的和大于另一组的和时,我们将大的选择为减数,小的为加数。
直接排序,前面小的一半当加数,后面大的一半当减数。
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=1e6+10;
int n;
struct node{int a,b,sum;}c[N];
bool cmp(node x,node y){return x.sum<y.sum;}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>c[i].a>>c[i].b,c[i].sum=c[i].a+c[i].b;
sort(c+1,c+n+1,cmp);
int ans=0;
for(int i=1;i<=n/2;i++) ans+=min(c[i].a,c[i].b);
for(int i=n/2+1;i<=n;i++) ans-=max(c[i].a,c[i].b);
cout<<ans;
return 0;
}
::::