qdu_组队训练(ABCFIJK)

A - Second-price Auction

Do you know second-price auction? It's very simple but famous. In a second-price auction, each potential buyer privately submits, perhaps in a sealed envelope or over a secure connection, his (or her) bid for the object to the auctioneer. After receiving all the bids, the auctioneer then awards the object to the bidder with the highest bid, and charges him (or her) the amount of the second-highest bid.

Suppose you're the auctioneer and you have received all the bids, you should decide the winner and the amount of money he (or she) should pay.

Input

There are multiple test cases. The first line of input contains an integer T(T <= 100), indicating the number of test cases. Then T test cases follow.

Each test case contains two lines: The first line of each test case contains only one integer N, indicating the number of bidders. (2 <= N <= 100) The second line of each test case contains N integers separated by a space. The i-th integer Piindicates the i-th bidder's bid. (0 < Pi <= 60000) You may assume that the highest bid is unique.

Output

For each test case, output a line containing two integers x and y separated by a space. It indicates that the x-th bidder is the winner and the amount of money he (or she) should pay is y.

Sample Input

2
3
3 2 1
2
4 9

Sample Output

1 2
2 4

水题,上代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>

const int maxn=1e5+5;
typedef long long ll;
using namespace std;
int a[maxn];
int b[maxn];
int main()
{
	int T;
	while(cin>>T)
	{
		while(T--)
		{
			int n;
			cin>>n;
			for(int t=0;t<n;t++)
			{
				scanf("%d",&a[t]);
				b[t]=a[t];
			}
			sort(a,a+n);
			int k;
			for(int t=0;t<n;t++)
			{
				if(b[t]==a[n-1])
				{
					k=t+1;
				}
			}
			cout<<k<<" "<<a[n-2]<<endl;
		}
	}
	return 0;
}

B - Light Bulb

Compared to wildleopard's wealthiness, his brother mildleopard is rather poor. His house is narrow and he has only one light bulb in his house. Every night, he is wandering in his incommodious house, thinking of how to earn more money. One day, he found that the length of his shadow was changing from time to time while walking between the light bulb and the wall of his house. A sudden thought ran through his mind and he wanted to know the maximum length of his shadow.

Input

The first line of the input contains an integer T (T <= 100), indicating the number of cases.

Each test case contains three real numbers Hh and D in one line. H is the height of the light bulb while h is the height of mildleopard. D is distance between the light bulb and the wall. All numbers are in range from 10-2 to 103, both inclusive, and H - h >= 10-2.

Output

For each test case, output the maximum length of mildleopard's shadow in one line, accurate up to three decimal places..

Sample Input

3
2 1 0.5
2 0.5 3
4 3 4

Sample Output

1.000
0.750
4.000

三分  附队友代码

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
double H,h,D;
double fun(double x)
{
	return 1.0*h/(1.0*x*(H-h)/(D*h-H*x)+1)+1.0*x;
}
double sanfen(double l,double r)
{
	if(r-l<0.00001)
		return fun(l);
	double mid=(l+l+r)/3;
	double midd=(l+r+r)/3;
	double a=fun(mid);
	double b=fun(midd);
	if(a>b)
		return sanfen(l,midd);
	else
		return sanfen(mid,r);
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%lf%lf%lf",&H,&h,&D);
		double tt=1.0*D*h/H;
		printf("%.3f\n",sanfen(0,tt));
	}
}

C - Connect them

You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are two-way (that is connecting computers i and j is the same as connecting computers j and i). The cost of connecting computer i and computer j is cij. You cannot connect some pairs of computers due to some particular reasons. You want to connect them so that every computer connects to any other one directly or indirectly and you also want to pay as little as possible.

Given n and each cij , find the cheapest way to connect computers.

Input

There are multiple test cases. The first line of input contains an integer T (T <= 100), indicating the number of test cases. Then T test cases follow.

The first line of each test case contains an integer n (1 < n <= 100). Then n lines follow, each of which contains n integers separated by a space. The j-th integer of the i-th line in these n lines is cij, indicating the cost of connecting computers iand j (cij = 0 means that you cannot connect them). 0 <= cij <= 60000, cij = cjicii= 0, 1 <= ij <= n.

Output

For each test case, if you can connect the computers together, output the method in in the following fomat:

ijij1 ......

where ik ik (k >= 1) are the identification numbers of the two computers to be connected. All the integers must be separated by a space and there must be no extra space at the end of the line. If there are multiple solutions, output the lexicographically smallest one (see hints for the definition of "lexicography small") If you cannot connect them, just output "-1" in the line.

Sample Input

2
3
0 2 3
2 0 5
3 5 0
2
0 0
0 0

Sample Output

1 2 1 3
-1

Hint

s:
A solution A is a line of p integers: a1a2, ...ap.
Another solution B different from A is a line of q integers: b1b2, ...bq.
A is lexicographically smaller than B if and only if:
(1) there exists a positive integer r (r <= pr <= q) such that ai = bi for all 0 < i < r and ar < br 
OR
(2) p < q and ai = bi for all 0 < i <= p

最小生成树

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>

const int maxn=1e2+10;
typedef long long ll;

using namespace std;
int pre[maxn];
struct node
{
	int x,y;
	int sum;
}p[1000005];
struct node1
{
 int x,y;	
}mp[1000005];

int find(int  x)
{
	if(pre[x]==x)
	{
		return x;
	}
	else
	{
		return pre[x]=find(pre[x]);
	}
}
bool merge(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		pre[fx]=fy;
		return true;
	}
	else
	{
		return false;
	}
}
bool cmp(node x1,node y1)
{
	if(x1.sum!=y1.sum) 
	return x1.sum<y1.sum;
	else
	{
		if(x1.x!=y1.x)
		{
			return x1.x<y1.x;
		}
		else
		{
			return x1.y<y1.y;
		}
	}
}
bool cmp1( node1 a, node1 b){
	if(a.x == b.x){
		return a.y < b.y;
	}
	else
	return a.x < b.x;
}


int main()
{
	int T;
	
	cin>>T;
		while(T--)
		{
			int n;
			cin>>n;
			for(int t=1;t<=n;t++)
			{
				pre[t]=t;
			}
			int cnt=0;
			for(int t=1;t<=n;t++)
			{
			    for(int j=1;j<=n;j++)
			    {
			    	int x;
			    	scanf("%d",&x);
			    	if(x!=0&&t<j)
			    	{
			    	p[cnt].x=t;
			    	p[cnt].y=j;
			    	p[cnt++].sum=x;
			        }
			    	
				}
			}
			sort(p,p+cnt,cmp);
			int cc=0;
			for(int t=0;t<cnt;t++)
			{
				if(cc==n-1)
				{
					break;
				}
				if(merge(p[t].x,p[t].y))
				{
					mp[cc].x=p[t].x;
					mp[cc].y=p[t].y;
					cc++;
					
				}
			}
			if(cc!=n-1)
			{
				cout<<"-1"<<endl;
				continue;
			}
			sort(mp,mp+cc,cmp1);
			for(int t=0;t<cc;t++)
			{
				if(t==0)
				cout<<mp[t].x<<" "<<mp[t].y;
				else
				{
					cout<<" "<<mp[t].x<<" "<<mp[t].y;
				}
			}
			cout<<endl;
			
		}
	
	return 0;
}
 

F - 80ers' Memory

I guess most of us are so called 80ers, which means that we were born in the 1980's. This group of people shared a lot of common memories. For example, the Saint Seiya, the YoYo ball, the Super Mario, and so on. Do you still remember these?

Input

There will be ONLY ONE test case.

The test case begins with a positive integer N, (N < 100).
Then there will be N lines each containing a single word describing a keyword of the typical 80ers' memories. Each word consists of letters, [a-zA-Z], numbers, [0-9], and the underline, '_'. The length of each word would be no more than 20.
Then one line follows with a positive integer K, (K < 100).
The last part of the test case will be K lines. The i-th line contains the keywords given by the i-th person and starts with a positive integer Ni. Then there will be Ni words separated by white spaces. Each of these words has no more than 20 characters.
All the words are case sensitive.

Output

For each of these K people, you are asked to count the number of typical 80ers' keywords on his/her list and output it in a single line.

Sample Input

4
Saint_Seiya
YoYo_ball
Super_Mario
HuLuWa
3
2 Saint_Seiya TiaoFangZi
1 KTV
3 HuLuWa YOYO_BALL YoYo_ball

Sample Output

1
0
2

map

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>

const int maxn=1e5+5;
typedef long long ll;
using namespace std;
int a[maxn];
int b[maxn];
char str[1005][1005];
map<string,int>mp;
int main()
{
	int n;
	cin>>n;
	getchar();
	for(int t=1;t<=n;t++)
	{
		scanf("%s",&str[t]);
		mp[str[t]]=t;
	}
	int k;
	cin>>k;
	for(int t=0;t<k;t++)
	{
		int m;
		cin>>m;
		int s=0;
		getchar();
		char str2[105];
		for(int j=0;j<m;j++)
		{
			cin>>str2;
			if(mp[str2]!=0)
			{
				s++;
			}
		}
		cout<<s<<endl;
	}
	
	return 0;
}

I - A Stack or A Queue?

Do you know stack and queue? They're both important data structures. A stack is a "first in last out" (FILO) data structure and a queue is a "first in first out" (FIFO) one.

Here comes the problem: given the order of some integers (it is assumed that the stack and queue are both for integers) going into the structure and coming out of it, please guess what kind of data structure it could be - stack or queue?

Notice that here we assume that none of the integers are popped out before all the integers are pushed into the structure.

Input

There are multiple test cases. The first line of input contains an integer T (T <= 100), indicating the number of test cases. Then T test cases follow.

Each test case contains 3 lines: The first line of each test case contains only one integer N indicating the number of integers (1 <= N <= 100). The second line of each test case contains N integers separated by a space, which are given in the order of going into the structure (that is, the first one is the earliest going in). The third line of each test case also contains N integers separated by a space, whick are given in the order of coming out of the structure (the first one is the earliest coming out).

Output

For each test case, output your guess in a single line. If the structure can only be a stack, output "stack"; or if the structure can only be a queue, output "queue"; otherwise if the structure can be either a stack or a queue, output "both", or else otherwise output "neither".

Sample Input

4
3
1 2 3
3 2 1
3
1 2 3
1 2 3
3
1 2 1
1 2 1
3
1 2 3
2 3 1

Sample Output

stack
queue
both
neither

模拟

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>

const int maxn=1e5+5;
typedef long long ll;
using namespace std;
int a[maxn];
int b[maxn];
int main()
{
	int T;
    queue<int>q;
    stack<int>s;
	while(cin>>T)
	{
		while(T--)
		{
		
		int n;
		cin>>n;
		for(int t=0;t<n;t++)
		{
			scanf("%d",&a[t]);
			q.push(a[t]);
			s.push(a[t]);
		}
		for(int t=0;t<n;t++)
		{
			scanf("%d",&b[t]);

		}
		int flag1=0,flag2=0;
		for(int t=0;t<n;t++)
		{
			if(b[t]!=q.front())
			{
				flag2=1;
			}
			if(b[t]!=s.top())
			{
				flag1=1;
			}
			q.pop();
			s.pop();
		}
		if(flag1==1&&flag2==1)
		{
			cout<<"neither"<<endl;
		}
		else if(flag1==1&&flag2==0)
		{
			cout<<"queue"<<endl;
		}
		else if(flag1==0&&flag2==1)
		{
			cout<<"stack"<<endl;
		}
		else
		{
			cout<<"both"<<endl;
		}
	
	
	}
	}
	return 0;
}

J - Dream City

JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are ntrees in the yard. Let's call them tree 1, tree 2 ...and tree n. At the first day, each tree i has ai coins on it (i=1, 2, 3...n). Surprisingly, each tree i can grow bi new coins each day if it is not cut down. From the first day, JAVAMAN can choose to cut down one tree each day to get all the coins on it. Since he can stay in the Dream City for at most m days, he can cut down at most m trees in all and if he decides not to cut one day, he cannot cut any trees later. (In other words, he can only cut down trees for consecutive m or less days from the first day!)

Given nmai and bi (i=1, 2, 3...n), calculate the maximum number of gold coins JAVAMAN can get.

Input

There are multiple test cases. The first line of input contains an integer T (T <= 200) indicates the number of test cases. Then T test cases follow.

Each test case contains 3 lines: The first line of each test case contains 2 positive integers n and m (0 < m <= n <= 250) separated by a space. The second line of each test case contains n positive integers separated by a space, indicating ai. (0 < ai <= 100, i=1, 2, 3...n) The third line of each test case also contains npositive integers separated by a space, indicating bi. (0 < bi <= 100, i=1, 2, 3...n)

Output

For each test case, output the result in a single line.

Sample Input

2
2 1
10 10
1 1
2 2
8 10
2 3

Sample Output

10
21

Hint

s:
Test case 1: JAVAMAN just cut tree 1 to get 10 gold coins at the first day.
Test case 2: JAVAMAN cut tree 1 at the first day and tree 2 at the second day to get 8 + 10 + 3 = 21 gold coins in all.

dp,要让效益好的往后排,如果相同就数值大的在前面

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>

const int maxn=300;
typedef long long ll;
using namespace std;

int dp[maxn][maxn];
struct node
{
	int val,speed;
}p[1005];
bool cmp(node x,node y)
{
	if(x.speed!=y.speed)
	{
		return x.speed<y.speed;
	}
	else
	{
		return x.val>y.val;
	}
}
int main()
{
	int T;
	while(cin>>T)
	{
	while(T--)
	{
		int n,m;
		memset(dp,0,sizeof(dp));
		cin>>n>>m;
		for(int t=1;t<=n;t++)
		{
			scanf("%d",&p[t].val);
		}
		for(int j=1;j<=n;j++)
		{
			scanf("%d",&p[j].speed);
		}
		sort(p+1,p+n+1,cmp);
	    for(int t=1;t<=n;t++)
	    {
	    	for(int j=1;j<=m;j++)
			{
				dp[t][j]=max(dp[t-1][j],dp[t-1][j-1]+p[t].speed*(j-1)+p[t].val);
			}
		}
		cout<<dp[n][m]<<endl;
	}
    }
	return 0;
}

K - K-Nice

This is a super simple problem. The description is simple, the solution is simple. If you believe so, just read it on. Or if you don't, just pretend that you can't see this one.

We say an element is inside a matrix if it has four neighboring elements in the matrix (Those at the corner have two and on the edge have three). An element inside a matrix is called "nice" when its value equals the sum of its four neighbors. A matrix is called "k-nice" if and only if k of the elements inside the matrix are "nice".

Now given the size of the matrix and the value of k, you are to output any one of the "k-nice" matrix of the given size. It is guaranteed that there is always a solution to every test case.

Input

The first line of the input contains an integer T (1 <= T <= 8500) followed by Ttest cases. Each case contains three integers nmk (2 <= nm <= 15, 0 <= k <= (n- 2) * (m - 2)) indicating the matrix size n * m and it the "nice"-degree k.

Output

For each test case, output a matrix with n lines each containing m elements separated by a space (no extra space at the end of the line). The absolute value of the elements in the matrix should not be greater than 10000.

Sample Input

2
4 5 3
5 5 3

Sample Output

2 1 3 1 1
4 8 2 6 1
1 1 9 2 9
2 2 4 4 3
0 1 2 3 0
0 4 5 6 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

思维,初始化全是0,其余都填数,每天一个减少一个符合的

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>

const int maxn=30;
typedef long long ll;

int Map[maxn][maxn];
using namespace std;

int main()
{
	
	int T;
	while(cin>>T)
	{
		while(T--)
		{
			memset(Map,0,sizeof(Map));
			int n,m,k;
			cin>>n>>m>>k;
			int s=(n-2)*(m-2);
			int cnt=2;
			int flag=0;
			for(int t=0;t<n&&flag==0;t++)
			{
				for(int j=0;j<m;j++)
				{
					if(t==n-1||j==0||j==m-1)
					{
						continue;
					}
					if(s==k)
					{
						flag=1;
						break;
					}
					Map[t][j]=cnt++;
					s--;
					
				}
			}
			for(int t=0;t<n;t++)
			{
				for(int j=0;j<m;j++)
				{
					if(j==0)
					{
						printf("%d",Map[t][j]);
					}
					else
					{
						printf(" %d",Map[t][j]);
					}
				}
				printf("\n");
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lbperfect123/article/details/88354296