A. Bear and Big Brother
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Bear Limak wants to become the largest of bears, or at least to become larger than his brother Bob.
Right now, Limak and Bob weigh a and b respectively. It's guaranteed that Limak's weight is smaller than or equal to his brother's weight.
Limak eats a lot and his weight is tripled after every year, while Bob's weight is doubled after every year.
After how many full years will Limak become strictly larger (strictly heavier) than Bob?
Input
The only line of the input contains two integers a andb (1 ≤ a ≤ b ≤ 10) — the weight of Limak and the weight of Bob respectively.
Output
Print one integer, denoting the integer number of years after which Limak will become strictly larger than Bob.
Examples
Input
4 7
Output
2
Input
4 9
Output
3
Input
1 1
Output
1
Note
In the first sample, Limak weighs 4 and Bob weighs7 initially. After one year their weights are4·3 = 12 and 7·2 = 14 respectively (one weight is tripled while the other one is doubled). Limak isn't larger than Bob yet. After the second year weights are36 and28, so the first weight is greater than the second one. Limak became larger than Bob after two years so you should print2.
In the second sample, Limak's and Bob's weights in next years are: 12 and 18, then 36 and36, and finally108 and72 (after three years). The answer is3. Remember that Limak wants to be larger than Bob and he won't be satisfied with equal weights.
In the third sample, Limak becomes larger than Bob after the first year. Their weights will be3 and2 then.
水题,数据量不大直接模拟,a*=3、b*=2,一大于就跳出,代码如下:
#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
int t=0;
while(a<=b)
{
a*=3,b*=2;
++t;
}
cout<<t<<endl;
return 0;
}
B. Bear and Friendship Condition
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures).
There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves.
Let A-B denote that members A and B are friends. Limak thinks that a network isreasonable if and only if the following condition is satisfied: For every threedistinct members (X,Y,Z), ifX-Y andY-Z then alsoX-Z.
For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well.
Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes.
Input
The first line of the input contain two integers n andm (3 ≤ n ≤ 150 000,) — the number of members and the number of pairs of members that are friends.
The i-th of the next m lines contains two distinct integers ai andbi (1 ≤ ai, bi ≤ n, ai ≠ bi). Members ai andbi are friends with each other. No pair of members will appear more than once in the input.
Output
If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes).
Examples
Input
4 3
1 3
3 4
1 4
Output
YES
Input
4 4
3 1
2 3
3 4
1 2
Output
NO
Input
10 4
4 3
5 10
8 9
1 2
Output
YES
Input
3 2
1 2
2 3
Output
NO
Note
The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members(2, 3) are friends and members (3, 4) are friends, while members(2, 4) are not.
图论的题,数连通子图的个数并记录每个连通子图包含的点数,若满足题中要求,则每个连通子图都为都为完全图:边数为
n*(n-1)/2。把这些边数相加,若最后等于该图总的边数则符合题中条件,否则不满足。一开始没有考虑sum和m的范围直接gg。代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
long long ss=0,n,m;
bool vis[150002];
vector<int> v[150002];
queue<int> q;
void BFS()
{
for(int i=1;i<=n;++i)
{
if(!vis[i]&&v[i].size())
{
while(!q.empty()) q.pop();
vis[i]=true;
q.push(i);
long long sum=1;
while(!q.empty())
{
int t=q.front();
q.pop();
for(int j=0;j<v[t].size();++j)
{
int u=v[t][j];
if(!vis[u])
{
vis[u]=true;
q.push(u);
++sum;
}
}
}
ss=ss+sum*(sum-1)/2;
}
}
}
int main()
{
memset(vis,false,sizeof(vis));
scanf("%I64d %I64d",&n,&m);
for(int i=0;i<m;++i)
{
int x,y;
scanf("%d %d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
BFS();
if(ss==m) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}
C. Bear and Different Names
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
In the army, it isn't easy to form a group of soldiers that will be effective on the battlefield. The communication is crucial and thus no two soldiers should share a name (what would happen if they got an order that Bob is a scouter, if there are two Bobs?).
A group of soldiers is effective if and only if their names are different. For example, a group (John, Bob, Limak) would be effective, while groups (Gary, Bob, Gary) and (Alice, Alice) wouldn't.
You are a spy in the enemy's camp. You noticed n soldiers standing in a row, numbered1 throughn. The general wants to choose a group ofk consecutive soldiers. For everyk consecutive soldiers, the general wrote down whether they would be an effective group or not.
You managed to steal the general's notes, with n - k + 1 stringss1, s2, ..., sn - k + 1, each either "YES" or "NO".
- The string s1 describes a group of soldiers1 throughk ("YES" if the group is effective, and "NO" otherwise).
- The string s2 describes a group of soldiers2 throughk + 1.
- And so on, till the string sn - k + 1 that describes a group of soldiersn - k + 1 throughn.
Your task is to find possible names of n soldiers. Names should match the stolen notes. Each name should be a string that consists of between1 and10 English letters, inclusive. The first letter should be uppercase, and all other letters should be lowercase. Names don't have to be existing names — it's allowed to print "Xyzzzdj" or "T" for example.
Find and print any solution. It can be proved that there always exists at least one solution.
Input
The first line of the input contains two integers n andk (2 ≤ k ≤ n ≤ 50) — the number of soldiers and the size of a group respectively.
The second line contains n - k + 1 stringss1, s2, ..., sn - k + 1. The stringsi is "YES" if the group of soldiersi throughi + k - 1 is effective, and "NO" otherwise.
Output
Find any solution satisfying all given conditions. In one line print n space-separated strings, denoting possible names of soldiers in the order. The first letter of each name should be uppercase, while the other letters should be lowercase. Each name should contain English letters only and has length from1 to 10.
If there are multiple valid solutions, print any of them.
Examples
Input
8 3
NO NO YES YES YES NO
Output
Adam Bob Bob Cpqepqwer Limak Adam Bob Adam
Input
9 8
YES NO
Output
R Q Ccccccccc Ccocc Ccc So Strong Samples Ccc
Input
3 2
NO NO
Output
Na Na Na
Note
In the first sample, there are 8 soldiers. For every3 consecutive ones we know whether they would be an effective group. Let's analyze the provided sample output:
- First three soldiers (i.e. Adam, Bob, Bob) wouldn't be an effective group because there are two Bobs. Indeed, the strings1 is "NO".
- Soldiers 2 through 4 (Bob, Bob, Cpqepqwer) wouldn't be effective either, and the strings2 is "NO".
- Soldiers 3 through 5 (Bob, Cpqepqwer, Limak) would be effective, and the strings3 is "YES".
- ...,
- Soldiers 6 through 8 (Adam, Bob, Adam) wouldn't be effective, and the strings6 is "NO".
就是寻找一个字符串的排列,从左往右每连续的k个串应满足题中的要求。我是从左往右枚举的,先编50个名字:如果是YES,就从第一个没填的空开始填满k个不同的名字;如果是NO,则让该组第1个没填的名字与该组第1个名字相等,后跳入下一组的判断(这样的话,考虑下一组的时候这组的第一个就会被跳过,而不影响后面的决策)。按此规则填满就得到了答案。代码如下:
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 55;
string a[maxn];
string b;
string ans[maxn];
void init()
{
for(int i = 0;i <= maxn; ++i)
{
a[i].insert(0,1,i / 26 + 'A');
a[i].insert(1,1,i % 26 + 'a');
}
}
int main()
{
int n,k;
init();
while(cin >> n >> k)
{
int t = 0;
for(int i = 0;i < k - 1; ++i) ans[i] = a[t++];
for(int i = 0;i < n - k + 1; ++i)
{
cin >> b;
if(b[0] == 'N') ans[i + k - 1] = ans[i];
else if(b[0] == 'Y') ans[i + k - 1] = a[t++];
}
for(int i = 0;i < n; ++i)
{
cout << ans[i];
if(i != n - 1) cout << " ";
}
cout << endl;
}
return 0;
}