暑假训练day1 : 2016 USP-ICMC

发挥还算不错。。。

传送门:http://codeforces.com/gym/101063

https://vjudge.net/contest/169377#overview


A. Giant Snail Maze
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

It is the year of 2016 and Mars is still not colonized. A daring group of immortal, rich explorers based in São Carlos decided that it is about time to do something about it, so they founded a space exploration company called GEMA - Go Explore Mars Already!

Fast forward to 2017. The first mission of GEMA has finally landed on Mars, but it seems like the rocket fell in the middle of some ancient ruins that form a giant maze resembling a snail. The maze is composed of Ncircular sectors all centered at the crash site of the rocket.

The circular sectors of the maze all have a railway running through their middle, and some portions of the walls are open, making it possible to leave to the next sector. The rails connecting sectors are straight, radial lines through the opening, connecting the rails of the two sectors. There is always a straight line connecting the crash site to the first sector railway through the openings. The only safe way to travel in this maze is by using these rails.

The crew of the GEMA mission wants to leave the maze as soon as possible. Luckily, they are in possession of small carts that fit the railway. Help them find the path with the minimum length to leave the maze. You are out of the maze as soon as you cross the boundary of the outermost wall.

暑假训练day1 : 2016 USP-ICMC_第1张图片

The above figure illustrates the first sample test case. Dashed lines represent the railways that run through the middle of the sectors, whose boundaries are represented by solid circles. The best solution in this case is to go from points A, B, C, D and then E, going through the sectors railways through points X, Y, W and Z, as indicated by the green line.

Input

The input begins with an integer N (1 ≤ N ≤ 105), the number of circular sectors of the Snail Maze. The next line has N integers r, the radius of each of the sectors (1 ≤ r ≤ 109). The next line contains an integer Q (N ≤ Q ≤ 105), the number of openings. The next Q lines contains an integer and a real number each. The first integer, ri (1 ≤ ri ≤ 109) gives the radius of a wall (that radius will be one of the N numbers given in the second line), and the real number d, gives the angle, in radians, where the opening is located (0 ≤ d ≤ 2π). The angle is measured in clockwise direction with respect to west (the positive x-axis).

It is guaranteed that there is a path to leave the maze, i.e., every wall has at least one opening. There will not be two openings closer than 10 - 4 radians.

Output

Output the length of the shortest path to leave the maze. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Example
input
3
4 10 6
4
4 1.0472
4 1.5707
6 4.7123
10 4.1857
output
27.30323
因为从内到外的直线距离固定,所以只需要关注圆环的长度。角度相同的圆环,内环比外环短,因此对于每个点,到下一层只需要取外环与它相邻最近的两个点。之后做一个最短路即可。

构图比较难搞,待补

B. Martian Sunrise
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Life on a different planet is hard. It is usually all about science, research and cold nights and days. It is no different on Mars.

To entertain people, GEMA brought with the mission a group of talented musicians to write ballads about the new and exciting world. They were instructed to write a very long and optimistic song called Martian Sunrise and play it all day around the research and living facilities.

No musician could write, alone, a song with thousands of notes, so they divided their task. GEMA was on a tight budget, and ended up hiring musicians that can only write and play songs in their own favourite single key signature. A key signature is a set (out of 16 possible sets) of notes that can be used. In the end, the ballad was composed of different parts in various key signatures.

Now that cash is flowing in, the high commanders have decided to bring new, more talented musicians that can play up to two different key signatures to play the Mars Jam. It was decided that the musicians will take turns playing, and no musician will play again after having finished their part. GEMA has all the musicians on Earth at their disposal, since the mission is very highly regarded. This means that for any pair of key signatures, they can find infinitely many musicians that can play it.

Even with endless cash, it is good to be optimal. It is up to you to figure out the minimum number of musicians necessary to play the whole song.

Input

The input will begin with an integer M, the number of existing key signatures (1 ≤ M ≤ 16). The next Mlines of the input will be composed of a set of 7 words each, denoting the notes used in this signature. The words representing notes will have a maximum of two characters each, and will be in the format [A-G][b#]?, that is, start with an uppercase character between 'A' and 'G' and optionally have a '#' or a 'b', for a sharp or flat.

The next line of the input contains an integer N (1 ≤ N ≤ 104). The following line contains the N notes of the original Martian Sunrise, separated by a space. Every note of the song is present in at least one of the M key signatures.

Please note that, although in musical terms C# equals Db, in this problem a musician that can play C# cannot necessarily play Db.

Output

Output the minimum number of musicians necessary to play the Martian Sunrise.

Examples
input
10
C# D# E# F# G# A# B#
Cb Db Eb F Gb Ab Bb
C# D# E F# G# A B
C D E F G A B
C D E F# G A B
C D Eb F G A Bb
C# D E F# G# A B
C D E F G A Bb
C Db Eb F G Ab Bb
C# D# E# F# G# A# B
3
F# Bb B#
output
1
input
8
Cb Db Eb F Gb Ab Bb
C# D# E F# G# A# B
C# D# E# F# G# A# B
C D Eb F G Ab Bb
C# D# E# F# G# A# B#
C Db Eb F Gb Ab Bb
C D E F# G A B
C# D E F# G A B
6
C F# G# D B# Db
output
2

比较水的模拟。。。

比赛时候超时还以为是复杂度太高,用位运算搞了个O(n)算法,其实是死循环,没有考虑m=1。还好最后一分钟绝杀此题。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long double ld;
int a[256][25],b[20][25],pitch[10005];
char s[23];

int main() {
	int n,m,i,j,k;
	char c;
	scanf("%d",&m);
	map my;
	my['A']=1;my['B']=2;my['C']=3;my['D']=4;my['E']=5;my['F']=6;my['G']=7;
	my['#']=7;my['b']=14;
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	for (i=1;i<=m;i++) {
		for (j=1;j<=7;j++) {
			scanf("%s",s);
			if (strlen(s)==1) {
				b[i][my[s[0]]]=1;
			} else {
				b[i][my[s[0]]+my[s[1]]]=1;
			}
		}
	}
	int l=0;
	if (m!=1)
	for (i=1;i<=m;i++) {
		for (j=i+1;j<=m;j++) {
			l++;
			for (k=1;k<=21;k++) {
				a[l][k]=b[i][k]+b[j][k];
				if (a[l][k]) a[l][0]=a[l][0]|(1<<(k-1));
			}
		}
	}
	else {
			l=1;
			for (k=1;k<=21;k++) {
				a[l][k]=b[1][k];
				if (a[l][k]) a[l][0]=a[l][0]|(1<<(k-1));
			}
		}
	scanf("%d",&n);
	for (i=1;i<=n;i++) {
		scanf("%s",s);
		if (strlen(s)==1) {
			pitch[i]=my[s[0]];
		} else {
			pitch[i]=my[s[0]]+my[s[1]];
		}
	}
	int maxlen=1,maxcondition,cnt=0;
	while (maxlen<=n) {
		cnt++;
		maxcondition=0;
		for (j=1;j<=l;j++) {
			if (maxcondition!=(maxcondition&a[j][0])) continue;
			while (a[j][pitch[maxlen]]&&maxlen<=n) {
				maxcondition=(maxcondition|(1<<(pitch[maxlen]-1)));
				maxlen++;
			}
		}
	}
	printf("%d\n",cnt);
	return 0;
}

C. Sleep Buddies
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

It is nighttime in the Earth Colony on Mars and everyone is getting ready to sleep. It is common to sleep in pairs, so that if anything were to happen during the night people could aid each other.

To decide who is a suitable candidate to sleep with whom, the leaders of GEMA asked everyone to answer a questionnaire about attributes they desire their partner to have from a list of M possible items.

To choose the pairs, GEMA uses the Jaccard index between the desired attributes of both persons. The Jaccard index for two sets A and B is defined as , that is, the size of the intersection between A and B divided by the size of their union. They allow a pair to be formed if the Jaccard index of the two attribute sets for the pair is at least K.

Thanks to GEMA, there are too many people living on Mars these days. Help the high commanders decide how many allowed pairs there are out of the N people living there.

Input

The input begins with two integers, N and M (1 ≤ N ≤ 1051 ≤ M ≤ 10), the number of people on Mars and the number of possible attributes on the questionnaire.

Then follow N lines, each beginning with an integer Q (1 ≤ Q ≤ M), the number of desired attributes on the list of the i-th person. Then follow Q integers q (1 ≤ q ≤ M), encoding these attributes. There numbers are all different.

The last line of input contains a real number K (0 ≤ K ≤ 1), the minimum required Jaccard index for a pair.

Output

Output the number of pairs that are allowed.

Examples
input
2 5
2 1 3
5 3 1 5 4 2
0.66489
output
0
input
3 1
1 1
1 1
1 1
0.85809
output
3
把集合转成十位的二进制,再用位运算搞。

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long double ld;
typedef long long ll;
const int maxn=100005; 
int a[maxn][11],b[maxn];
ll c[(1<<10)+5],d[maxn];

int main() {
	int n,m,q,i,j;
	ld k;
	scanf("%d%d",&n,&m);
	memset(c,0,sizeof(c));
	for (i=1;i<=n;i++) {
		scanf("%d",&a[i][0]);
		b[i]=0;
		for (j=1;j<=a[i][0];j++) {
			scanf("%d",&a[i][j]);
			b[i]+=(1<<(a[i][j]-1));
		}
		c[b[i]]++;
	}
	cin >> k;
	ll ans=0;
	for (i=1;i<=1023;i++) {
		int l=i;
		d[i]=0;
		while (l>0) {
			if (l%2) d[i]++;
			l/=2;
		}
	}
	for (i=1;i<=1023;i++) {
		for (j=i;j<=1023;j++) {
			int q=i&j,w=i|j;
			ld ratio=(ld)d[q]/(ld)d[w];
			if (ratio>=k) {
				if (i!=j) ans+=c[i]*c[j]; else ans+=c[i]*(c[i]-1)/2;
			}
		}
	}
	printf("%I64d\n",ans);
}

F. Bandejao
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

It is lunch time on Mars! Everyone has got that big smile on their faces, all eager to see what the GEMA restaurant is serving for dessert. Everything is pretty much like Earth, but nobody knows why, GEMA decided to invent new types of utensils. Somehow, the food research group concluded that the usual fork, knife and spoon would not be so adequate on Mars.

There are K types of utensils and if you want to have some food, you must use all K of them (one of each). You like to have lunch with your N friends, and when you are leaving, you want to distribute the utensils among you and all your friends equally (everyone having the same amount of utensils). In addition to that, you want each one of your friends (you included) to carry only one type of utensil (it speeds up the process of leaving the restaurant, and your friends get really mad when it takes more time than usual for them to get back to work).

Given N and K, answer if it is possible for you and your friends to leave the restaurant with everyone carrying the same number of utensils and each one carrying only one type of utensil.

Input

Two integers N and K (0 ≤ NK ≤ 100 and K > 0). (You and your friends form a group of N + 1 people).

Output

Print "yes" (if it is possible) or "no" without quotes.

Examples
input
1 2
output
yes
input
1 3
output
no

最水的一道题,还有15分钟结束时才做出来,花样作死

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int a[105][105];

int main() {
	int n,k;	
    cin >> n >> k;
    if ((n+1)%k==0) cout << "yes\n"; else cout << "no\n";	
	return 0;
}

H. Reporting on Mars
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A lot of people, still living on Earth, dream of one day getting the opportunity to go to Mars and be a GEMA explorer. It's so glamorous and important, everything is so exciting, so different from the regular boring life on Earth.

However, the reality of the regular folk on Mars is not as nearly glamorous as everyone thinks. A day of an average explorer is at least as boring as a day of any Earth-living human. Every week, GEMA assigns tasks to each explorer in the expedition. Most of the times, these tasks are just very long and detailed reports they have to make. The worst part is that, after completing it, you would still have to review reports from your colleagues and correct them if mistakes are found.

One type of report consists of reporting collected data, but, before making the report, one should verify the consistency of the data. This is obviously the most boring part, so it is not surprising that some people prefer to skip it and just write the report without any verification.

To make matters worse, when reviewing a report containing contradictory data, instead of canceling the report, some explorers choose to manipulate the data, changing some numbers, so it would become consistent. This happens because canceled reports are seen as a great incompetence of the whole team who worked on it.

You are a GEMA explorer and you've just received a report. You need to manipulate the data on it, in order to make it seem correct. The data of the report is a bunch of positive or negative numbers. The data is considered consistent if and only if every sub-array of length k has a positive result when all numbers in it are multiplied together.

Formally, if the report is an array A of N positive and/or negative numbers, the report is consistent, if and only if, for all 1 ≤ i ≤ N - k + 1, we have:

Answer what is the least number of changes you have to make in the report so it would become consistent. A change in the report is changing the value of one of its numbers.

Input

The input begins with two integers N and k (1 ≤ k ≤ N ≤ 5 × 105) in a single line. The next line will contain N integers ai ( - 100 ≤ ai ≤ 100 and ai ≠ 0).

Output

One integer, the answer to the task.

Examples
input
3 1
10 -100 33
output
1
input
5 5
1 -1 -2 3 4
output
0
input
5 3
1 -1 -2 3 4
output
1

只考虑正负性即可。

序号相差k的数字,满足题中式子时正负性一定相等。把极性相同的数字改成同一符号即可。取正还是负呢?枚举前k个数有0,2,...2n个负数的情况,贪心更新最优解。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn=500005;
int a[maxn];

struct Node {
	int b,c;
} node[maxn];

bool cmp(Node x,Node y) {
	return x.b-x.c>y.b-y.c;
}
 
int main() {
	int n,i,k,j,ans=0,sum=0;
	memset(node,0,sizeof(node));
	scanf("%d%d",&n,&k);
	for (i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		if (a[i]<0) node[i%k].b++; else node[i%k].c++;
	}
	sort(node,node+k,cmp);
	for (i=0;i

J. The Keys
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Out of all science labs constructed by the GEMA mission on Mars, the DSL - Dangerous Species Lab is the most dangerous of them all. The laboratory is so dangerous that you have to go through N doors in succession to get to it. Each one of those doors can only be opened by one key di (notice, however, that there may be different doors that can be opened by the same key).

A nameless lazy biologist (we'll call him LB) from GEMA needs to open all those doors first thing in the morning, every day. He has all the keys necessary to open them, but he finds carrying all of them in his pockets too much of a mess.

To be more organized and lose the title of being a lazy biologist, LB purchased K key-chains and is planning to distribute all the keys among them. His plan of distribution is very simple. For each key, randomly choose a key-chain with uniform probability and put this key on it.

When opening the doors, LB will hold one key-chain and will keep the others in his pocket (initially all of them are in his pocket). Whenever he gets to a door that needs a key that is not on the key-chain he is holding, he will swap it with the key-chain that has this key. Getting the first key-chain from his pocket is not considered a swap.

You have to help LB and find what is the expected number of key-chain swaps he will have to do when opening the doors the next morning.

Input

Input begins with N and K (1 ≤ N ≤ 1051 ≤ K ≤ N), the number of doors and the number of key-chains. On the next line there are N numbers di (1 ≤ di ≤ 106), the identifier of the key that opens the i-th door.

Output

Output the expected number of swaps. Your answer will be considered correct if the absolute and relative error are less than 10 - 6.

Examples
input
3 3
1 2 3
output
1.333333333
input
1 1
2
output
0.000000000
input
5 2
1 2 3 2 1
output
2.000000000

数学题,草稿纸上推个公式。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long double ld;
const int maxn=100005;
int a[maxn];

int main() {
	int n,k,i,j,q=0;
	scanf("%d%d",&n,&k);
	for (i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		if (i>1&&a[i]!=a[i-1]) q++;
	}
	ld ans=(ld)(k-1)/(ld)k;
	ans*=(ld)q;
	cout << setiosflags(ios::fixed) << setprecision(9);
	cout << ans;
	return 0;
}

K. Dire, Dire Docks
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Half a century after the Mars arrival by GEMA, we are finally here. The mission is finally starting to build artificial rivers on mars.

That is possibly the most important event on Mars since the GEMA arrival.

In order to carry on with it, the mission have mapped the surface as 2-dimensional plane, and has identified N points in it that are going to work as reference points to build a river system. There will be a dock constructed in each of these points to help on transportation efforts and collect data for the mission.

The goal of the mission is to connect all the docks with rivers. The lead researcher of the team responsible for that, doctor Maya Waters, has identified the main requirements to build a healthy river system. These are:

  • There are N docks. There should be a way to navigate between any two docks only through water;
  • The system must have exactly N rivers - no more, no less;
  • At most four rivers can meet at any single dock;
  • Any river should flow in a straight line between points A and B;
  • There should be no river crossings apart from the rivers meeting at docks. Note that a river passing through a dock counts as a crossing.

Help doctor Maya figure out which points to connect to build such river system and finally Make Mars Wet Again!

Input

Input begins with N (3 ≤ N ≤ 103), the number of points. Follows N lines, each with two integers xiyi( - 109 ≤ xi, yi ≤ 109), the coordinates of the pivotal points.

Output

Output N lines. In each line, output two integers , a and b - the indexes of the pivotal points that must be connected by a river. Indexing begins in 1. If there are multiple solutions, output any.

If it is an impossible task, output -1.

Examples
input
5
-1 0
2 3
3 2
5 4
6 1
output
3 2
3 4
5 3
1 5
2 4
input
3
0 0
1 2
3 6
output
-1
除了所有点在一条直线,其他情况都有解。

先按坐标排个序,按顺序连起来,再找一条线就可以。剩下的一条线,一端为第一个点,依次枚举。


你可能感兴趣的:(日常训练,数论)