文章目录
#725-Div.3
A. Stone Game
先找到最大值最小值的位置,然后有三种选取(两边中走一边/两边都走一点)
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define maxn 105
int T, n;
int a[maxn];
bool vis[maxn];
int main() {
scanf( "%d", &T );
while( T -- ) {
scanf( "%d", &n );
for( int i = 1;i <= n;i ++ )
scanf( "%d", &a[i] );
int minn = maxn, maxx = -maxn, pos_min, pos_max;
for( int i = 1;i <= n;i ++ ) {
if( a[i] < minn ) minn = a[i], pos_min = i;
if( a[i] > maxx ) maxx = a[i], pos_max = i;
}
int l = min( pos_min, pos_max ), r = max( pos_min, pos_max );
printf( "%d\n", min( r, min( n - l + 1, l + n - r + 1 ) ) );
}
return 0;
}
B. Friends and Candies
首先糖果要能被均分,然后肯定是糖果多的人分给别人
#include <cstdio>
#define maxn 200005
int T, n;
int a[maxn];
int main() {
scanf( "%d", &T );
while( T -- ) {
scanf( "%d", &n );
int tot = 0;
for( int i = 1;i <= n;i ++ ) {
scanf( "%d", &a[i] );
tot += a[i];
}
if( tot % n ) printf( "-1\n" );
else {
tot /= n;
int ans = 0;
for( int i = 1;i <= n;i ++ )
if( a[i] > tot ) ans ++;
printf( "%d\n", ans );
}
}
return 0;
}
C. Number of Pairs
先排个序,用lower_bound/upper_bound
查询要求值域中的值
#include <cstdio>
#include <algorithm>
using namespace std;
#define maxn 200005
#define int long long
int T, n, l, r;
int a[maxn];
signed main() {
scanf( "%lld", &T );
while( T -- ) {
scanf( "%lld %lld %lld", &n, &l, &r );
int ans = 0;
for( int i = 1;i <= n;i ++ )
scanf( "%lld", &a[i] );
sort( a + 1, a + n + 1 );
for( int i = 1;i < n;i ++ ) {
if( a[i] > r ) continue;
int L, R;
R = upper_bound( a + i + 1, a + n + 1, r - a[i] ) - a - 1 - 1;
L = lower_bound( a + i + 1, a + n + 1, l - a[i] ) - a - 1 - 1;
ans += R - L;
}
printf( "%lld\n", ans );
}
return 0;
}
D. Another Problem About Dividing Numbers
特判k=1
的情况
最小的操作次数肯定是 2 2 2,直接除成 1 1 1,所以只需要考虑最大操作次数,在这之间的操作次数肯定都是可以达到的,显然
最大操作次数就是唯一质因数分解的幂次和
刚开始还担心根号带 T T T跑不过(#.#)
#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
int T, a, b, k;
int gcd( int x, int y ) {
if( x < y ) swap( x, y );
if( ! y ) return x;
else return gcd( y, x % y );
}
int main() {
scanf( "%d", &T );
while( T -- ) {
scanf( "%d %d %d", &a, &b, &k );
if( k == 1 ) {
if( a == b ) printf( "No\n" );
else if( a % b == 0 || b % a == 0 ) printf( "Yes\n" );
else printf( "No\n" );
continue;
}
int cnta = 0, Sqr = sqrt( a );
for( int i = 2;i <= Sqr;i ++ )
if( a % i == 0 )
while( a % i == 0 ) a /= i, cnta ++;
if( a != 1 ) cnta ++;
int cntb = 0; Sqr = sqrt( b );
for( int i = 2;i <= Sqr;i ++ )
if( b % i == 0 )
while( b % i == 0 ) b /= i, cntb ++;
if( b != 1 ) cntb ++;
if( k <= cnta + cntb ) printf( "Yes\n" );
else printf( "No\n" );
}
return 0;
}
E. Funny Substrings
过分原始的暴力模拟不可取,疯狂自叠加字符串长度可以达到 2 50 2^{50} 250级别,望而却步
事实上对于每一个字符串,我们只需要存下haha
的个数,长度,以及前后各三个字符即可
因为只有这特殊的六个字符才可能与新的字符串相加时产生新的haha
#include <map>
#include <cstdio>
#include <iostream>
using namespace std;
#define int long long
struct word {
int len, cnt;
string s;
};
map < string, word > mp;
string GetHead( string s ) {
if( s.size() < 3 ) return s;
else return s.substr( 0, 3 );
}
string GetTail( string s ) {
if( s.size() < 3 ) return s;
else return s.substr( s.size() - 3, 3 );
}
int calc( string s, string p ) {
int cnt = 0;
for( int i = 0;i + p.size() <= s.size();i ++ )
if( s.substr( i, p.size() ) == p ) cnt ++;
return cnt;
}
word merge( word a, word b ) {
word New;
New.len = a.len + b.len;
New.cnt = a.cnt + b.cnt + calc( GetTail( a.s ) + GetHead( b.s ), "haha" );
New.s = a.s + b.s;
if( New.s.size() > 7 ) New.s = GetHead( a.s ) + "#" + GetTail( b.s );
return New;
}
signed main() {
int T, n;
string a, b, c, op, plus;
scanf( "%lld", &T );
while( T -- ) {
mp.clear();
scanf( "%lld", &n );
int ans = 0;
while( n -- ) {
cin >> a >> op >> b;
if( op == ":=" ) {
word New;
New.cnt = calc( b, "haha" );
New.len = b.length();
New.s = b;
mp[a] = New;
}
else {
cin >> plus >> c;
mp[a] = merge( mp[b], mp[c] );
}
ans = mp[a].cnt;
}
printf( "%lld\n", ans );
}
return 0;
}
F. Interesting Function
差分一下
发现对于任何数位上的值,完整向前一位进 1 1 1,变换次数都是 10 10 10
所以枚举数位,看前面进位次数乘个 10 10 10,再加上该数位的值即可
#include <cstdio>
#define int long long
int cnt[20];
int solve( int x ) {
int ans = 0;
while( x ) {
int r = x % 10; x /= 10;
ans += x * 10 + r;
}
return ans;
}
signed main() {
int T, l, r;
scanf( "%lld", &T );
while( T -- ) {
scanf( "%lld %lld", &l, &r );
printf( "%lld\n", solve( r ) - solve( l ) );
}
return 0;
}
G. Gift Set
能分成 n n n个组合,就蕴含 n − 1 n-1 n−1
所以考虑二分组合个数 t t t,假设 x > y , a > b x>y,a>b x>y,a>b
x , y x,y x,y都至少会减去 t × b t\times b t×b个,剩下了 t × ( a − b ) t\times(a-b) t×(a−b)就随机让 x − t × b , y − t × b x-t\times b, y-t\times b x−t×b,y−t×b补上
用向下取整判断即可
#include <cstdio>
#include <iostream>
using namespace std;
int T, x, y, a, b;
bool check( int t ) {
int X = x - t * b, Y = y - t * b;
if( X / ( a - b ) + Y / ( a - b ) >= t ) return 1;
else return 0;
}
int main() {
scanf( "%d", &T );
while( T -- ) {
scanf( "%d %d %d %d", &x, &y, &a, &b );
if( a == b ) {
printf( "%d\n", min( x / a, y / b ) );
continue;
}
if( x < y ) swap( x, y );
if( a < b ) swap( a, b );
int l = 0, r = y / b, ans;
while( l <= r ) {
int mid = ( l + r ) >> 1;
if( check( mid ) ) ans = mid, l = mid + 1;
else r = mid - 1;
}
printf( "%d\n", ans );
}
return 0;
}