前言
A. Diverse Substring
题意
做法
坑点
代码
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
#include<bitset>
#include<stack>
#include<set>
#include<vector>
#include <time.h>
#include<string.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
const int maxn = 2e5+5;
const int Mod=1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e=exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;
#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
char str[maxn];
int a[maxn];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int len;
scanf("%d%s",&len,str);
for(int i=0;i<len-1;i++)
{
if(str[i]>str[i+1])
{
for(int j=0;j<i;j++) printf("%c",str[j]);
for(int j=i+1;j<len;j++) printf("%c",str[j]);
return 0;
}
}
for(int i=0;i<len-1;i++)
{
printf("%c",str[i]);
}
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
B. Divisor Subtraction
题意
做法
坑点
代码
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
#include<bitset>
#include<stack>
#include<set>
#include<vector>
#include <time.h>
#include<string.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
const int maxn = 2e5+5;
const int Mod=1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e=exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;
#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define maxn 100005
int main()
{
ll n;
ll ans=0;
scanf("%lld",&n);
if(n%2==0)
{
printf("%lld\n",n/2);
return 0;
}
for(ll i=3;i*i<=n;i++)
{
if(n%i==0)
{
printf("%lld\n",(n-i)/2+1);
return 0;
}
}
printf("1\n");//n为质数的情况
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
C. Meme Problem
题意
做法
代码
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
#include<bitset>
#include<stack>
#include<set>
#include<vector>
#include <time.h>
#include<string.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
const int maxn = 2e5+5;
const int Mod=1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e=exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;
#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define maxn 100005
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
double x_1,x_2,d;
scanf("%lf",&d);
if(d*d-4*d<0)
{
printf("N\n");
continue;
}
printf("Y\n");
x_1=-0.5*(-d+sqrt(d*d-4*d));
x_2=-0.5*(-d-sqrt(d*d-4*d));
printf("%.10f %.10f\n",x_1,x_2);
}
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
D. Edge Deletion
题意
做法
坑点
代码
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
#include<bitset>
#include<stack>
#include<set>
#include<vector>
#include <time.h>
#include<string.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
const int maxn = 3e5+5;
const int Mod=1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e=exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;
#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
typedef long long ll;
#define maxm 600005
#define maxn 300005
#define inf 0x3f3f3f3f3f3f3f3f
struct P
{
int to;
ll cost;
bool operator < (const P & a) const
{
return cost>a.cost;
}
};
struct node
{
int to;
ll val;
int nxt;
int id;
int s;
}edge[maxm];
int head[maxn],tot;//head和tot记得重置,head重置为-1
int n,m;//点数,边数,不要再main里面重新定义
bool vis[maxn];//每次在dij里面初始化为0
ll dis[maxn];//根据题意初始化为inf可能int可能longlong
void addedge(int x,int y,ll val,int id)
{
edge[tot].to=y;
edge[tot].val=val;
edge[tot].nxt=head[x];
edge[tot].id=id;
head[x]=tot++;
}
int pre[maxn],prem[maxn];
int vv[maxn];
void Dijkstra(int s)
{
memset(vis,0,sizeof(vis));
fill(dis,dis+n+2,inf);
dis[s]=0;
priority_queue<P>q;
q.push(P{s,0});
while(!q.empty())
{
P p1=q.top();q.pop();
int u=p1.to;
if(vis[u])continue;
vis[u]=1;
for(int i=head[u];i+1;i=edge[i].nxt)
{
int v=edge[i].to;
if(vis[v])continue;
if(dis[v]>dis[u]+edge[i].val)
{
pre[v]=u;
prem[v]=edge[i].id;
dis[v]=dis[u]+edge[i].val;
q.push(P{v,dis[v]});
}
}
}
}
vector<pii> G[maxn];
vector<int> ans;
int ind[maxn];
int cc,k;
void dfs(int st)
{
if(cc==k) return ;
for(int i=0;i<G[st].size();i++)
{
cc++;
ans.push_back(G[st][i].Se);
if(cc==k) return;
dfs(G[st][i].Fi);
if(cc==k) return ;
}
}
int main()
{
int u,v;
ll w;
memset(head,-1,sizeof(head));
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=m;i++)
{
scanf("%d%d%lld",&u,&v,&w);
addedge(u,v,w,i);
addedge(v,u,w,i);
}
Dijkstra(1);
if(k>=n-1)
{
printf("%d\n",n-1);
for(int i=2;i<=n;i++)
{
printf("%d ",prem[i]);
}
}
else
{
int re=n-1;
for(int i=2;i<=n;i++)
{
G[pre[i]].push_back(pii(i,prem[i]));
}
cc=0;
dfs(1);
printf("%d\n",k);
for(int i=0;i<ans.size();i++)
{
printf("%d ",ans[i]);
}
}
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
E. Vasya and a Tree
题意
做法
代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn = 3e5+10;
int n,m;
ll tree[maxn],ans[maxn];
vector<int> G[maxn],D[maxn],X[maxn];
void add(int x,int val)
{
while(x<=n)
{
tree[x]+=val;
x=x+(x&-x);
}
}
ll sum(int x)
{
ll ans=0;
while(x)
{
ans+=tree[x];
x=x-(x&-x);
}
return ans;
}
void dfs(int x,int fa,int dep)
{
for(int i=0;i<D[x].size();i++)
{
add(min(D[x][i]+dep,n),X[x][i]);//进子树之前更新
}
ans[x]=sum(n)-sum(dep-1);//树状数组变区间查询为两个前缀和相减
//由于性质2,所以在这个地方就可以直接算出当前节点的最终答案
for(int i=0;i<G[x].size();i++)
{
if(G[x][i]==fa) continue;
dfs(G[x][i],x,dep+1);
}
for(int i=0;i<D[x].size();i++)
{
add(min(D[x][i]+dep,n),-X[x][i]);//出子树之后还原
}
}
int main()
{
int x,y,z;
scanf("%d",&n);
for(int i=1;i<=n-1;i++)
{
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
D[x].push_back(y);
X[x].push_back(z);
}
dfs(1,0,1);
for(int i=1;i<=n;i++)
{
printf("%lld ",ans[i]);
}
return 0;
}