Codeforces Round #569 Div. 1

  A: the maximum value of n-1 operations will be placed first, then the front force simulation n-1 times, then obviously cycle.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1000000010
#define N 300010
#define int long long
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
int n, q, a [N] years [N] [2];
and <int> N;
signed main()
{
	n=read(),q=read();
	for (int i=1;i<=n;i++) a[i]=read();
	for (int i=1;i<=n;i++) Q.push_back(a[i]);
	for (int i=1;i<n;i++)
	{
		int x=Q.front();Q.pop_front();
		int y=Q.front();Q.pop_front();
		years old [i] [0] = x, years [i] [1] = y;
		Q.push_front(max(x,y)),Q.push_back(min(x,y));
	}
	for (int i=1;i<=n;i++)
	{
		a[i]=Q.front(),Q.pop_front();
	}
	while (q--)
	{
		int x=read();
		if (x<n) printf("%I64d %I64d\n",ans[x][0],ans[x][1]);
		else printf("%I64d %I64d\n",a[1],a[(x-1)%(n-1)+2]);
	}
	return 0;
	//NOTICE LONG LONG!!!!!
}

  B: consider the one-dimensional configuration, each endpoint can take on both sides. Extended to two-dimensional, on-line is still under construction method using the one-dimensional case, of course, endpoints need to be repeated several times to alternately take over all the points. Configuration corresponds to a column question: 1 ~ n to each of the respective numbers 2, to find a differential arranged such that the same sequence number does not exist. The method can use a similar configuration, and even if the first half and the second half from each of pass 1 ~ n, a first half of the end sides each taking the same dimension, the front half of the half mirror.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1000000010
#define N 1000010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
int n,m,a[N],b[N];
signed main()
{
	n=read(),m=read();
	you and = 0;
	for (int i=1;i<=n/2;i++)
		for (int j=1;j<=2*m;j++)
		a[++u]=j&1?i:n-i+1;
	for (;u<=n*m;) a[++u]=n/2+1;
	u = 0;
	for (int i=1;i<=n;i++)
	{
		int p = 1;
		for (int j=1;j<=m;j++)
		{
			b[++u]=p;
			if (j&1) p=m-p+1;
			else p=m-p+2;
		}
	}
	for (int i=1;i<=n;i++)
	if (i%2==0) reverse(b+(i-1)*m+1,b+i*m+1);
	for (int i=1;i<=n*m;i++) printf("%d %d\n",a[i],b[i]);
	return 0;
	//NOTICE LONG LONG!!!!!
}

  C: easy to think sequence actually does not matter. Consider the price of all food and everyone's property on the number line, food standard human standard +1 -1 if the last dish as the answer, then obviously it is only required to meet a final suffix and> 0 of dishes. The largest segment tree maintenance and suffixes and query.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1000000010
#define N 300010
#define M 1000000
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
int n,m,q,a[N],b[N],mx[M+10<<2],lazy[M+10<<2];
void up(int k)
{
	mx[k]=max(mx[k<<1],mx[k<<1|1]);
}
void update(int k,int x)
{
	mx[k]+=x;
	lazy[k]+=x;
}
void down(int k)
{
	update(k<<1,lazy[k]);
	update(k<<1|1,lazy[k]);
	lazy[k]=0;
}
void add (k you, you're, you're down, you x, y you, you p)
{
	if (l==x&&r==y) {update(k,p);return;}
	if (lazy[k]) down(k);
	int mid=l+r>>1;
	if (y<=mid) add(k<<1,l,mid,x,y,p);
	else if (x>mid) add(k<<1|1,mid+1,r,x,y,p);
	else add(k<<1,l,mid,x,mid,p),add(k<<1|1,mid+1,r,mid+1,y,p);
	up(k);
}
int query(int k,int l,int r)
{
	if (l==r) return mx[k]>0?l:-1;
	if (lazy[k]) down(k);
	int mid=l+r>>1;
	if (mx[k<<1|1]>0) return query(k<<1|1,mid+1,r);
	else return query(k<<1,l,mid);
}
signed main()
{
	n=read(),m=read();
	for (int i=1;i<=n;i++) a[i]=read();
	for (int i=1;i<=m;i++) b[i]=read();
	for (int i=1;i<=n;i++) add(1,1,M,1,a[i],1);
	for (int i=1;i<=m;i++) add(1,1,M,1,b[i],-1);
	q=read();
	while (q--)
	{
		int op=read(),p=read(),x=read();
		if (at == 1)
		{
			add(1,1,M,1,a[p],-1);
			a[p]=x;
			add(1,1,M,1,a[p],1);
		}
		else
		{
			add(1,1,M,1,b[p],1);
			b[p]=x;
			add(1,1,M,1,b[p],-1);
		}
		printf("%d\n",query(1,1,M));
	}
	return 0;
	//NOTICE LONG LONG!!!!!
}

  D: little conversion can be obtained, corresponding to a selected chain, the chain that was sequentially cut each portion to minimize the size and the square. A comparison obvious dp is set f [i] is considered only a subtree i, i of the selected one strand, can be obtained and the least squares. Violence which sub-tree at both ends of the transfer need to enumerate each chain in, apparently chrysanthemum degraded. But easy to find for the same size of the sub-tree, we just need to get the maximum value dp and the second largest, so the complexity of the violence is clearly no more than O (nsqrtn) a.

  But there is a not permit the practice, that seek to mimic twice dfs diameter, just find another Imperial endpoints optimized endpoint can answer, end found the answer will certainly be in the chain of your choice.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1000000010
#define N 500010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
int n,p[N],size[N],t;
ll f[N];
struct data{int to,nxt;
}edge[N<<1];
void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;}
void getsize(int k,int from)
{
	size[k]=1;
	for (int i=p[k];i;i=edge[i].nxt)
	if (edge[i].to!=from)
	{
		getsize(edge[i].to,k);
		size[k]+=size[edge[i].to];
	}
}
void dfs(int k,int from)
{
	for (int i=p[k];i;i=edge[i].nxt)
	if (edge[i].to!=from)
	{
		f[edge[i].to]=f[k]+1ll*(size[k]-size[edge[i].to])*size[edge[i].to];
		dfs(edge[i].to,k);
	}
}
signed main()
{
	n=read();
	for (int i=1;i<n;i++)
	{
		int x=read(),y=read();
		addedge (x, y), addedge (y, x);
	}
	getsize(1,1);
	f[1]=0;dfs(1,1);
	int root=0;for (int i=1;i<=n;i++) if (f[i]>f[root]) root=i;
	getsize(root,root);
	f[root]=0;dfs(root,root);
	ll ans=0;
	for (int i=1;i<=n;i++) ans=max(ans,f[i]);
	cout << years + 1ll * n * (n-1) / 2 ;;
	return 0;
	//NOTICE LONG LONG!!!!!
}

  

Guess you like

Origin www.cnblogs.com/Gloid/p/11094019.html