4479. 最长子序列
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
void solve()
{
cin>>n>>m;
vector<int>v1,v2;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
v1.pb(x);
}
for(int i=0;i<m;i++)
{
int x;
cin>>x;
v2.pb(x);
}
unordered_map<int,int>mp;
for(auto x:v2)mp[x]++;
for(auto x:v1)
if(mp.count(x))cout<<x<<' ';
cout<<endl;
}
signed main()
{
io;
solve();
return 0;
}
4480. 倒垃圾
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=2e5+10;
int one[N],zero[N],a[N];
int res[N];
void solve()
{
cin>>n>>m;
for(int i=0;i<n+m;i++)cin>>a[i];
int cnt1=0,cnt2=0;
for(int i=0;i<n+m;i++)
{
int x;
cin>>x;
if(x==1)one[++cnt1]=a[i];
else zero[++cnt2]=a[i];
}
one[0]=-ll_INF,one[cnt1+1]=ll_INF;
for(int i=1;i<=cnt2;i++)
{
int l=0,r=cnt1+1;
while(l<r)
{
int mid=l+r>>1;
if(one[mid]>=zero[i])r=mid;
else l=mid+1;
}
if(zero[i]-one[r-1]<=one[r]-zero[i])res[r-1]++;
else res[r]++;
}
for(int i=1;i<=m;i++)cout<<res[i]<<' ';
cout<<endl;
}
signed main()
{
io;
solve();
return 0;
}
4481. 方格探索
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int dx[4]={-1, 0, 1, 0};
int dy[4]={0, 1, 0, -1};
int n,m;
int r,c;
int x,y;
const int N=2010;
char g[N][N];
int dist[N][N];
bool st[N][N];
void bfs()
{
memset(dist,0x3f,sizeof dist);
dist[r][c]=0;
deque<PII>q;
q.push_back({r,c});
while(q.size())
{
auto t=q.front();
q.pop_front();
if(st[t.x][t.y])continue;
st[t.x][t.y]=true;
for(int i=0;i<4;i++)
{
int x=dx[i]+t.x,y=dy[i]+t.y;
if(x<=0||x>n||y<=0||y>m)continue;
if(g[x][y]=='*')continue;
int w=0;
if(i==1)w=1;
if(dist[x][y]>dist[t.x][t.y]+w)
{
dist[x][y]=dist[t.x][t.y]+w;
if(w)q.push_back({x,y});
else q.push_front({x,y});
}
}
}
}
void solve()
{
cin>>n>>m>>r>>c>>x>>y;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>g[i][j];
bfs();
int res=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int b=dist[i][j];
if(b<=y&&b-(j-c)<=x)res++;
}
}
cout<<res<<endl;
}
signed main()
{
io;
solve();
return 0;
}