牛客小白月赛24 题解
最后还剩一道计算几何,其实可以AK,但没必要///
B:签到
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int a[N];
int main()
{
int t;scanf("%d",&t);
while(t--){
int n,k;scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
sort(a+1,a+n+1);
int l=1;
int ans=1;
for(int i=1;i<=n;i++){
while(l<n&&a[i]-a[l]>k)l++;
ans=max(ans,i-l+1);
}
printf("%d\n",ans);
}
return 0;
}
C:还行
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=505;
char s[N][N];
bool vis[N][N];
int n,m;
bool ok[N][N];
void dfs(int x,int y){
if(x<1||y<1)return ;
if(x>n||y>m)return ;
if(s[x][y]=='#'||ok[x][y])return ;
ok[x][y]=true;
dfs(x-1,y);
dfs(x+1,y);
dfs(x,y+1);
dfs(x,y-1);
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)scanf(" %s",s[i]+1);
dfs(1,1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
if(s[i][j]=='#'){
if(s[i+1][j]=='.')vis[i+1][j]=1;
if(s[i-1][j]=='.')vis[i-1][j]=1;
if(s[i][j+1]=='.')vis[i][j+1]=1;
if(s[i][j-1]=='.')vis[i][j-1]=1;
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(vis[i][j]&&ok[i][j])s[i][j]='*';
for(int i=1;i<=n;i++)printf("%s\n",s[i]+1);
return 0;
}
D:还可 QAQ
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=505;
char s[N][N];
bool vis[N][N];
int n,m;
bool ok[N][N];
void dfs(int x,int y){
if(x<1||y<1)return ;
if(x>n||y>m)return ;
if(s[x][y]=='#'||ok[x][y])return ;
ok[x][y]=true;
dfs(x-1,y);
dfs(x+1,y);
dfs(x,y+1);
dfs(x,y-1);
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)scanf(" %s",s[i]+1);
dfs(1,1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
if(s[i][j]=='#'){
if(s[i+1][j]=='.')vis[i+1][j]=1;
if(s[i-1][j]=='.')vis[i-1][j]=1;
if(s[i][j+1]=='.')vis[i][j+1]=1;
if(s[i][j-1]=='.')vis[i][j-1]=1;
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(vis[i][j]&&ok[i][j])s[i][j]='*';
for(int i=1;i<=n;i++)printf("%s\n",s[i]+1);
return 0;
}
E:还行吧
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int mod=1e5+3;
const int N=1e5+7;
const int M=5e5+10;
int n,m,sta,ed,p[N],head[N],cnt=1;
ll dis[N];
bool vis[N];
bool ok[M<<1];
struct Node{
int to,val,next;
}edge[M<<1];
struct Cmp{
bool operator()(int a,int b){
return dis[a]>dis[b];
}
};
priority_queue<int,vector<int>,Cmp> t;
inline void add(int x,int y,int z)
{
edge[++cnt].to=y;
edge[cnt].val=z;
edge[cnt].next=head[x];
head[x]=cnt;
}
void dijkstra()
{
memset(vis,false,sizeof(false));
memset(dis,0x3f,sizeof(dis));
t.push(sta);
dis[sta]=0;
int x,y,z;
while(!t.empty()){
x=t.top();t.pop();
if(vis[x])continue;
vis[x]=true;
for(int i=head[x];i!=-1;i=edge[i].next){
y=edge[i].to;
if(dis[y]>dis[x]+edge[i].val){
dis[y]=dis[x]+edge[i].val;
t.push(y);
}
}
}
}
void dfs(int u){
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to,w=edge[i].val;
if(dis[v]+w==dis[u]){
ok[i]=ok[i^1]=true;
dfs(v);
}
}
}
bool op[N];
int ans=0;
void dfs1(int u,int f){
if(!op[u]){
op[u]=true;ans++;
}
for(int i=head[u];i!=-1;i=edge[i].next){
if(ok[i])continue;
int v=edge[i].to;
if(v==f||op[v])continue;
dfs1(v,u);
}
}
int main()
{
scanf("%d %d",&n,&m);
sta=1,ed=n;
memset(head,-1,sizeof(head));
int x,y,z;
for(int i=1;i<=m;++i){
scanf("%d %d %d",&x,&y,&z);
if(x==y)continue;
add(x,y,z);add(y,x,z);
}
dijkstra();
dfs(n);
dfs1(1,0);
if(ans==n)puts("YES");
else puts("NO");
return 0;
}
F:签到
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+10;
string s[5]={"elephant","tiger","cat","mouse"};
int main()
{
string s1,s2;
cin>>s1>>s2;
int p1,p2;
for(int i=0;i<4;i++){
if(s1==s[i])p1=i;
if(s2==s[i])p2=i;
}
if((p2+1)%4==p1)puts("tiangou txdy");
else puts("tiangou yiwusuoyou");
return 0;
}
G:为了拼手速,所以完全乱写
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+10;
int a[N];
int main()
{
int n;ll m;
scanf("%d %lld",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
if(m<a[i]){
printf("%d\n",i-1);return 0;
}
m-=a[i];
}
printf("%d\n",n);
return 0;
}
H:还行
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define x first
#define y second
using namespace std;
const int N = 2e6 + 5;
vector<pair<int, int> > a, b;
int t, m, n, p[N], x, y, c, id[N];
int find(int x) {
return x == p[x] ? x : (p[x] = find(p[x]));
}
int getId(int x) {
return lower_bound(id + 1, id + 1 + n, x) - id;
}
void solve() {
for (int i = 1; i <= n; i++) p[i] = i;
for (int i = 0; i < a.size(); i++) {
int x = getId(a[i].x), y = getId(a[i].y);
int fx = find(x), fy = find(y);
p[fx] = fy;
}
for (int i = 0; i < b.size(); i++) {
int x = getId(b[i].x), y = getId(b[i].y);
int fx = find(x), fy = find(y);
if (fx == fy) {printf("NO\n"); return;}
}
printf("YES\n");
}
int main() {
scanf("%d", &t);
while (t--) {
a.clear(); b.clear(); n = 0;
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &x, &y, &c);
if (c == 1) a.push_back(make_pair(x, y));
else b.push_back(make_pair(x, y));
id[++n] = x; id[++n] = y;
}
sort(id + 1, id + 1 + n);
n = unique(id + 1, id + 1 + n) - id - 1;
solve();
}
return 0;
}
I:好像是树剖
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long ll;
const int N=1e6+10;
int first[N],tot,cnt,n,w[N],wt[N];
int d[N],fa[N],siz[N],son[N],top[N],id[N],rk[N],en[N];
ll sum[N<<2];
struct node
{
int v,nex;
}e[N<<1];
void adde(int u,int v)
{
e[tot].v=v;
e[tot].nex=first[u];
first[u]=tot++;
}
void init()
{
memset(first,-1,sizeof(first));
memset(son,0,sizeof(son));
tot=cnt=0;
}
void dfs1(int u,int pre,int depth)
{
d[u]=depth;
fa[u]=pre;
siz[u]=1;
for(int i=first[u];~i;i=e[i].nex)
{
int v=e[i].v;
if(v==pre) continue;
dfs1(v,u,depth+1);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]])
son[u]=v;
}
}
void dfs2(int u,int t)
{
top[u]=t;
en[u]=id[u]=++cnt;
rk[cnt]=u;
wt[cnt]=w[u];
if(!son[u]) return;
dfs2(son[u],t);
for(int i=first[u];~i;i=e[i].nex)
{
int v=e[i].v;
if(v!=fa[u]&&v!=son[u])
dfs2(v,v);
}
en[u]=cnt;
}
void pushup(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=wt[l];
return;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int k,int c,int l,int r,int rt)
{
if(l==r)
{
sum[rt]+=c;
return;
}
int mid=(l+r)>>1;
if(k<=mid) update(k,c,lson);
else update(k,c,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
return sum[rt];
int mid=(l+r)>>1,ans=0;
if(L<=mid) ans+=query(L,R,lson);
if(R>mid) ans+=query(L,R,rson);
return ans;
}
int main()
{
int k,m;
scanf("%d %d %d",&n,&m,&k);
init();
for(int i=1;i<=n;i++)
scanf("%d",&w[i]);
int x,y,z,op;
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
adde(x,y);
adde(y,x);
}
dfs1(k,0,1);
dfs2(k,k);
build(1,n,1);
while(m--)
{
scanf("%d",&op);
if(op==2)
{
scanf("%d",&x);
printf("%d\n",query(id[x],en[x],1,n,1));
}
else if(op==1)
{
scanf("%d%d",&x,&z);
update(id[x],z,1,n,1);
}
}
return 0;
}
J:还行 QAQ
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+10;
const int mod=1e9+7;
ll a[N];
int main()
{
int n;
ll sum=0;
scanf("%d",&n);
ll ans=0;
for(int i=1;i<=n;i++)scanf("%lld",&a[i]),sum+=a[i];
sum%=mod;
for(int i=1;i<=n;i++){
sum=(sum-a[i]+mod)%mod;
ans=(ans+a[i]*a[i]%mod*(n-1)%mod-2*a[i]*(sum)%mod)%mod;
}
ans=(ans+mod)%mod;
printf("%lld\n",ans);
return 0;
}