标签:就是 odi 电信 last gen second int bre else
题面:bzoj
题解:三分+贪心
可以发现一个显然的性质
就是你买外卖的次数和你能维持的天数大概是成一个单峰函数证明不会
于是我们三分峰值
然后找到这个次数后再贪心
首先把那些又贵又放不久的扔掉,可以用单调栈
然后从最便宜的开始往上贪心
code
题面:bzoj
题解:最短路?
可以显然的写出一个dp方程,然而你会发现会有环
我们发现这个方程的形式和spfa的转移形式差不多
所以把所有点扔到队列里,初值设置魔法攻击的代价,跑最短路
如果这个点被更新了,则反图上与它相连的点都有可能更新,入队
codeC
题面:bzoj
题解:线段树
首先将这些值,因为不管怎么样都不会改变他们的大小关系
区间修改,如果最小的值小于下界就直接推平,上界同理
codeF
题面:bzoj
题解:树形dp
设\(f[i]\)表示这个点有存档,之后都不存档的最少代价
\[f[u]=\sum_{(u,v,w)\in E}{f[v]+siz[v]*w}\]
设\(dp[u]\)表示这个点子树内有存档的最小代价
如果当前点存档,儿子也有一个存档则\(dp[u]+=dp[v]+deep[v]\),\(deep\)为从1到这个点的距离
否则直接转移(就是枚举一个最小值)
codeG
题面:bzoj
题解:
用set维护不能分辨出来的一群人,相当于染色
具体看代码吧
codeJ
题面:bzoj
题解:线段树
区间加法,区间乘法
codeO
#include<bits/stdc++.h>
using namespace std;
#define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout);
template<typename T>
inline void read(T& x)
{
x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
}
#define maxn 205
#define ll long long
struct Food
{
ll v,t;
inline friend bool operator < (Food a,Food b)
{
return a.t<b.t;
}
}a[maxn],sta[maxn];
int n,top;
ll m,f;
inline ll get(ll x)
{
ll left=m-f*x,times=0,days=0,last;//剩余钱数,总天数,一次的天数,这一次买的东西能支持的天数
if(left<0) return 0;
for(int i=1;i<=n;++i)
{
last=min(a[i].t-days,left/(x*a[i].v));
days+=last,times+=x*last,left-=last*x*a[i].v;
if(days<a[i].t)//如果还是小于,则left那一项不够了(相当于没有买够这一项),就直接全买这一项
{
times+=left/a[i].v;
break;
}
}
return times;
}
int main()
{
read(m),read(f),read(n);
for(int i=1;i<=n;++i) read(a[i].v),read(a[i].t),++a[i].t;
sort(a+1,a+n+1);
for(int i=1;i<=n;++i)
{
while(top&&a[i].v<=sta[top].v) --top;
sta[++top]=a[i];
}
n=top;
for(int i=1;i<=n;++i) a[i]=sta[i];
ll l=1,r=m/f+1,lm,rm,ans=0,ans1,ans2;
while(l<=r)
{
lm=(l+l+r)/3,rm=(l+r+r)/3;
ans1=get(lm),ans2=get(rm);
if(ans1<ans2) l=lm+1,ans=max(ans,ans2);
else r=rm-1,ans=max(ans,ans1);
}
if(l>1) ans=max(ans,get(l-1));//我的三分似乎有点锅,要把左右也算一下
ans=max(ans,get(l+1));
printf("%lld\n",ans);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
template<typename T>
inline void read(T& x)
{
x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
}
#define maxn 2000005
#define ll long long
struct Edge
{
int fr,to;
}eg[maxn],eg_rev[maxn];
int head[maxn],head_rev[maxn],edgenum,vis[maxn],n;
inline void add(int fr,int to)
{
eg[++edgenum]=(Edge){head[fr],to};
eg_rev[edgenum]=(Edge){head_rev[to],fr};
head[fr]=head_rev[to]=edgenum;
}
ll dis[maxn],s[maxn];
void spfa()
{
queue<int> q;
for(int i=1;i<=n;++i) q.push(i),vis[i]=1;
while(!q.empty())
{
int id=q.front();
q.pop(),vis[id]=0;
ll res=s[id];
for(int i=head[id];i;i=eg[i].fr)
res+=dis[eg[i].to];
if(res<dis[id])
{
dis[id]=res;
for(int i=head_rev[id];i;i=eg_rev[i].fr)
if(!vis[eg_rev[i].to]) vis[eg_rev[i].to]=1,q.push(eg_rev[i].to);
}
}
}
int main()
{
read(n);
int tp,num;
for(int i=1;i<=n;++i)
{
read(s[i]),read(dis[i]),read(num);
for(int j=1;j<=num;++j) read(tp),add(i,tp);
}
spfa();
printf("%lld\n",dis[1]);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout);
template<typename T>
inline void read(T& x)
{
x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
}
#define maxn 200005
#define ll long long
struct OP
{
int type;ll val;
}q[maxn];
pair<int,int> a[maxn];
struct Node
{
int l,r;
ll ptag,mtag,ptag2,mn,mx;
}t[maxn<<2];
#define ls rt<<1
#define rs rt<<1|1
ll L,R,ans[maxn];
inline void pushup(const int& rt)
{
t[rt].mx=t[rs].mx,t[rt].mn=t[ls].mn;
}
inline void modify(const int& rt,const ll& ptag,const ll& ptag2,const ll& mtag)
{
t[rt].mtag*=mtag,t[rt].ptag=t[rt].ptag*mtag+ptag,t[rt].ptag2=t[rt].ptag2*mtag+ptag2;
t[rt].mn=t[rt].mn*mtag+a[t[rt].l].first*ptag2+ptag;
t[rt].mx=t[rt].mx*mtag+a[t[rt].r].first*ptag2+ptag;
}
inline void pushdown(const int& rt)
{
modify(ls,t[rt].ptag,t[rt].ptag2,t[rt].mtag);
modify(rs,t[rt].ptag,t[rt].ptag2,t[rt].mtag);
t[rt].ptag=t[rt].ptag2=0,t[rt].mtag=1;
}
void build(const int& rt,const int& l,const int& r)
{
t[rt].mtag=1,t[rt].l=l,t[rt].r=r;
if(l==r)
{
t[rt].mx=t[rt].mn=a[l].first;
return;
}
int mid=(l+r)>>1;
build(ls,l,mid);
build(rs,mid+1,r);
pushup(rt);
}
void pushmin(const int& rt)
{
if(t[rt].l==t[rt].r) return modify(rt,L,0,0);
pushdown(rt);
if(t[rs].mn<L) modify(ls,L,0,0),pushmin(rs);
else pushmin(ls);
pushup(rt);
}
void pushmax(const int& rt)
{
if(t[rt].l==t[rt].r) return modify(rt,R,0,0);
pushdown(rt);
if(t[ls].mx>R) modify(rs,R,0,0),pushmax(ls);
else pushmax(rs);
pushup(rt);
}
void dfs(const int& rt)
{
if(t[rt].l==t[rt].r)
{
ans[a[t[rt].l].second]=t[rt].mn;
return;
}
pushdown(rt);
dfs(ls),dfs(rs);
}
int id[256];
int main()
{
//file("test");
int n,m;char op[2];
id['+']=0,id['-']=1,id['*']=2,id['@']=3;
read(n),read(L),read(R);
for(int i=1;i<=n;++i)
{
scanf("%s",op);
q[i].type=id[op[0]],read(q[i].val);
}
read(m);
for(int i=1;i<=m;++i) read(a[i].first),a[i].second=i;
sort(a+1,a+m+1);
build(1,1,m);
for(int i=1;i<=n;++i)
{
if(q[i].type==0) modify(1,q[i].val,0,1);
else if(q[i].type==1) modify(1,-q[i].val,0,1);
else if(q[i].type==2) modify(1,0,0,q[i].val);
else modify(1,0,q[i].val,1);
if(t[1].mn<L) pushmin(1);
if(t[1].mx>R) pushmax(1);
}
dfs(1);
for(int i=1;i<=m;++i) printf("%lld\n",ans[i]);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout);
inline void read(int& x)
{
x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
}
#define maxn 1000005
#define ll long long
struct Edge
{
int fr,to,val;
}eg[maxn<<1];
int head[maxn],edgenum;
inline void add(int fr,int to,int val)
{
eg[++edgenum]=(Edge){head[fr],to,val};
head[fr]=edgenum;
}
ll f[maxn],dp[maxn],siz[maxn];
void dfs(int rt,ll dis)
{
if(!head[rt])
{
siz[rt]=1;
return;
}
ll tmp=0;
for(int i=head[rt];i;i=eg[i].fr)
{
dfs(eg[i].to,eg[i].val+dis);
siz[rt]+=siz[eg[i].to];
f[rt]+=f[eg[i].to]+1ll*siz[eg[i].to]*eg[i].val;
tmp+=min(f[eg[i].to]+1ll*siz[eg[i].to]*eg[i].val,dp[eg[i].to]+dis+eg[i].val);
}
dp[rt]=f[rt];
for(int i=head[rt];i;i=eg[i].fr)
dp[rt]=min(dp[rt],tmp-min(f[eg[i].to]+1ll*siz[eg[i].to]*eg[i].val,dp[eg[i].to]+dis+eg[i].val)+dp[eg[i].to]+eg[i].val);
}
int main()
{
int n,k,a,b;
read(n);
for(int i=1;i<=n;++i)
{
read(k);
for(int j=1;j<=k;++j)
read(a),read(b),add(i,a,b);
}
dfs(1,0);
printf("%lld\n",dp[1]);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout);
inline void read(int& x)
{
x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
}
#define maxn 1000005
struct Edge
{
int fr,to;
}eg[maxn<<1];
int head[maxn],edgenum;
inline void add(int fr,int to)
{
eg[++edgenum]=(Edge){head[fr],to};
head[fr]=edgenum;
}
int vis[maxn],dfn[maxn],low[maxn],dfn_clock,col[maxn],siz,colnum,ans;
int invis[maxn],outvis[maxn];
stack<int> s;
void Tarjan(int rt)
{
low[rt]=dfn[rt]=++dfn_clock;
s.push(rt);
vis[rt]=1;
for(int i=head[rt];i;i=eg[i].fr)
{
#define to eg[i].to
if(!dfn[to]) Tarjan(to),low[rt]=min(low[rt],low[to]);
else if(vis[to]) low[rt]=min(low[rt],dfn[to]);
#undef to
}
if(low[rt]==dfn[rt])
{
++colnum;
siz=0;
while(s.top()!=rt)
{
int tp=s.top();
s.pop();
col[tp]=colnum;
++siz;
vis[tp]=0;
}
s.pop();
col[rt]=colnum;
++siz;
vis[rt]=0;
ans=max(ans,siz);
}
}
int main()
{
int n,m,x,y;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i) scanf("%d%d",&x,&y),add(x,y);
for(int i=1;i<=n;++i)
if(!dfn[i]) Tarjan(i);
printf("%d\n",ans);
for(int i=1;i<=n;++i)
for(int j=head[i];j;j=eg[j].fr)
if(col[i]!=col[eg[j].to])
invis[col[eg[j].to]]=1,outvis[col[i]]=1;
int indeg=0,outdeg=0;
for(int i=1;i<=colnum;++i)
{
if(!invis[i]) ++indeg;
if(!outvis[i]) ++outdeg;
}
printf("%d\n",max(indeg,outdeg));//注意不要特判0(我也不知道为什么)
return 0;
}
#include<bits/stdc++.h>
using namespace std;
inline void read(int& x)
{
x = 0; char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
}
#define maxn 100005
set<int> s[maxn];
int a[maxn], col[maxn], ans[maxn], cnt, n, m;
inline bool cmp(int a, int b)
{
return col[a] < col[b];
}
int main()
{
read(n), read(m);
int k;
for (int i = 1; i <= n; ++i) s[0].insert(i);
for (int i = 1; i <= m; ++i)
{
read(k);
for (int j = 1; j <= k; ++j) read(a[j]);
sort(a + 1, a + k + 1, cmp);
for (int l = 1, r; l <= k; l = r + 1)
{
r = l;
while (r < k && col[a[r + 1]] == col[a[l]]) ++r;
if (s[col[a[l]]].size() == r - l + 1) continue;
int now = col[a[l]]; ++cnt;
for (int j = l; j <= r; ++j)
s[now].erase(a[j]), col[a[j]] = cnt, s[cnt].insert(a[j]);
if (s[now].size() == 1 && !ans[*s[now].begin()])
ans[*s[now].begin()] = i;
if (s[cnt].size() == 1 && !ans[*s[cnt].begin()])
ans[*s[cnt].begin()] = i;
}
}
for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
//for (int i = 1; i <= n; ++i) printf("%d%c", ans[i], "\n"[i == n]);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
template<typename T>
inline void read(T& x)
{
x=0;char c=getchar();int f=1;
while(!isdigit(c)) {if(c=='-') f=-1;c=getchar();}
while(isdigit(c)) x=x*10+c-'0',c=getchar();
x*=f;
}
#define maxn 505
#define inf 0x3f3f3f3f
struct Edge
{
int fr,to,val;
}eg[maxn*maxn*2];
int head[maxn],edgenum=1,n,m;
inline void add(int fr,int to,int val)
{
eg[++edgenum]=(Edge){head[fr],to,val};
head[fr]=edgenum;
}
int deep[maxn],gap[maxn],cur[maxn],s,t,maxflow;
void bfs()
{
queue<int> q;
for(int i=0;i<=n;++i) deep[i]=-1,gap[i]=0;
deep[t]=0;gap[0]=1;
q.push(t);
while(!q.empty())
{
int tmp=q.front();q.pop();
for(int i=head[tmp];i;i=eg[i].fr)
if(deep[eg[i].to]==-1)
{
q.push(eg[i].to);
deep[eg[i].to]=deep[tmp]+1;
++gap[deep[eg[i].to]];
}
}
}
int dfs(int now,int flow)
{
if(now==t)
{
maxflow+=flow;
return flow;
}
int tmpflow=0,used=0;
for(int i=cur[now];i;i=eg[i].fr)
{
cur[now]=i;
if(deep[now]==deep[eg[i].to]+1&&eg[i].val)
{
if(tmpflow=dfs(eg[i].to,min(flow-used,eg[i].val)))
{
used+=tmpflow;
eg[i].val-=tmpflow;
eg[i^1].val+=tmpflow;
}
if(used==flow) return used;
}
}
if(!--gap[deep[now]]) deep[s]=n+1;
++gap[++deep[now]];
return used;
}
void ISAP()
{
bfs();
while(deep[s]<n) memcpy(cur,head,sizeof(head)),dfs(s,0x3f3f3f3f);
}
struct Node
{
int x,y,r,v;
}o[maxn];
inline int dis(Node a,Node b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int main()
{
read(n);
s=0,t=n+1;
for(int i=1;i<=n;++i)
read(o[i].x),read(o[i].y),read(o[i].r),read(o[i].v);
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
{
if(i==j) continue;
if(dis(o[i],o[j])<=o[i].r*o[i].r) add(i,j,inf),add(j,i,0);
}
int ans=0;
for(int i=1;i<=n;++i)
{
if(o[i].v>0) add(s,i,o[i].v),add(i,s,0),ans+=o[i].v;
else add(i,t,-o[i].v),add(t,i,0);
}
ISAP();
printf("%d\n",ans-maxflow);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout);
template<typename T>
inline void read(T& x)
{
x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
}
#define maxn 200005
#define ll long long
struct Node
{
int l,r;
ll val,ptag,mtag;
}t[maxn<<2];
#define ls rt<<1
#define rs rt<<1|1
ll p,a[maxn];
inline void pushup(const int& rt)
{
t[rt].val=(t[ls].val+t[rs].val)%p;
}
inline int len(const int& rt)
{
return t[rt].r-t[rt].l+1;
}
inline void modify(int rt,ll ptag,ll mtag)
{
if(ptag==0&&mtag==1) return;
t[rt].val=(t[rt].val*mtag+ptag*len(rt))%p;
t[rt].ptag=(t[rt].ptag*mtag+ptag)%p;
t[rt].mtag=t[rt].mtag*mtag%p;
}
inline void pushdown(const int& rt)
{
if(t[rt].ptag==0&&t[rt].mtag==1) return;
modify(ls,t[rt].ptag,t[rt].mtag);
modify(rs,t[rt].ptag,t[rt].mtag);
t[rt].ptag=0,t[rt].mtag=1;
}
inline void build(int rt,int l,int r)
{
t[rt].mtag=1,t[rt].l=l,t[rt].r=r;
if(l==r)
{
t[rt].val=a[l];
return;
}
int mid=(l+r)>>1;
build(ls,l,mid);
build(rs,mid+1,r);
pushup(rt);
}
inline void update(int rt,int l,int r,int fr,int to,ll ptag,ll mtag)
{
if(fr<=l&&to>=r) return modify(rt,ptag,mtag);
int mid=(l+r)>>1;
pushdown(rt);
if(fr<=mid) update(ls,l,mid,fr,to,ptag,mtag);
if(to>mid) update(rs,mid+1,r,fr,to,ptag,mtag);
pushup(rt);
}
inline ll query(int rt,int l,int r,int fr,int to)
{
if(fr<=l&&to>=r) return t[rt].val;
int mid=(l+r)>>1;ll ans=0;
pushdown(rt);
if(fr<=mid) ans=query(ls,l,mid,fr,to);
if(to>mid) ans+=query(rs,mid+1,r,fr,to);
return ans%p;
}
inline void print(int n)
{
for(int i=1;i<=n;++i) printf("%lld ",query(1,1,n,i,i));
puts(" ");
}
int main()
{
//file("test");
int n,m;
read(n),read(p);
for(int i=1;i<=n;++i) read(a[i]),a[i]%=p;
build(1,1,n);
read(m);
int op,x,y,z;
for(int i=1;i<=m;++i)
{
read(op),read(x),read(y);
if(op==3) printf("%lld\n",query(1,1,n,x,y));
else read(z),update(1,1,n,x,y,op==2?z:0,op==1?z:1);
}
return 0;
}
标签:就是 odi 电信 last gen second int bre else
原文地址:https://www.cnblogs.com/123789456ye/p/12262816.html