D. Lemonade Line
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
It’s a hot summer day out on the farm, and Farmer John is serving lemonade to his N cows! All N cows (conveniently numbered 1…N) like lemonade, but some of them like it more than others. In particular, cow i is willing to wait in a line behind at most wi cows to get her lemonade. Right now all N cows are in the fields, but as soon as Farmer John rings his cowbell, the cows will immediately descend upon FJ’s lemonade stand. They will all arrive before he starts serving lemonade, but no two cows will arrive at the same time. Furthermore, when cow i arrives, she will join the line if and only if there are at most wi cows already in line.
Farmer John wants to prepare some amount of lemonade in advance, but he does not want to be wasteful. The number of cows who join the line might depend on the order in which they arrive. Help him find the minimum possible number of cows who join the line.
Input
The first line contains N, and the second line contains the N space-separated integers w1,w2,…,wN. It is guaranteed that 1≤N≤105, and that 0≤wi≤109 for each cow i.
Output
Print the minimum possible number of cows who might join the line, among all possible orders in which the cows might arrive.
Example
input
5
7 1 400 2 2
output
3
Note
In this setting, only three cows might end up in line (and this is the smallest possible).
Suppose the cows with w=7 and w=400 arrive first and wait in line.
Then the cow with w=1 arrives and turns away, since 2 cows are already in line. The cows with w=2 then arrive, one staying and one turning away.
题目大意:
每头奶牛有自己的wi值, 给n头奶牛排队,前面的一定要比较大,求可能加入生产线的最小奶牛数量。
题目思路:
给奶牛从大到小排序,然后遍历出不同的数之和便是答案。
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#define INF 0x3f3f3f3f
using namespace std;
int n;
int a[100005];
bool cmp(const &b,const &c){
return b>c;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1,cmp);
int num=0;
for(int i=1;i<=n;i++){
if(a[i]>=i-1){
num++;
}
else{
break;
}
}
cout<<num<<endl;
}
F. Out of Sorts II
题目链接:http://codeforces.com/group/NVaJtLaLjS/contest/238202/problem/F
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Keeping an eye on long term career possibilities beyond the farm, Bessie the cow has started learning algorithms from various on-line coding websites.
Her favorite algorithm thus far is “bubble sort”. Here is Bessie’s initial implementation, in cow-code, for sorting an array A of length N.
sorted = false
while (not sorted):
sorted = true
moo
for i = 0 to N-2:
if A[i+1] < A[i]:
swap A[i], A[i+1]
sorted = false
Apparently, the “moo” command in cow-code does nothing more than print out “moo”. Strangely, Bessie seems to insist on including it at various points in her code.
After testing her code on several arrays, Bessie learns an interesting observation: while large elements can be pulled to the end of the array very quickly, it can take small elements a very long time to “bubble” to the front of the array (she suspects this is how the algorithm gets its name). In order to try and alleviate this problem, Bessie tries to modify her code so that it scans forward and then backward in each iteration of the main loop, so that both large and small elements have a chance to be pulled long distances in each iteration of the main loop. Her code now looks like this:
sorted = false
while (not sorted):
sorted = true
moo
for i = 0 to N-2:
if A[i+1] < A[i]:
swap A[i], A[i+1]
for i = N-2 downto 0:
if A[i+1] < A[i]:
swap A[i], A[i+1]
for i = 0 to N-2:
if A[i+1] < A[i]
sorted = false
Given an input array, please predict how many times “moo” will be printed by Bessie’s modified code.
Input
The first line of input contains N (1≤N≤100,000). The next N lines describe A[0]…A[N−1], each being an integer in the range 0…109. Input elements are not guaranteed to be distinct.
Output
Print the number of times “moo” is printed.
Example
input
5
1
8
5
3
2
output
2
题目大意:
和上面那题不同的是,这题的排序要同时进行上下冒泡,当时想了好久也没能想出来Orz。结果得用到树状数组。QAQ
可以这样想,每一次向后的冒泡就是把一个大数扔到能到达的最后,向前的冒泡就是把一个小数扔到到达的最前;
我们设个i,我们把数组分为前后两部分,使得前面部分比i大的大数和后面部分比标i的小数最多;想到这种相对于标杆的大小数都是成对出现的,那么每进行一次冒泡,这两个区间就会有一对大小数会对换区间。当区间所有的大小数交换完成时,那序列便排好序了;(其实我也不太懂。)
那么我们只需要从第一个数字开始往后移动这个i,同时用树状数组逐个保存数字的位置大小查询大数数量并记录其最大值就可以了。
最后注意一下即使序列一开始便有序也需要输出1;
代码:
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#define INF 0x3f3f3f3f
using namespace std;
int n,sum[100006];//这里不能开100005,不然会wa
struct node{
int id,x;
}a[100006];
bool cmp(const node&b,const node&c){
if(b.x!=c.x)return b.x<c.x;
else return b.id<c.id;
}
void add(int x){
for(int i=x;i<=100006;i+=i&-i)//这里不能也不能是100005,不然会wa
sum[i]++;
}
int querry(int x){
int ans=0;
while(x){
ans+=sum[x];
x-=x&-x;
}
return ans;
}
int main(){
int ans=1;
cin>>n;
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++){
cin>>a[i].x;
a[i].id=i;
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
add(a[i].id);
ans=max(ans,i-querry(i));
}
cout<<ans<<endl;
}
C. Team Tic Tac Toe
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Farmer John owns 26 cows, which by happenstance all have names starting with different letters of the alphabet, so Farmer John typically refers to each cow using her first initial – a character in the range A…Z.
The cows have recently become fascinated by the game of tic-tac-toe, but since they don’t like the fact that only two cows can play at a time, they have invented a variant where multiple cows can play at once! Just like with regular tic-tac-toe, the game is played on a 3×3 board, only instead of just Xs and Os, each square is marked with a single character in the range A…Z to indicate the initial of the cow who claims that square.
An example of a gameboard might be:
COW
XXO
ABC
The cows fill in each of the nine squares before they become confused about how to figure out who has won the game. Clearly, just like with regular tic-tac-toe, if any single cow has claimed an entire row, column, or diagonal, that cow could claim victory by herself. However, since the cows think this might not be likely given the larger number of players, they decide to allow cows to form teams of two, where a team of two cows can claim victory if any row, column, or diagonal consists only of characters belonging to the two cows on the team, and moreover if characters from both cows (not just one) are used in this row, column, or diagonal.
Please help the cows figure out how many individuals or two-cow teams can claim victory. Note that the same square on the game board might possibly be usable in several different claims to victory.
Input
The input consists of three lines, each of which is three characters in the range A…Z.
Output
Output should consist of two lines. On the first line, output the number of individual cows who can claim victory. On the second line, output the number of two-cow teams that could claim victory.
Example
input
COW
XXO
ABC
output
0
2
Note
In this example, no single cow can claim victory. However, if cows C and X team up, they can win via the C-X-C diagonal. Also, if cows X and O team up, they can win via the middle row.
题目大意:
有a…z24个字母奶牛,排在3*3的方阵里,按行列叉组队,然后输出由两头牛组队的队伍和三头牛组队的队伍。
题目思路:
我模拟一下方阵和可能出现的8种情况,然后数一下个数,记得去重。
代码:
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#define INF 0x3f3f3f3f
using namespace std;
char Dis[5][5];
int dis[5][5];
int ans[10];
int vis[1000];
int k[1000];
int slove(int a,int b,int c,int i){
int num=0;
if(a==b&&b==c) {num=1;k[i]=a;}
if(a==b&&b!=c) {num=2;k[i]=b+c;}
if(a==c&&b!=c) {num=2;k[i]=b+c;}
if(c==b&&a!=c) {num=2;k[i]=a+c;}
if(c!=a&&a!=b&&b!=c) {num=3;k[i]=a+c+b;}
return num;
}
int main(){
for(int i=0;i<3;i++){
scanf("%s",Dis[i]);
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
dis[i][j]=Dis[i][j]-'a';
}
}
ans[1]=slove(dis[0][0],dis[0][1],dis[0][2],1);
ans[2]=slove(dis[1][0],dis[1][1],dis[1][2],2);
ans[3]=slove(dis[2][0],dis[2][1],dis[2][2],3);
ans[4]=slove(dis[0][0],dis[1][0],dis[2][0],4);
ans[5]=slove(dis[0][1],dis[1][1],dis[2][1],5);
ans[6]=slove(dis[0][2],dis[1][2],dis[2][2],6);
ans[7]=slove(dis[0][0],dis[1][1],dis[2][2],7);
ans[8]=slove(dis[0][2],dis[1][1],dis[2][0],8);
int num1=0,num2=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=8;i++){
if(ans[i]==1&&vis[k[i]]==0) {num1++;vis[k[i]]=1;}
}
memset(vis,0,sizeof(vis));
for(int i=1;i<=8;i++){
if(ans[i]==2&&vis[k[i]]==0) {num2++;vis[k[i]]=1;}
}
cout<<num1<<endl;
cout<<num2<<endl;
}
E. Family Tree
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Farmer John owns a family-run farm that has been passed down over several generations, with a herd of cows whose familial roots can similarly be traced back several generations on the same farm. By examining old records, Farmer John is curious how the cows in his current herd are related to each-other. Please help him in this endeavor!
Input
The first line of input contains N (1≤N≤100) followed by the names of two cows. Cow names are each strings of at most 10 uppercase letters (A…Z). Farmer John is curious about the relationship between the two cows on this line of input.
The next N lines each contain two cow names X and Y, indicating that X is the mother of Y.
Output
You should print one line of output indicating the relationship between the two cows specified on the first line of input (for simplicity, let’s call these two cows BESSIE and ELSIE for the examples below). Here are the different types of relationships that are possible:
You should output “SIBLINGS” if BESSIE and ELSIE have the same mother.
BESSIE might be a direct descendant of ELSIE, meaning that ELSIE is either the mother, grand-mother, great-grand-mother, great-great-grand-mother, etc., of BESSIE. If this is the case, you should print “ELSIE is the (relation) of BESSIE”, where (relation) is the appropriate relationship, for example “great-great-grand-mother”.
If ELSIE is a child of an ancestor of BESSIE (and ELSIE is not herself an ancestor or sister of BESSIE), then ELSIE is BESSIE’s aunt. You should output “ELSIE is the aunt of BESSIE” if ELSIE is a child of BESSIE’s grand-mother, “ELSIE is the great-aunt of BESSIE” if ELSIE is a child of BESSIE’s great-grand-mother, “ELSIE is the great-great-aunt of BESSIE” if ELSIE is a child of BESSIE’s great-great-grand-mother, and so on.
If BESSIE and ELSIE are related by any other means (i.e., if they share a common ancestor), they are cousins, and you should simply output “COUSINS”.
You should output “NOT RELATED” if BESSIE and ELSIE have no common ancestor, or neither is directly descended from the other.
The following diagram helps illustrate the relationships above, which are the only relationship types you need to consider.
Observe that some relationships like “niece” (daughter of sister) are not necessary since if BESSIE is the niece of ELSIE, then ELSIE is BESSIE’s aunt.
Example
input
7 AA BB
MOTHER AA
GGMOTHER BB
MOTHER SISTER
GMOTHER MOTHER
GMOTHER AUNT
AUNT COUSIN
GGMOTHER GMOTHER
output
BB is the great-aunt of AA
题目大意:
就像建一棵家庭树,然后给你成员的关系,问你其中两个成员的关系。我比赛的时候以为得建一棵树去做,结果它的数据很小,可以通过模拟解出来。建个邻接表。
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#define INF 0x3f3f3f3f
using namespace std;
#define maxn 105
string f[maxn],cow[maxn];
int n;
string Find(string a){
for(int i=1;i<=n;i++){
if(a==cow[i]) return f[i];
}
return "";
}
int is(string a,string b){
int cnt=0;
while(b!=""){
if(a==b) return cnt;
b=Find(b);
cnt++;
}
return -1;
}
int main(){
string a,b;
cin>>n>>a>>b;
for(int i=1;i<=n;i++){
cin>>f[i]>>cow[i];
}
string now=a;
int cnta=0;
while(now!=""){
if(is(now,b)!=-1)
break;
now=Find(now);
cnta++;
}
if(now==""){
cout<<"NOT RELATED"<<endl;
return 0;
}
int cntb=is(now,b);
if(cnta==cntb&&cnta==1){
cout<<"SIBLINGS"<<endl;
return 0;
}
else if(cnta>1&&cntb>1){
cout<<"COUSINS"<<endl;
return 0;
}
else{
if(cnta<cntb){
swap(a,b);
swap(cnta,cntb);
}
cout<<b<<" is the ";
for(int i=0;i<cnta-2;i++){
cout<<"great-"; }
if(cnta>1&&cntb==0)
cout<<"grand-";
if(cntb==0)
cout<<"mother";
else
cout<<"aunt";
cout<<" of "<<a<<endl;
}
return 0;
}
A. Out of Sorts
题目链接:http://codeforces.com/group/NVaJtLaLjS/contest/238202/problem/A
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Keeping an eye on long term career possibilities beyond the farm, Bessie the cow has started learning algorithms from various on-line coding websites.
Her favorite algorithm thus far is “bubble sort”. Here is Bessie’s implementation, in cow-code, for sorting an array A of length N.
sorted = false
while (not sorted):
sorted = true
moo
for i = 0 to N-2:
if A[i+1] < A[i]:
swap A[i], A[i+1]
sorted = false
Apparently, the “moo” command in cow-code does nothing more than print out “moo”. Strangely, Bessie seems to insist on including it at various points in her code.
Given an input array, please predict how many times “moo” will be printed by Bessie’s code.
Input
The first line of input contains N (1≤N≤100,000). The next N lines describe A[0]…A[N−1], each being an integer in the range 0…109. Input elements are not guaranteed to be distinct.
Output
Print the number of times “moo” is printed.
Example
input
5
1
5
3
8
2
output
4
题目大意:
给你一串数字,通过用冒泡排序的方法,求一个需要经过几个循环。
题目思路:
重新建一个数组,给这个数组排一下序,这里sort得注意一下稳定排序,然后对于原来的数组从第一个数到第n个数,记下距离原位置最远得距离,即是答案。
代码:
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#define INF 0x3f3f3f3f
using namespace std;
struct node{
long long id,x;
}a[100005];
bool cmp(const node&b,const node&c){
if(b.x!=c.x)return b.x<c.x;
else return b.id<c.id;
}
int n;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].x;
a[i].id=i;
}
sort(a+1,a+n+1,cmp);
long long maxn=-1,ans=0,flag=0;
for(int i=1;i<=n;i++){
if(a[i].id>i){
maxn=max(abs(a[i].id-i),maxn);flag=1;
}
if(a[i].id<i){
ans+=1;flag=1;
}
}
maxn=max(ans,maxn);
if(flag==0) maxn=0;
cout<<maxn+1<<endl;
}