Summer Camp 2 2

Zuma game

Zuma is a classic game. In the game, n beads are connected in a string and run on a track, and each bead has a color. The goal of the game is to eliminate these beads as much as possible.
The player can control the launcher to launch beads into the orbit, and the beads will be inserted between this string of beads (may also be added before the first bead or after the nth bead). If after launching, there are 3 or more beads in the same color segment of the launched beads, these beads will be eliminated; if the string of beads is divided into two parts after elimination, the two parts will merge, if new If the same color segment contains 3 or more beads, these beads will continue to be eliminated; the process will continue until it cannot be eliminated.
Now give the colors of n beads from left to right, and request the bead sequence obtained after a new bead of color c is emitted between the xth bead and the x+1th bead. In particular, when x=0, it means adding beads before the first bead; when x=n, it means adding beads after the nth bead.
The longest continuous segment consisting of beads of the same color that contains a certain bead is called the same color segment where the bead is located. Note: If the same color segment containing 3 or more beads exists at the beginning, it will not be eliminated directly.

Simulate the added position, if it can be connected, then continue to expand outward

#include<bits/stdc++.h>
using namespace std;
string s;
int x,len;
char c;
int main()
{
    
    
    cin>>len>>s>>x>>c;
    if(!x)s=c+s;
    else if(x==len)s=s+c;
    else s=s.substr(0,x)+c+s.substr(x,s.size());
    while(1) 
    {
    
    
      int l=x,r=x;
      while(s[l]==s[x])l--;l++;
      while(s[r]==s[x])r++;r--;
      if((r-l+1)<3)break;
      s=s.substr(0,l)+s.substr(r+1,s.size());
      x=l-1;
      if(x<0)break; 
    }
    cout<<s;
    return 0;
}


Sequence division

Insert picture description here

We can consider the discussion classification
1. The maximum number of three and is most certainly able separated;
2. Classification discussion considers the minimum value
when the maximum value on both sides: the maximum value of the maximum value of the right + left + center maximum value
when The maximum value is in the middle: prefix maximum value + middle value + suffix maximum value
(Proof) If the right or left is cut out, assuming that the cut is larger than the middle, the answer becomes larger, and if it is smaller than the middle, the answer remains unchanged

#include<bits/stdc++.h>
#define ull unsigned long long
using namespace std;
int  hsum[200010],tsum[200010],a[200010],n,b[200010];
int maxnans,minans;
int main()
{
    
    
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
    
    
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    sort(b+1,b+1+n);for(int i=n;i>=n-2;i--)maxnans+=b[i];//做最大值
 
    for(int i=2;i<n;i++)minans=max(minans,a[i]);//写把答案设定为当最大值在两边的情况,然后更新
    minans+=a[1]+a[n];
     
    for(int i=2;i<=n;i++)
    hsum[i]=max(hsum[i-1],a[i-1]);//前缀最大
     
 
    for(int i=n-1;i>=1;i--)
    tsum[i]=max(tsum[i+1],a[i+1]);//后缀最大
     
    for(int i=2;i<n;i++)
    minans=min(minans,a[i]+hsum[i]+tsum[i]);//算第二种方法,打擂台
    cout<<minans<<' '<<maxnans;
    return 0;
}

Autumn OutingInsert picture description here

When inputting an edge, you can split this edge apart, add the edge weight to the weights of the two points of the edge, and subtract the edge weight from the ans. If both are selected, one is offset and the other is added. If you don’t choose, then you’ve already chosen.

#include<bits/stdc++.h>
using namespace std;
int n,m;
inline int read()
{
    
    
    int f=1;
    int res=0;char ch=getchar();
    while(!isdigit(ch)){
    
    if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch))res=(res<<1)+(res<<3)+(ch&15),ch=getchar();
    return res*f;
}
int x,y,z,a[100010],ans;
bool cmp(int a,int b)
{
    
    
    return a>b;
}
int main()
{
    
    
    n=read();m=read();
    for(int i=1;i<=n;i++)a[i]=read();
    for(int i=1;i<=m;i++)
    {
    
    
        x=read();y=read();z=read();
        a[x]+=z;//妙啊
        a[y]+=z;//妙啊
        ans-=z;
    }   
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++)
    {
    
    
        ans+=a[i];
        printf("%d\n",ans);
    }
    return 0;
}


Rabbit Breeding Enhanced Edition

Insert picture description here

Comment directly

#include<bits/stdc++.h>
#define maxn 10000010
#define mod 10007
#define ll long long 
using namespace std;
ll k,n,t,m;
int dp[maxn],g[maxn];
inline int read()//读入优化
{
    
    
    int f=1;
    int res=0;char ch=getchar();
    while(!isdigit(ch)){
    
    if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch))res=(res<<1)+(res<<3)+(ch&15),ch=getchar();
    return res*f;
}
int main()
{
    
    
    n=read();k=read();t=read();m=read();
    dp[1]=1;g[1]=1;
    
    for(int i=2;i<=n;i++)
    {
    
       
        g[i]=(dp[max(0ll,i-k)]-dp[max(i-(k+m*t),0ll)]+mod)%mod;//假新生的兔子-死掉的兔子=实际新生的兔子
        dp[i]=(g[i]+dp[max(i-t,0ll)])%mod;//新生的兔子加上上个月的兔子
 
    }
    ll ans=0;
    for(int i=max(n-k-(m-1)*t+1,1ll);i<=n;i++)ans+=g[i],ans%=mod;//没死的兔子加起来就是答案
    cout<<ans%mod<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/yhhy666/article/details/108264832