CF1593部分题解

A

若三个数相同,答案都是一

若两个数相等大于第三个,相等两数答案为一,第三数答案是大数减小数加一

若一个数严格大于另外两数,最大数为零两个小数为大数减小数加一

#include <bits/stdc++.h>
using namespace std;

int n , a , b , c ;


int main()
{
    cin >> n;
    for( register int i = 1 ; i <= n ; i ++ )
    {
        cin >> a >> b >> c;
        if( a == b && b == c ) printf("1 1 1\n");
        else if( a == b && a > c ) printf("1 1 %d\n" , a - c + 1 );
        else if( a == c && a > b ) printf("1 %d 1\n" , a - b + 1 );
        else if( b == c && b > a ) printf("%d 1 1\n" , b - a + 1 );
        else if( a > b && a > c ) printf("0 %d %d\n" , a - b + 1 , a - c + 1 );
        else if( b > a && b > c ) printf("%d 0 %d\n" , b - a + 1 , b - c + 1 );
        else printf("%d %d 0\n" , c - a + 1 , c - b + 1 );
    }
    return 0;
}

B

观察发现若该数一定被能被25整除,则该数各位和十位一定是25,50,75,00中一种

从后向前查询这两位数出现在哪里,第一次出现的位置后面的数都需要删除

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int INF = 0x7f7f7f7f;
int t , l , ans , n;
string s;

int main()
{
    cin >> t;
    for( ; t ; t -- )
    {
        cin >> s;
        n = s.size() , ans = INF;
        for( int i = n - 1 ; i > 0 ; i -- )
        {
            if( s[i] != '0' ) continue;
            for( int j = i - 1 ; j >= 0 ; j -- )
            {
                if( s[j] != '0' && s[j] != '5' ) continue;
                ans = n - j - 2;
                break;
            }
            break;
        }
        for( int i = n - 1 ; i > 0 ; i -- )
        {
            if( s[i] != '5' ) continue;
            for( int j = i - 1 ; j >= 0 ; j -- )
            {
                if( s[j] != '2' && s[j] != '7' ) continue;
                ans = min( ans , n - j - 2 );
                break;
            }
            break;
        }
        cout << ans << endl;
    }
    return 0;
}

C

贪心做,优先移动离洞口近的鼠

移动鼠\(i\)的时间为n-a[i],猫移动到洞口的时间为n-1

保证\(\sum_{i=k}^{n} a_i \leqslant n-1\)即可,枚举k并判断即可

#include <bits/stdc++.h>
using namespace std;


const int N = 4e5 + 5;
int n , m , t , a[N] , cnt ;
long long sum;

inline int read()
{
    register int x = 0;
    register char ch = getchar();
    while( ch < '0' || ch > '9' ) ch = getchar();
    while( ch >= '0' && ch <= '9' ) x = ( x << 3 ) + ( x << 1 ) + ch - '0' , ch = getchar();
    return x;
}


int main()
{
    t = read();
    while( t -- )
    {
        m = read() , n = read();
        for( register int i = 1 ; i <= n ; i ++ ) a[i] = read();
        sort( a + 1 , a + 1 + n , greater<int>() ) , cnt = sum = 0;
        for( register int i = 1 ; i <= n ; i ++ )
        {
            if( sum - a[i] + 1 <= 0 ) sum += m - a[i] , cnt ++ ;
            else break;
        }
        cout << cnt << endl;
    }
}

D1

所有数与最小的数差值的最大公约数即为答案

#include <bits/stdc++.h>
using namespace std;


const int N = 45;
int n , t , a[N]  ;

int gcd(int x, int y) {
	int z = y;
	while(x%y!=0)
	{
		z = x%y;
		x = y;
		y = z;
	}
	return z;
}

int main()
{
    cin >> t;
    while( t -- )
    {
        cin >> n;
        for( register int i = 1 ; i <= n ; i ++ ) cin >> a[i];
        sort( a + 1 , a + 1 + n );
        if( a[1] == a[n] )
        {
            cout << -1 << endl;
            continue;
        }
        int p = 0;
        for( register int i = 2 ; i <= n ; i ++ )
        {
            if( a[i] == a[1] ) continue;
            // t = a[i];
            if( !p ) p = abs( a[i] - a[1] );
            else p = gcd( p , abs( a[i]- a[1]));
        }
        cout << p << endl;
    }
}
posted @ 2021-10-15 21:01  PHarr  阅读(68)  评论(0编辑  收藏  举报