A Codeforces Round #398 (Div. 2)

#include<bits/stdc++.h>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define
int a[MAXN];
int c[MAXN];
priority_queue<int> q;
int main()
{
// freopen("A.in","r",stdin);
// freopen(".out","w",stdout);
int n=read();
For(i,n) cin>>a[i],c[i]=a[i];
sort(c+1,c+1+n);
int p=n;
For(i,n) {
q.push(a[i]);
bool fl=0;
while(!q.empty()&&q.top()==c[p]) {
if (!fl) fl=1;else putchar(' ');
cout<<c[p--];
q.pop();
}
puts("");
}
return 0;
}

B The Queue

还有点小问题

C Garland

#include<bits/stdc++.h>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define
vi edges[MAXN];
int fa[MAXN],d[MAXN];
int n,c[MAXN]={0},a[MAXN]={0},b[MAXN]={0};
void dfs(int x,int dep=0) {
Rep(i,SI(edges[x])) {
int v=edges[x][i];
if (b[v]) continue;
dfs(v,dep+1);
a[x]+=a[v];
}
a[x]+=c[x];
d[x]=dep;
}
void dfs2(int x) {
Rep(i,SI(edges[x])) {
int v=edges[x][i];
dfs2(v);
b[v]=1;
}
b[x]=1;
}

int main()
{
// freopen("C.in","r",stdin);
// freopen(".out","w",stdout);
cin>>n;
int tt=0,ro;
For(i,n) {
fa[i]=read();
if (!fa[i]) ro=i;
int t=read();
edges[fa[i]].pb(i);
c[i]=t;tt+=t;
}
if (tt%3!=0) {
puts("-1");return 0;
}
tt/=3;
MEM(a)
dfs(ro);
int ans1=0,dp=-1;
For(i,n) if (a[i]==tt&&i!=ro && dp<=d[i]) ans1=i,dp=d[i];
if (!ans1) {
puts("-1");return 0;
}
dfs2(ans1);
MEM(a) b[ans1]=1;
dfs(ro);
int ans2=0; dp=-1;
For(i,n) if (!b[i] && a[i]==tt&&i!=ro && dp<=d[i]) ans2=i,dp=d[i];
if (!ans2) {
puts("-1");return 0;
}
if (ans1>ans2) swap(ans1,ans2);
cout<<ans1<<' '<<ans2<<endl;

return 0;
}

D Cartons of milk

二分

#include<bits/stdc++.h>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define
ll a[MAXN];
pair<ll,int > b[MAXN];
ll c[MAXN];
int n,m,k;
ll calc(int p) {
int l1=1,l2=m-p+1;
int t=0;
For(i,n+p) {
if (l1>n) c[++t]=b[l2].fi,l2++;
else if (l2>m) c[++t]=a[l1],l1++;
else {
if (a[l1]<b[l2].fi) c[++t]=a[l1],l1++;
else c[++t]=b[l2].fi,l2++;
}
}
sort(c+1,c+1+t);
ll dt=0,nd=k;
For(i,t) {
if (!nd) ++dt,nd+=k;
if (c[i]<dt) return 0;
nd--;
}
return 1;
}
int main()
{
// freopen("D.in","r",stdin);
// freopen(".out","w",stdout);
n=read(),m=read(),k=read();
For(i,n) a[i]=read();
For(i,m) b[i]=mp(read(),i);
sort(a+1,a+1+n);
sort(b+1,b+1+m);
int l=0,r=m,ans=0;
if (!calc(0)) puts("-1");
else {
while(l<=r) {
int m=(l+r)/2;
if (calc(m)) ans=m,l=m+1;
else r=m-1;
}
cout<<ans<<endl;
bool fl=0;
Fork(i,m-ans+1,m) {
if (!fl) fl=1;
else putchar(' ');
cout<<b[i].se;
}puts("");
}



return 0;
}