Educational Codeforces Round 102 (Rated for Div. 2) 寒假訓練題解
技術標籤:練習總結
A. Replacing Elements
In one step you can choose three distinct indices i, j, and k (i≠j; i≠k; j≠k) and assign the sum of aj and ak to ai, i. e. make ai=aj+ak.
Can you make all ai lower or equal to d using the operation above any number of times (possibly, zero)?
Input
The first line contains a single integer t (1≤t≤2000) — the number of test cases.
The first line of each test case contains two integers n and d (3≤n≤100; 1≤d≤100) — the number of elements in the array a and the value d.
The second line contains n integers a1,a2,…,an (1≤ai≤100) — the array a.
Output
For each test case, print YES, if it’s possible to make all elements ai less or equal than d using the operation above. Otherwise, print NO.
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
input
3
5 3
2 3 2 5 4
3 4
2 4 4
5 4
2 1 5 3 6
output
NO
YES
YES
解題思路:排個序把前兩個加一起比較一下大小就可以了
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,k,a[105];
int flag=0;
cin>>n>>k;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]>k) flag=1;
}
sort(a,a+n);
if(!flag) cout<<"YES"<<endl;
else{
if(a[0]+a[1]<=k) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return 0;
}
B. String LCM
Let’s define a multiplication operation between a string a and a positive integer x: a⋅x is the string that is a result of writing x copies of a one after another. For example, “abc” ⋅ 2 = “abcabc”, “a” ⋅ 5 = “aaaaa”.
A string a is divisible by another string b if there exists an integer x such that b⋅x=a. For example, “abababab” is divisible by “ab”, but is not divisible by “ababab” or “aa”.
LCM of two strings s and t (defined as LCM(s,t)) is the shortest non-empty string that is divisible by both s and t.
You are given two strings s and t. Find LCM(s,t) or report that it does not exist. It can be shown that if LCM(s,t) exists, it is unique.
Input
The first line contains one integer q (1≤q≤2000) — the number of test cases.
Each test case consists of two lines, containing strings s and t (1≤|s|,|t|≤20). Each character in each of these strings is either ‘a’ or ‘b’.
Output
For each test case, print LCM(s,t) if it exists; otherwise, print -1. It can be shown that if LCM(s,t) exists, it is unique.
Example
input
3
baba
ba
aa
aaa
aba
ab
output
baba
aaaaaa
-1
解題思路: 這題資料不大,可以直接暴力做
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#define ll long long
using namespace std;
int x;
int main(){
cin >> x;
while(x--){
string s, t;
int f = 0;
cin >> s;
cin >> t;
for(int i = 1; i < 150; i++){
string d, m;
for(int j = 0; j < i; j++) d += s;
for(int j = 1; j < 150; j++){
m += t;
if(m == d){
cout << d << endl;
f = 1;
break;
}
}
if(f == 1) break;
}
if(f == 0) cout << "-1" << endl;
}
return 0;
}