zoukankan      html  css  js  c++  java
  • bzoj 3704: 昊昊的机油之GRST【贪心+脑洞】

    脑洞题大概
    首先处理出每个位置需要操作的次数c,假设第一次达到目标就不能再走,这样的操作次数是c差分后值的正数和,就想成分治每一段然后同减最小值然后从0处断开
    然后考虑能一圈一圈走的情况,连续一段多走一圈在差分数组上的贡献是一个位置+4一个位置-4,
    然后对于差分c后值为(-3,3)的一对位置,+4-4操作后变成(1,-1),对答案的贡献会-2;(-3,2),(-2,3)同理,对答案的贡献会-1
    贪心的配对即可,先配(-3,3)的情况

    #include<iostream>
    #include<cstdio>
    using namespace std;
    const int N=10000005;
    int n,a[N],b[N],c[N],s[N],top,s2,s3,ans;
    bool v[N];
    int read()
    {
    	int r=0,f=1;
    	char p=getchar();
    	while(p>'9'||p<'0')
    	{
    		if(p=='-')
    			f=-1;
    		p=getchar();
    	}
    	while(p>='0'&&p<='9')
    	{
    		r=r*10+p-48;
    		p=getchar();
    	}
    	return r*f;
    }
    int main()
    {
    	n=read();
    	for(int i=1;i<=n;i++)
    		a[i]=read();
    	for(int i=1;i<=n;i++)
    		b[i]=read(),c[i]=(b[i]-a[i]+4)%4;
    	for(int i=n;i>=1;i--)
    	{
    		c[i]=c[i]-c[i-1];
    		if(c[i]>0)
    			ans+=c[i];
    	}
    	for(int i=1;i<=n;i++)
    	{
    		if(c[i]==-3)
    			s[++top]=i;
    		if(c[i]==3&&top)
    		{
    			ans-=2;
    			v[s[top--]]=1,v[i]=1;
    		}
    	}
    	for(int i=1;i<=n;i++)
    		if(!v[i])
    		{
    			if(c[i]==-3)
    				s3++;
    			if(c[i]==-2)
    				s2++;
    			if(c[i]==3&&s2)
    				ans--,s2--;
    			if(c[i]==2&&s3)
    				ans--,s3--;
    		}
    	printf("%d
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    左偏树
    output html
    jsp web.xml
    mysql link db
    beamline
    jsp embend java into html / mix scriptlets and HTML
    StringTokenizer
    TreeSet
    centOS 显示中文
    request and response
  • 原文地址:https://www.cnblogs.com/lokiii/p/10724194.html
Copyright © 2011-2022 走看看