【LDU】2018个人PK赛#4

A - Quasi-palindrome

Let quasi-palindromic number be such number that adding some leading zeros (possible none) to it produces a palindromic string.

String t is called a palindrome, if it reads the same from left to right and from right to left.

For example, numbers 131 and 2010200 are quasi-palindromic, they can be transformed to strings "131" and "002010200", respectively, which are palindromes.

You are given some integer number x. Check if it's a quasi-palindromic number.

Input

The first line contains one integer number x (1 ≤ x ≤ 109). This number is given without any leading zeroes.

Output

Print "YES" if number x is quasi-palindromic. Otherwise, print "NO" (without quotes).

Example
Input
131
Output
YES
Input
320
Output
NO
Input
2010200
Output
YES

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
      ll a,c;
      scanf("%lld",&a);
      while(a%10==0)
      {
            a/=10;
      }
      c=a;
      ll b=0;
      for(int i=0;c!=0;i++)
      {
            b=b*10+c%10;
            c/=10;
      }
      if(a==b)
      {
        printf("YES\n");
      }
      else
      {
        printf("NO\n");
      }
      return 0;
}

B - Luba And The Ticket 

Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky.

The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits.

Input

You are given a string consisting of 6 characters (all characters are digits from 0 to 9) — this string denotes Luba's ticket. The ticket can start with the digit 0.

Output

Print one number — the minimum possible number of digits Luba needs to replace to make the ticket lucky.

Examples
input
000000
output
0
input
123456
output
2
input
111000
output
1
Note

In the first example the ticket is already lucky, so the answer is 0.

In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required.

In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.


有三种做法,首先是暴力,然后有暴力,然后有技巧的。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char a[7];
    scanf("%s",a);
    int cnt=3;
    if(a[0]+a[1]+a[2]==a[3]+a[4]+a[5])
    {
        printf("0\n");
        return 0;
    }
    int a1,a2,a3,a4,a5,a6;
    a1=a[0]-'0';
    a2=a[1]-'0';
    a3=a[2]-'0';
    a4=a[3]-'0';
    a5=a[4]-'0';
    a6=a[5]-'0';
    for(int i1=0;i1<10;i1++)
    {
        for(int i2=0;i2<10;i2++)
         {
            for(int i3=0;i3<10;i3++)
            {
                for(int i4=0;i4<10;i4++)
                {
                    for(int i5=0;i5<10;i5++)
                    {
                        for(int i6=0;i6<10;i6++)
                        {
                            if(i1+i2+i3!=i4+i5+i6)
                                continue;
                            int ans=0;
                            ans+=(a[0]-'0'!=i1);
                            ans+=(a[1]-'0'!=i2);
                            ans+=(a[2]-'0'!=i3);
                            ans+=(a[3]-'0'!=i4);
                            ans+=(a[4]-'0'!=i5);
                            ans+=(a[5]-'0'!=i6);
                            cnt=min(ans,cnt);
                        }
                    }
                }
            }
         }
    }
    printf("%d\n",cnt);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int maxz(int a,int b,int c)
{
    return max(a,b)>c?max(a,b):c;
}
int minz(int a,int b,int c)
{
    return min(a,b)<c?min(a,b):c;
}
int diff(int a,int b)
{
    return abs(a-b);
}
int mid(int a,int b,int c)
{
    int d[3]={a,b,c};
    sort(d,d+3);
    return d[1];
}
int main()
{
    char a[7];
    cin>>a;
    int b[6];
    for(int i=0;i<6;i++)
    {
        b[i]=a[i]-'0';
    }
    int sum1=b[0]+b[1]+b[2];
    int sum2=b[3]+b[4]+b[5];
    if(sum1==sum2)
    {
        printf("0\n");
        return 0;
    }
    else if(diff(sum1,sum2)>18)
    {
        printf("3\n");
        return 0;
    }
    else if(sum1>sum2&&(sum1-sum2<=9-minz(b[3],b[4],b[5])||sum1-sum2<=maxz(b[0],b[1],b[2])))
    {
        printf("1\n");
        return 0;
    }
    else if(sum1<sum2&&(sum2-sum1<=9-minz(b[0],b[1],b[2])||sum2-sum1<=maxz(b[3],b[4],b[5])))
    {
        printf("1\n");
        return 0;
    }
    else if(sum1>sum2&&diff(sum1,sum2)<=maxz(b[0],b[1],b[2])+9-minz(b[3],b[4],b[5]))
    {
        printf("2\n");
        return 0;
    }
    else if(sum1<sum2&&diff(sum1,sum2)<=maxz(b[3],b[4],b[5])+9-minz(b[0],b[1],b[2]))
    {
        printf("2\n");
        return 0;
    }
    else if(sum1>sum2&&diff(sum1,sum2)<=maxz(b[0],b[1],b[2])+mid(b[0],b[1],b[2]))
    {
        printf("2\n");
        return 0;
    }
    else if(sum1>sum2&&diff(sum1,sum2)<=18-minz(b[3],b[4],b[5])-mid(b[3],b[4],b[5]))
    {
        printf("2\n");
        return 0;
    }
    else if(sum2>sum1&&diff(sum1,sum2)<=maxz(b[3],b[4],b[5])+mid(b[3],b[4],b[5]))
    {
        printf("2\n");
        return 0;
    }
    else if(sum2>sum1&&diff(sum1,sum2)<=18-minz(b[0],b[1],b[2])-mid(b[0],b[1],b[2]))
    {
        printf("2\n");
        return 0;
    }
    else
    {
        printf("3\n");
        return 0;
    }

}
#include<bits/stdc++.h>
using namespace std;
int cmp(int a,int b)
{
    return a>b;
}
int main()
{
    char s[7];
        cin>>s;
    int sum=0;
    int a[6];
    for(int i=0;i<6;i++)
    {
        if(i<=2)
            sum+=s[i]-'0';
        else
            sum-=s[i]-'0';
    }
    if(sum==0)
    {
        return 0*printf("0\n");
    }
    else if(sum>0)
    {
        for(int i=0;i<6;i++)
        {
            if(i<3)
                a[i]=s[i]-'0';
            else
                a[i]=9-(s[i]-'0');
        }
    }
    else
    {
        for(int i=0;i<6;i++)
        {
            if(i<3)
                a[i]=9-(s[i]-'0');
            else
                a[i]=s[i]-'0';
        }
    }
    sum=abs(sum);
    sort(a,a+6,cmp);
    /*for(int i=0;i<6;i++)
    {
        printf("%d\n",a[i]);
    }*/
    for(int i=0;i<6;i++)
    {
         if(sum-a[i]<=0)
         {
            printf("%d\n",i+1);
            return 0;
         }
        else
            sum-=a[i];
    }

    return 0;
}

C - Bertown Subway


The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself.

There are n stations in the subway. It was built according to the Bertown Transport Law:

  1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i;
  2. For each station i there exists exactly one station j such that pj = i.

The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n).

The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes.

The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get!

Input

The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations.

The second line contains n integer numbers p1p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct.

Output

Print one number — the maximum possible value of convenience.

Examples
input
3
2 1 3
output
9
input
5
1 5 4 3 2
output
17
Note

In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1, 1)(1, 2)(1, 3)(2, 1)(2, 2)(2, 3)(3, 1)(3, 2)(3, 3).

In the second example the mayor can change p2 to 4 and p3 to 5.


这题说的是有个市长来巡查,希望把两个环连成一个大环,其中把环内的节点的总和
的平方  求的就是  所有  k平方的总和。
#include<bits/stdc++.h>
using namespace std;
int p[100005];
bool vis[100005]={false};
int main()
{
    int n,t=0;
    long long r=0;
    long long max1=0,max2=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>p[i];
    }
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==true)
            continue;
        long long cnt=1;
        int x=p[i];
        vis[x]=true;
        while(x!=i)
        {
            x=p[x];
            vis[x]=true;
            cnt++;
        }
        r=r+cnt*cnt;
        if(cnt>max1)
        {
            max2=max1;
            max1=cnt;
        }
        else if(cnt>max2)
        {
            max2=cnt;
        }
    }
        r=r+2LL*(max1*max2);
        cout<<r;
        return 0;

}

D - 最少拦截系统

 
 

Problem Description
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹.
怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统.
 

Input
输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔)
 

Output
对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统.
 

Sample Input
 
            
8 389 207 155 300 299 170 158 65
 
Sample Output
 
            
2

#include<stdio.h>
typedef long long ll;
int main()
{
    ll n,a[10000],b[10000],i,j,ans,cnt;
    while(~scanf("%lld",&n))
    {
        for(i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
        }
        b[0]=a[0];
        cnt=0;
        for(ll i=1;i<n;i++)
        {
            for(j=0;j<=cnt;j++)
            {
                if(b[j]>a[i])
                {
                    b[j]=a[i];
                    break;
                }
            }
            if(j==cnt+1)
            {
                b[++cnt]=a[i];
            }
        }
        printf("%lld\n",cnt+1);
    }
       /* for(int i=0;i<=cnt;i++)
        {
            printf("%d\n",b[i]);
        }*/

    return 0;
}

#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,i,j,a[1000],dp[1000];
    while(~scanf("%d",&n))
    {
        int ans=1;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=0;i<n;i++)
        {
            dp[i]=1;
            for(int j=0;j<i;j++)
            {
                if(a[j]<a[i])
                {
                    dp[i]=max(dp[i],dp[j]+1);
                }
            }
            ans=max(ans,dp[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}


E - K-Dominant Character

 

You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant iff each substring of s with length at least kcontains this character c.

You have to find minimum k such that there exists at least one k-dominant character.

Input

The first line contains string s consisting of lowercase Latin letters (1 ≤ |s| ≤ 100000).

Output

Print one number — the minimum value of k such that there exists at least one k-dominant character.

Examples
input
abacaba
output
2
input
zzzzz
output
1
input
abcde
output
3

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
char a[maxn];
int alpha[50],last[50];
int main()
{
        scanf("%s",a);
        memset(last,-1,sizeof(last));
        int len=strlen(a);
        for(int i=0;i<len;i++)
        {
            int x=a[i]-'a';
            alpha[x]=max(alpha[x],i-last[x]);
            last[x]=i;
        }
        for(int i=0;i<26;i++)
        {
            alpha[i]=max(alpha[i],len-last[i]);
        }
        int ans=len/2+1;
        for(int i=0;i<26;i++)
        {
            ans=min(ans,alpha[i]);
        }
        printf("%d\n",ans);
        return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值