Comet OJ simulation game Day1

A. practice

Topic Overview: select a single operation interval, the sum assigned a number in the interval interval numbers and clear the other numbers in the interval. Analyzing \ (A \) whether a sequence can become \ (B \) sequence is output if the minimum number of operations.

Bad practice:

Double pointer record now the position & a & sequences and \ (B \) position of the sequence, each time \ (b [i] \) is not equal to 0, \ (ANS ++ \) , if itself a [i] inherently conform conditions minus back. Plus the rest of the line, if the increase is not enough to output -1.

Also you need to start sentenced sum are equal.

#include<queue>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxm=1e+7;
int t,n,ans;
int a[maxm],b[maxm];
int solve()
{
 int r=1;
 ll sum=0;
 ans=0;
 for(int i=1;i<=n;i++)
 {
   if(!b[i]) continue;
   ans++;
   int l=r;
   sum=0;
   while((sum<b[i])&&(r<=n)) sum+=a[r++];
   if(sum!=b[i]){
     return -1;
   }
   if(i>=l&&i<=r-1&&a[i]==b[i]) ans--;//;如果段只包含了自己并且位置相同,我们就不需要浪费操作在它身上
 }
 return ans;
}
int main()
{
 scanf("%d",&t);
 while(t--)
 {
  scanf("%d",&n);   
  ll sum1=0,sum2=0;
  for(int i=1;i<=n;i++)
  scanf("%d",a+i),sum1+=a[i];
  for(int i=1;i<=n;i++)
  scanf("%d",b+i),sum2+=b[i];
  if(sum1!=sum2)//如果b[i]后面有连续的0,下面的就不能输出-1了 
  {
   printf("-1\n");
   continue;    
  }
  printf("%d\n",solve());
 }
 return 0;  
}

Guess you like

Origin www.cnblogs.com/lihan123/p/11828940.html