BUAA-SCSE Training day3 5 6 7

Highways
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18400 Accepted: 8543

Description

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system. 

Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. 

The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.

Input

The first line of input is an integer T, which tells how many test cases followed. 
The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.

Output

For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.

Sample Input

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

Hint

Huge input,scanf is recommended.

Source

POJ Contest,Author:Mathematica@ZSU

唯一一道会做的mst...
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
int n;
int map[505][505];
int visit[505];
int dis[505];
void getmap()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cin>>map[i][j];
		}
	}
}
void prim()
{
	int k=1;
	int ans=0;
	memset(visit,0,sizeof(visit));
	memset(dis,INF,sizeof(dis));
	for(int i=1;i<=n;i++)
	{
		dis[i]=map[1][i];
	}
	visit[1]=1;
	for(int i=1;i<n;i++)
	{
		int MIN=INF;
		for(int j=1;j<=n;j++)
		{
			if(!visit[j]&&dis[j]<MIN)
			{
				MIN=dis[j];
				k=j;
			}
		}
		if(MIN==INF)
			break;
		visit[k]=1;
		ans+=MIN;
		for(int j=1;j<=n;j++)
		{
			if(!visit[j])
			{
				dis[j]=min(dis[j],map[k][j]);
			}
		}
	}
	cout<<ans<<endl;
}
	
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n;
		getmap();
		prim();
	}
	system("pause");
	return 0;
}

最短路

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20977    Accepted Submission(s): 8965


Problem Description
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?

 

Input
输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。
 

Output
对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间
 

Sample Input
   
   
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
 

Sample Output
   
   
3 2
 

Source
 

Recommend
lcy
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
int n;
int map[105][105];
int visit[105];
int dis[105];
int m;
void getmap()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			map[i][j]=i==j?0:INF;
		}
	}
	for(int i=0;i<m;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		map[a][b]=map[b][a]=min(c,map[a][b]);
	}
}
void dijsktra()
{
	int k=1;
	memset(visit,0,sizeof(visit));
	memset(dis,0,sizeof(dis));
	for(int i=1;i<=n;i++)
	{
		dis[i]=map[1][i];
	}
	for(int i=0;i<n;i++)
	{
		int MIN=INF;
		for(int j=1;j<=n;j++)
		{
			if(!visit[j]&&dis[j]<MIN)
			{
				MIN=dis[j];
				k=j;
			}
		}
		if(MIN==INF)
			break;
		visit[k]=1;
		for(int j=1;j<=n;j++)
		{
			if(!visit[j])
			{
				dis[j]=min(dis[j],dis[k]+map[k][j]);
			}
		}
	}
	cout<<dis[n]<<endl;
}
int main()
{
	while(cin>>n>>m,n!=0&&m!=0)
	{
		getmap();
		dijsktra();
	}
	return 0;
}

B - Dijkstra?
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n.

Input

The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form aibi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints and wi is the length of the edge.

It is possible that the graph has loops and multiple edges between pair of vertices.

Output

Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.

Sample Input

Input
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
Output
1 4 3 5 
Input
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
Output
1 4 3 5 

TT..不会。。。
C - Tram
Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

Description

Tram network in Zagreb consists of a number of intersections and rails connecting some of them. In every intersection there is a switch pointing to the one of the rails going out of the intersection. When the tram enters the intersection it can leave only in the direction the switch is pointing. If the driver wants to go some other way, he/she has to manually change the switch. 

When a driver has do drive from intersection A to the intersection B he/she tries to choose the route that will minimize the number of times he/she will have to change the switches manually. 

Write a program that will calculate the minimal number of switch changes necessary to travel from intersection A to intersection B. 

Input

The first line of the input contains integers N, A and B, separated by a single blank character, 2 <= N <= 100, 1 <= A, B <= N, N is the number of intersections in the network, and intersections are numbered from 1 to N. 

Each of the following N lines contain a sequence of integers separated by a single blank character. First number in the i-th line, Ki (0 <= Ki <= N-1), represents the number of rails going out of the i-th intersection. Next Ki numbers represents the intersections directly connected to the i-th intersection.Switch in the i-th intersection is initially pointing in the direction of the first intersection listed. 

Output

The first and only line of the output should contain the target minimal number. If there is no route from A to B the line should contain the integer "-1".

Sample Input

3 2 1
2 2 3
2 3 1
2 1 2

Sample Output

0

关键是构图
#include<iostream>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
int map[105][105];
int visit[105];
int dis[105];
int n,s,e;
void getmap()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			map[i][j]=i==j?0:INF;
		}
	}
	for(int i=1;i<=n;i++)
	{
		int sum;
		cin>>sum;
		for(int j=0;j<sum;j++)
		{
			int x;
			cin>>x;
			if(j)
			{
				map[i][x]=1;
			}
			else
			{
				map[i][x]=0;
			}
		}
	}
}
void dijsktra()
{
	int k=1;
	memset(visit,0,sizeof(visit));
	memset(dis,0,sizeof(dis));
	for(int i=1;i<=n;i++)
	{
		dis[i]=map[s][i];
	}
	visit[s]=1;
	for(int i=0;i<n;i++)
	{
		int MIN=INF;
		for(int j=1;j<=n;j++)
		{
			if(!visit[j]&&dis[j]<MIN)
			{
				MIN=dis[j];
				k=j;
			}
		}
		if(MIN==INF)
			break;
		visit[k]=1;
		for(int j=1;j<=n;j++)
		{
			if(!visit[j])
			{
				dis[j]=min(dis[j],dis[k]+map[k][j]);
			}
		}
	}
	if(dis[e]==INF)
	{
		cout<<"-1"<<endl;
	}
	else
	{
		cout<<dis[e]<<endl;
	}
}
int main()
{
	cin>>n>>s>>e;
	getmap();
	dijsktra();
	return 0;
}

D - Greg and Graph
Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game:

  • The game consists of n steps.
  • On the i-th step Greg removes vertex number xi from the graph. As Greg removes a vertex, he also removes all the edges that go in and out of this vertex.
  • Before executing each step, Greg wants to know the sum of lengths of the shortest paths between all pairs of the remaining vertices. The shortest path can go through any remaining vertex. In other words, if we assume that d(i, v, u) is the shortest path between vertices v and u in the graph that formed before deleting vertex xi, then Greg wants to know the value of the following sum: .

Help Greg, print the value of the required sum before each step.

Input

The first line contains integer n (1 ≤ n ≤ 500) — the number of vertices in the graph.

Next n lines contain n integers each — the graph adjacency matrix: the j-th number in the i-th line aij (1 ≤ aij ≤ 105, aii = 0)represents the weight of the edge that goes from vertex i to vertex j.

The next line contains n distinct integers: x1, x2, ..., xn (1 ≤ xi ≤ n) — the vertices that Greg deletes.

Output

Print n integers — the i-th number equals the required sum before the i-th step.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cincout streams of the %I64dspecifier.

Sample Input

Input
1
0
1
Output
0 
Input
2
0 5
4 0
1 2
Output
9 0 
Input
4
0 3 1 1
6 0 400 1
2 4 0 1
1 1 1 0
4 1 2 3
Output
17 23 404 0 

反向floyd。。
其实不太懂。。
#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define MAXN 505
int n;
long long int sum[MAXN];
int map[MAXN][MAXN];
int del[MAXN];
using namespace std;
void floyd()
{
	for(int k=n-1;k>=0;--k)
    {
        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=n;++j)
            {
                map[i][j]=min(map[i][del[k]]+map[del[k]][j],map[i][j]);
            }
        }
        for(int i=n-1;i>=k;i--)
        {
            for(int j=n-1;j>=k;j--)
            {
                sum[k]+=map[del[i]][del[j]];
			}
		}
	}
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>map[i][j];
        }
    }
    for(int i=0;i<n;i++)
    {
        cin>>del[i];
        sum[i]=0;
    }
    floyd();
    for(int i=0;i<n;i++)
    {
        if(i!=0)
			cout<<" ";
        cout<<sum[i];
    }
    cout<<endl;
    return 0;
}

A - 签到
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Calculate  A + B.
 

Input

Each line will contain two integers  A and  B. Process to end of file.
 

Output

For each case, output  A + B in one line.
 

Sample Input

        
        
1 1
 

Sample Output

        
        
2
 

#include<iostream>
using namespace std;
int main()
{
	int a,b;
	while(cin>>a>>b)
	{
		cout<<a+b<<endl;
	}
	return 0;
}

B - Sudoku Checker
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

The puzzle game of Sudoku is played on a board of N2 × N2 cells. The cells are grouped in N × N squares of N × N cells each. Each cell is either empty or contains a number between 1 and N2.

The sudoku position is correct when numbers in each row, each column and each square are different. The goal of the game is, starting from some correct position, fill all empty cells so that the final position is still correct.

This game is fairly popular in the Internet, and there are many sites which allow visitors to solve puzzles online. Such sites always have a subroutine to determine a correctness of a given position.

You are to write such a routin.

Input

Input file contains integer N, followed by N4 integers — sudoku position. Empty cells are denoted by zeroes.

Constraints

1 ≤ N ≤ 10.

Output

Output file must contain a single string 'CORRECT' or 'INCORRECT'.
 

Sample Input

Sample input 1
2
0 0 0 0
0 0 0 0
0 0 2 0
0 0 0 1
Sample input 2
2
2 1 3 0
3 2 4 0
1 3 2 4
0 0 0 1

Sample Output

Sample output 1
CORRECT
Sample output 2
INCORRECT

Hint

Bold texts appearing in the sample sections are informative and do not form part of the actual data.

微软小学者面试的时候还问我数独算法来着。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib> 
using namespace std;
int map[105][105];
int visit[105];
int n;
int N;
int column(int index)
{
    memset(visit,0,sizeof(visit));
    for(int i=0;i<N;i++)
    {
        if(visit[map[i][index]])
		{
			return 0;
		}
        if(map[i][index]!=0)
        {
            visit[map[i][index]]=1;
        }
    }
    return 1;
}
   
int row(int index)
{
    memset(visit,0,sizeof(visit));
    for(int i=0;i<N;i++)
    {
        if(visit[map[index][i]])
		{
			return 0;
		}
        if(map[index][i]!=0)
        {
            visit[map[index][i]]=1;
        }
    }
    return 1;
}
int oksquare(int x,int y)
{
    memset(visit,0,sizeof(visit));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(map[i+x][y+j]==0)
			{
				continue;
			}
            if(visit[map[i+x][y+j]])
			{
				return 0;
			}
            visit[map[i+x][y+j]]=1;
        }
    }
    return 1;
}
int judge()
{
    for(int i=0;i<N;i++)
    {
        if(!column(i))
		{
			return 0;
		}
		if(!row(i))
		{
			return 0;
		}
    }
    for(int i=0;i<N;i+=n)
    {
        for(int j=0;j<N;j+=n)
        {
            if(!oksquare(i,j))
			{
				return 0;
			}
        }
    }
    return 1;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
    	N=n*n;
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
            {
                scanf("%d",&map[i][j]);
            }
        }   
        if(judge())
        {
            printf("CORRECT\n");
        }
        else
        {
            printf("INCORRECT\n");
        }
    }
    return 0;
}

C - Unordered Subsequence
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.

A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.

Input

The first line of the input contains one integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — the given sequence. All numbers in this sequence do not exceed 106 by absolute value.

Output

If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then output k integers from the range [1..n] — indexes of the elements of this subsequence. If there are several solutions, output any of them.

Sample Input

Input
5
67 499 600 42 23
Output
3
1 3 5
Input
3
1 2 3
Output
0
Input
3
2 3 1
Output
3
1 2 3

就像找波峰/波谷一样
#include<iostream>
using namespace std;
int main()
{
	int n;
	int a[100005];
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	int j;
	for(j=2;a[j]==a[1];j++)
	{
		;
	}
	int flag=0;
	if(a[j]>a[1])
	{
		for(int i=j+1;i<=n;i++)
		{
			if(a[i]<a[i-1])
			{
				cout<<"3"<<endl;
				cout<<"1 "<<i-1<<" "<<i<<endl;
				return 0;
			}
		}
	}
	if(a[j]<a[1])
	{
		for(int i=j+1;i<=n;i++)
		{
			if(a[i]>a[i-1])
			{
				cout<<"3"<<endl;
				cout<<"1 "<<i-1<<" "<<i<<endl;
				return 0;
			}
		}
	}
	cout<<"0"<<endl;
	return 0;
}

D - Greenhouse Effect
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Emuskald is an avid horticulturist and owns the world's longest greenhouse — it is effectively infinite in length.

Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His greenhouse is very narrow and can be viewed as an infinite line, with each plant occupying a single point on that line.

Emuskald has discovered that each species thrives at a different temperature, so he wants to arrange m - 1 borders that would divide the greenhouse into m sections numbered from 1 to m from left to right with each section housing a single species. He is free to place the borders, but in the end all of the i-th species plants must reside in i-th section from the left.

Of course, it is not always possible to place the borders in such way, so Emuskald needs to replant some of his plants. He can remove each plant from its position and place it anywhere in the greenhouse (at any real coordinate) with no plant already in it. Since replanting is a lot of stress for the plants, help Emuskald find the minimum number of plants he has to replant to be able to place the borders.

Input

The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 5000n ≥ m), the number of plants and the number of different species. Each of the following n lines contain two space-separated numbers: one integer number si (1 ≤ si ≤ m), and one real number xi (0 ≤ xi ≤ 109), the species and position of the i-th plant. Each xi will contain no more than 6 digits after the decimal point.

It is guaranteed that all xi are different; there is at least one plant of each species; the plants are given in order "from left to the right", that is in the ascending order of their xi coordinates (xi < xi + 1, 1 ≤ i < n).

Output

Output a single integer — the minimum number of plants to be replanted.

Sample Input

Input
3 2
2 1
1 2.0
1 3.100
Output
1
Input
3 3
1 5.0
2 5.5
3 6.0
Output
0
Input
6 3
1 14.284235
2 17.921382
1 20.328172
3 20.842331
1 25.790145
1 27.204125
Output
2

Hint

In the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1.

In the second test case, the species are already in the correct order, so no replanting is needed.


/*
dp[i]表示前i株植物不需要移动的最大数目,其中第i株不动 
则dp[i]=max{dp[j]},1<=j<=i,其中s[j]<s[i] 
所求即为n-dp[n]
*/
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib> 
#include<algorithm>
using namespace std;
int main()
{
	int n,m;
	int s[5005];
	double x[5005];
	int dp[5005];
	while(cin>>n>>m)
	{
		for(int i=1;i<=n;i++)
		{
			cin>>s[i]>>x[i];
		}
		s[n+1]=m+1;
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n+1;i++)
		{
			for(int j=1;j<i;j++)
			{
				if(s[j]<=s[i])
				{
					dp[i]=max(dp[i],dp[j]);
				}
			}
			dp[i]++;
		}
		cout<<n-dp[n]<<endl;
	}
	return 0;
}

E - Binomial Coefficients
Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u

Description

The binomial coefficient C(nk) has been extensively studied for its importance in combinatorics. Binomial coefficients can be recursively defined as follows:

C(n, 0) = C(nn) = 1 for all n > 0;
C(nk) = C(n − 1, k − 1) + C(n − 1, k) for all 0 < k < n.

Given n and k, you are to determine the parity of C(nk).

Input

The input contains multiple test cases. Each test case consists of a pair of integers n and k (0 ≤ k ≤ n < 231n > 0) on a separate line.

End of file (EOF) indicates the end of input.

Output

For each test case, output one line containing either a “0” or a “1”, which is the remainder of C(nk) divided by two.

Sample Input

1 1
1 0
2 1

Sample Output

1
1
0

数学题无解
/*
(n,k)(k<=n)的奇偶性取决于(n-k)与k的二进制表达式是否存在同一位上的两个数码均为1,
若存在,则为偶数,反之为奇数
*/
#include<iostream>
using namespace std;
int main()
{
	int n,k;
    while(cin>>n>>k)
	{
		if(k&(n-k))
		{
			cout<<"0"<<endl;
		}
		else
		{
			cout<<"1"<<endl;
		}
    }
    return 0;
}

F - Good Sequences
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Squirrel Liss is interested in sequences. She also has preferences of integers. She thinks n integers a1, a2, ..., an are good.

Now she is interested in good sequences. A sequence x1, x2, ..., xk is called good if it satisfies the following three conditions:

  • The sequence is strictly increasing, i.e. xi < xi + 1 for each i (1 ≤ i ≤ k - 1).
  • No two adjacent elements are coprime, i.e. gcd(xi, xi + 1) > 1 for each i (1 ≤ i ≤ k - 1) (where gcd(p, q) denotes the greatest common divisor of the integers p and q).
  • All elements of the sequence are good integers.

Find the length of the longest good sequence.

Input

The input consists of two lines. The first line contains a single integer n (1 ≤ n ≤ 105) — the number of good integers. The second line contains a single-space separated list of good integers a1, a2, ..., an in strictly increasing order (1 ≤ ai ≤ 105ai < ai + 1).

Output

Print a single integer — the length of the longest good sequence.

Sample Input

Input
5
2 3 4 6 9
Output
4
Input
9
1 2 3 5 6 7 8 9 10
Output
4

Hint

In the first example, the following sequences are examples of good sequences: [2; 4; 6; 9], [2; 4; 6], [3; 9], [6]. The length of the longest good sequence is 4.


额。。
复杂度小一点的筛法不会做。。下面代码tle
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int gcd(int a,int b)
{
	int r=a%b;
	while(r)
	{
		a=b;
		b=r;
		r=a%b;
	}
	return b;
}
int main()
{
	int n;
	int a[100005];
	int dp[100005];
	while(cin>>n)
	{
		a[0]=1;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<i;j++)
			{
				if(gcd(a[i],a[j])>1)
				{
					dp[i]=max(dp[i],dp[j]);
				}
			}
			dp[i]++;
		}
		cout<<dp[n]<<endl;
	}
	return 0;
}

G - The Frog's Games
Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64u

Description

The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they 
are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog's longest jump distance).
 

Input

The input contains several cases. The first line of each case contains three positive integer L, n, and m. 
Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.
 

Output

For each case, output a integer standing for the frog's ability at least they should have.
 

Sample Input

        
        
6 1 2 2 25 3 3 11 2 18
 

Sample Output

        
        
4 11
 

/*
二分
*/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int n,m,l;
int dis[500005];
/*
int ok(int ability)
{
	int j=0,i=0,cur=0;
	for(int tt=1;tt<=m;tt++)
	{
		while(dis[j]-dis[i]<=ability)
		{
			j++;
			if(j-1==n+1)
			{
				break;
			}
		}
		cur=dis[j-1];
		i=j-1;
		if(cur>=l)
		{
			break;
		}
	}
	if(cur<l)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}
*/	 
int ok(int ablity)
{
	if(ablity*m<l)
	{
		return 0;
	}
	int i=0,cnt=0,j=0;
	while(i<=n+1)
	{
		if(dis[i]-dis[j]>ablity)
		{
			return 0;
		}
		while(dis[i]-dis[j]<=ablity&&i<=n+1)
		{
			i++;
		}
		j=i-1;
		cnt++;
	}
	if(cnt>m)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

int main()
{
	while(scanf("%d%d%d",&l,&n,&m)!=EOF)
	{
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&dis[i]);
		}
		dis[0]=0;
		dis[n+1]=l;
		sort(dis,dis+n+2);
		int left=0;
		int right=l;
		int mid;
		while(left<right)
		{
			mid=(left+right)/2;
			if(ok(mid))
			{
				right=mid-1;
			}
			else
			{
				left=mid+1;
			}
		}
		mid=(left+right)/2;
		printf("%d\n",mid);
	}
	return 0;
}

A - Best Cow Line
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges.

The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order according to the string of the initials of the cows' names.

FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them.

FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he's finished, FJ takes his cows for registration in this new order.

Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.

Input

* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single initial ('A'..'Z') of the cow in the ith position in the original line

Output

The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the new line.

Sample Input

6
A
C
D
B
C
B

Sample Output

ABCBCD

/*
模拟就好
pe了一万遍
*/
#include<iostream>
using namespace std; 
int main()
{
	char cow[2005];
	int n; 
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>cow[i];
	}
	int k=0;
	int cnt=0; 
	for(int i=1;i<=n;i++)
	{
		int left=i;
		int right=n;
		while(cow[left]==cow[right])
		{
			left++;
			right--;
		}
		if(cow[left]<cow[right])
		{
			cout<<cow[i];
		}
		else
		{
			cout<<cow[n];
			n--;
			i--;
		}
		cnt++;
		if(cnt%80==0)
		{
			cout<<endl;
		}
	}
	return 0;
}

B - Saruman's Army
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep track of his forces, Saruman distributes seeing stones, known as palantirs, among the troops. Each palantir has a maximum effective range of R units, and must be carried by some troop in the army (i.e., palantirs are not allowed to “free float” in mid-air). Help Saruman take control of Middle Earth by determining the minimum number of palantirs needed for Saruman to ensure that each of his minions is within R units of some palantir.

Input

The input test file will contain multiple cases. Each test case begins with a single line containing an integer R, the maximum effective range of all palantirs (where 0 ≤ R ≤ 1000), and an integer n, the number of troops in Saruman’s army (where 1 ≤ n ≤ 1000). The next line contains n integers, indicating the positions x1, …, xn of each troop (where 0 ≤ xi ≤ 1000). The end-of-file is marked by a test case with R = n = −1.

Output

For each test case, print a single integer indicating the minimum number of palantirs needed.

Sample Input

0 3
10 20 20
10 7
70 30 1 7 15 20 50
-1 -1

Sample Output

2
4

Hint

In the first test case, Saruman may place a palantir at positions 10 and 20. Here, note that a single palantir with range 0 can cover both of the troops at position 20.

In the second test case, Saruman can place palantirs at position 7 (covering troops at 1, 7, and 15), position 20 (covering positions 20 and 30), position 50, and position 70. Here, note that palantirs must be distributed among troops and are not allowed to “free float.” Thus, Saruman cannot place a palantir at position 60 to cover the troops at positions 50 and 70.


/*
简单贪心,排序后一次寻找中间那个数就好
*/
include<iostream>
#include<algorithm> 
using namespace std;
int n,R;
int a[1005];
int main()
{
	while(cin>>R>>n,R!=-1&&n!=-1)
	{
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
		}
		int ans=0;
		sort(a,a+n);
		for(int i=0;i<n;i++)
		{
			int j;
			for(j=i+1;j<n;j++)
			{
				if(a[i]+R<a[j])
				{
					break;
				}
			}
			int k;
			for(k=j-1;k<n;k++)
			{
				if(a[j-1]+R<a[k])
				{
					break;
				}
			}
			ans++;
			i=k-1;
		}
		cout<<ans<<endl;
	}
	return 0;
}
		

C - Fence Repair
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 1: One integer  N, the number of planks 
Lines 2..  N+1: Each line contains a single integer describing the length of a needed plank

Output

Line 1: One integer: the minimum amount of money he must spend to make  N-1 cuts

Sample Input

3
8
5
8

Sample Output

34

Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8. 
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).

/*
简单huffman
用优先队列做的
注意要用Long Long 
*/
#include<iostream>
#include<queue>
using namespace std;
struct node
{
	long long len;
	bool operator <(const node &x)const
	{
		return len>x.len;
	}
}pq;
priority_queue<node> que;
int main()
{
	int n;
	long long ans=0;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>pq.len;
		que.push(pq);
	}
	while(que.size()>1)
	{
		long long a=que.top().len;
		que.pop();
		long long b=que.top().len;
		que.pop();
		pq.len=a+b;
		que.push(pq);
		ans+=pq.len;
	}
	cout<<ans<<endl;
	return 0;
}

D - Cleaning Shifts
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T. 

Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval. 

Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.

Input

* Line 1: Two space-separated integers: N and T 

* Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.

Output

* Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

Sample Input

3 10
1 7
3 6
6 10

Sample Output

2

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. 

INPUT DETAILS: 

There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10. 

OUTPUT DETAILS: 

By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.

/*
贪心
排序后
每次选择包括范围最大的
*/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
struct node
{
	int start;
	int end;
}cow[25005];
bool cmp(const node &a,const node &b)
{
	if(a.start==b.start)
	{
		return a.end>b.end;
	}
	else
	{
		return a.start<b.start;
	}
}
int main()
{
	int n,t;
	scanf("%d%d",&n,&t);
	for(int i=0;i<n;i++)
	{
		scanf("%d%d",&cow[i].start,&cow[i].end);
	}
	sort(cow,cow+n,cmp);
	if(cow[0].start!=1)
	{
		printf("-1\n");;
	}
	else if(cow[0].end==t)
	{
		printf("1\n");
	}
	else
	{
		int maxend=0;
		int k;
		int flag=0;
		int ok=0;
		int cnt=1;
		for(int i=0;i<n; )
		{
			flag=0;
			maxend=cow[i].end;
			for(int j=i+1;j<n;j++)
			{
				if(cow[j].start>=cow[i].start&&cow[j].start<=cow[i].end+1)
				{
					if(cow[j].end>maxend)
					{
						maxend=cow[j].end;
						k=j;
						flag=1;
					}
				}
			}
			if(flag)
			{
				i=k;
				cnt++;
			}
			else
			{
				break;
			}
			if(cow[i].end==t)
			{
				ok=1;
				break;
			}
		}
		if(ok)
		{
			printf("%d\n",cnt);
		}
		else
		{
			printf("-1\n");
		}
	}
	return 0;
}

E - Radar Installation
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Description

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d. 

We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates. 
 
Figure A Sample Input of Radar Installations


Input

The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases. 

The input is terminated by a line containing pair of zeros 

Output

For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.

Sample Input

3 2
1 2
-3 1
2 1

1 2
0 2

0 0

Sample Output

Case 1: 2
Case 2: 1

/*
先求出每个island可能圆心的左右边界
问题转换成一般的贪心选择问题
然后从小到大排序后
从左往右贪心寻找(右边界优先) 
*/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int n,d;
struct node
{
	double left;
	double right;
}island[1005];
bool cmp(const node &a,const node &b)
{
	return a.left<b.left;
}
int main()
{
	int tt=1;
	while(cin>>n>>d,n!=0&&d!=0)
	{
		memset(island,0,sizeof(island));
		int ans=1;
		int flag=1;		
		for(int i=0;i<n;i++)
		{
			int x,y;
			cin>>x>>y;
			if(y>d)
			{
				flag=0;
			}
			else if(flag)
			{
				double lenx=sqrt(double(d*d-y*y));
				island[i].left=x-lenx;
				island[i].right=x+lenx;
			}
		}
		if(!flag)
		{
			cout<<"Case "<<tt++<<": -1"<<endl;
		}
		else
		{
			sort(island,island+n,cmp);
			double temp=island[0].right;
			for(int i=1;i<n;i++)
			{
				if(island[i].left>temp)
				{
					ans++;
					temp=island[i].right;
				}
				else if(island[i].right<temp)
				{
					temp=island[i].right;
				}
			}
			cout<<"Case "<<tt++<<": "<<ans<<endl;
		}
	}
	return 0;
}

F - Stall Reservations
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows. 

Help FJ by determining:
  • The minimum number of stalls required in the barn so that each cow can have her private milking period
  • An assignment of cows to these stalls over time
Many answers are correct for each test dataset; a program will grade your answer.

Input

Line 1: A single integer, N 

Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.

Output

Line 1: The minimum number of stalls the barn must have. 

Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

Sample Input

5
1 10
2 4
3 6
5 8
4 7

Sample Output

4
1
2
3
2
4

Hint

Explanation of the sample: 

Here's a graphical schedule for this output: 

Time     1  2  3  4  5  6  7  8  9 10

Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>

Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..

Stall 3 .. .. c3>>>>>>>>> .. .. .. ..

Stall 4 .. .. .. c5>>>>>>>>> .. .. ..
Other outputs using the same number of stalls are possible.
还是Tle。。。
线段树神马的不会。。
/*
tle了
*/
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
	int start;
	int end;
	int index;
}cow[50005];
bool cmp(const node &a,const node &b)
{
	if(a.start==b.start)
	{
		return a.end>b.end;
	}
	else
	{
		return a.start<b.start;
	}
}
int num[500005];
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>cow[i].start>>cow[i].end;
		cow[i].index=i;
	}
	sort(cow,cow+n,cmp);
	int k=1;
	int ok=0;
	for(int i=0;i<n;i++)
	{
		if(!num[cow[i].index])
		{
			num[cow[i].index]=k;
		}
		int temp=cow[i].end;
		for(int j=i+1;j<n;j++)
		{ 
			if(cow[j].start>temp)
			{
				num[cow[j].index]=k;
				temp=cow[j].end;
			}
		}
		k++;	
	}
	int ans=0;
	for(int i=0;i<n;i++)
	{
		ans=max(ans,num[i]);
	}
	cout<<ans<<endl;
	for(int i=0;i<n;i++)
	{
		cout<<num[i]<<endl;
	}
	return 0;
}
				
		

明天计组实验的任务小一点
看看书
干做题不是办法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值