水题易错点

数据运算时候int*int不能和long long比,所有的都要开成long long!
由此可见,设计测试数据的时候还是设计大点吧,都跑一遍,要不然会死那里!

For example:

Walking Between Houses http://codeforces.com/problemset/problem/1015/D

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    long long n,k,time=0,jian,now;//这里不开long long能玩死你!
    long long s;
    cin>>n>>k>>s;
    if ((n-1)*k<s or s<k)
    {
      cout<<"NO";
      return 0;
    }
    else
      cout<<"YES\n";
    while (k>0)
    {
        if (s-n+1>=(k-1))
        {
            k--;
            time++;
            s=s-n+1;
            if (time%2==1)
              printf("%d ",n);
            else
              printf("1 ");
        }
        else
        {
            k--;
            jian=s-k;
            if (time%2==1)
            {
                printf("%d ",n-jian);
                now=n-jian;
            }
            else
            {
                printf("%d ",1+jian);
                now=1+jian;
            }
                    for (int p=1;p<=k;p++)
                      if (p%2==1)
                        printf("%d ",now+1);
                      else
                        printf("%d ",now);
                    break;
        }
    }
    return 0;
}

循环退出机制要设置,不要死循环,该break就break!

For example:
B - Obtaining the String http://codeforces.com/problemset/problem/1015/B

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
//  freopen("test.in","r",stdin);
//  freopen("test.out","w",stdout);
    int n,tot=0,a[50],b[50],ans[10010];
    char s[100],t[100];
    cin>>n;
    cin>>s>>t;
    for (int i=0;i<=30;i++)
    {
      a[i]=0;
      b[i]=0;
    }
    for (int i=0;i<n;i++)
    {
      a[(int)s[i]-97]++;
      b[(int)t[i]-97]++;
    }
    for (int i=0;i<=30;i++)
      if (a[i]!=b[i])
      {
        cout<<-1;
        return 0;
      }
    for (int i=0;i<n-1;i++)
      if (s[i]!=t[i])
      {
        for (int j=i+1;j<n;j++)
          if (s[j]==t[i])
          {
            for (int k=j;k>i;k--)
            {
                char temp;
                temp=s[k];
                s[k]=s[k-1];
                s[k-1]=temp;
                tot++;
                ans[tot]=k;
            }
            break;//break break break!!!要不然会一直找
          }
      }
    printf("%d\n",tot);
    for (int i=1;i<=tot;i++)
      printf("%d ",ans[i]);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/IamIron-Man/p/12056845.html