A. Deletions of Two Adjacent Letters
题意
链接 :A. Deletions of Two Adjacent Letters 给出一个长度为奇数的字符串s(
1
≤
t
≤
49
1 \le t\le49
1 ≤ t ≤ 4 9 )和一个字符c,每一次操作可以把字符串中任意两个相邻的字符删除,如果可以通过任意次操作得到字符c则输出YES否则输出NO。 t组数据 (
1
≤
t
≤
1
0
3
1 \le t\le10^3
1 ≤ t ≤ 1 0 3 )
解题思路
代码
#include <bits/stdc++.h>
using namespace std;
int main ( )
{
int t;
scanf ( "%d" , & t) ;
while ( t-- )
{
string s, c;
cin>> s>> c;
int len= s. length ( ) , f= 0 ;
for ( int i= 0 ; i< len; i++ )
{
if ( ( i% 2 == 0 ) && ( s[ i] == c[ 0 ] ) )
{
f= 1 ; break ;
}
}
if ( f== 1 ) printf ( "YES\n" ) ;
else printf ( "NO\n" ) ;
}
return 0 ;
}
B. DIV + MOD
题意
链接 :B. DIV + MOD 令
f
a
(
x
)
=
⌊
x
a
⌋
+
x
m
o
d
a
f_a(x)=⌊\frac{x}{a}⌋+x \, mod\, a
f a ( x ) = ⌊ a x ⌋ + x m o d a ,给出a和x的取值范围
l
≤
x
≤
r
l \le x\le r
l ≤ x ≤ r ,求出
f
a
(
x
)
f_a(x)
f a ( x ) 的最大值
解题思路
数学 x在l到r内取值,x/a是递增,那么从r开始往左看就是递减,再看xmod a他是伪周期性递增,那么最大值只可能在r 这个点或者在倒数第二个周期的顶点 。
代码
#include <bits/stdc++.h>
using namespace std;
int main ( )
{
int t;
scanf ( "%d" , & t) ;
while ( t-- )
{
int l, r, a;
cin>> l>> r>> a;
int ans= r/ a+ r% a, x= r- ( r% a+ 1 ) ;
if ( x>= l) ans= max ( ans, x/ a+ x% a) ;
printf ( "%d\n" , ans) ;
}
return 0 ;
}
C. Weight of the System of Nested Segments
题意
解题思路
贪心 取权重最小的2n个点,如果将2n个点按坐标排序,一左一右取组合。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn= 2e5 + 5 ;
struct node{
int x, w, id;
} a[ maxn] , b[ maxn] ;
bool cmp1 ( node X, node Y)
{
return X. w< Y. w;
}
bool cmp2 ( node X, node Y)
{
return X. x< Y. x;
}
int main ( )
{
int t;
scanf ( "%d" , & t) ;
while ( t-- )
{
int n, m;
cin>> n>> m;
for ( int i= 1 ; i<= m; i++ )
{
cin>> a[ i] . x>> a[ i] . w;
a[ i] . id= i;
}
sort ( a+ 1 , a+ 1 + m, cmp1) ;
int len= 2 * n, ans= 0 ;
for ( int i= 1 ; i<= len; i++ )
{
b[ i] . id= a[ i] . id;
b[ i] . x= a[ i] . x;
b[ i] . w= a[ i] . w;
ans+ = a[ i] . w;
}
sort ( b+ 1 , b+ 1 + len, cmp2) ;
printf ( "%d\n" , ans) ;
for ( int i= 1 ; i<= n; i++ )
{
printf ( "%d %d\n" , b[ i] . id, b[ len- i+ 1 ] . id) ;
}
}
return 0 ;
}