链接:戳这里
A. Round House
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya lives in a round building, whose entrances are numbered sequentially by integers from 1 to n. Entrance n and entrance 1 are adjacent.
Today Vasya got bored and decided to take a walk in the yard. Vasya lives in entrance a and he decided that during his walk he will move around the house b entrances in the direction of increasing numbers (in this order entrance n should be followed by entrance 1). The negative value of b corresponds to moving |b| entrances in the order of decreasing numbers (in this order entrance 1 is followed by entrance n). If b = 0, then Vasya prefers to walk beside his entrance.
Help Vasya to determine the number of the entrance, near which he will be at the end of his walk.
Input
The single line of the input contains three space-separated integers n, a and b (1 ≤ n ≤ 100, 1 ≤ a ≤ n, - 100 ≤ b ≤ 100) — the number of entrances at Vasya's place, the number of his entrance and the length of his walk, respectively.
Output
Print a single integer k (1 ≤ k ≤ n) — the number of the entrance where Vasya will be at the end of his walk.
Examples
input
6 2 -5
output
3
input
5 1 3
output
4
input
3 2 7
output
3
Note
The first example is illustrated by the picture in the statements.
题意:给出n,a,b (1<=a<n<=100) (-100<=b<=100) n表示长度为n的环,标号1~n, 现在给你初始位置a,在你走了b步之后回走到哪里 b为负数的话走逆时针 否则顺时针
思路:强行把b换成顺时针直接取模就可以了
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,a,b;
int main(){
scanf("%d%d%d",&n,&a,&b);
if(b==0) {
cout<<a<<endl;
return 0;
}
while(b<0){
b+=n;
}
int ans=(a+b)%n;
if(ans==0) cout<<n<<endl;
else cout<<ans<<endl;
return 0;
}
B. Qualifying Contest
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive.
The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest.
Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests.
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 10 000, n ≥ 2m) — the number of participants of the qualifying contest and the number of regions in Berland.
Next n lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to m) and the number of points scored by the participant (integer from 0 to 800, inclusive).
It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the m regions. The surnames that only differ in letter cases, should be considered distinct.
Output
Print m lines. On the i-th line print the team of the i-th region — the surnames of the two team members in an arbitrary order, or a single character "?" (without the quotes) if you need to spend further qualifying contests in the region.
Examples
input
5 2
Ivanov 1 763
Andreev 2 800
Petrov 1 595
Sidorov 1 790
Semenov 2 503
output
Sidorov Ivanov
Andreev Semenov
input
5 2
Ivanov 1 800
Andreev 2 763
Petrov 1 800
Sidorov 1 800
Semenov 2 503
output
?
Andreev Semenov
Note
In the first sample region teams are uniquely determined.
In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: "Petrov"-"Sidorov", "Ivanov"-"Sidorov", "Ivanov" -"Petrov", so it is impossible to determine a team uniquely.
题意:背景大概是一个城市承办资格赛,城市设置m个赛点,总共有n个参赛者,每个赛区保证至少有两个人参赛,我们需要处理的是输出每个赛区的前两名,但是如果存在分数一样使得不存在前二名的话,就输出"?"
思路:vector+结构体处理出来再排个序就可以了,这里需要注意一个hack点,就是赛区只有两个人的时候且得分都为0
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
struct node{
string s;
int sco;
bool operator < (const node &a) const{
return sco>a.sco;
}
};
vector<node> V[1000100];
int n,m;
string ss;
int main(){
scanf("%d%d",&n,&m);
node tmp;
for(int i=1;i<=n;i++){
int id,x;
cin>>ss;
scanf("%d%d",&id,&x);
tmp.s=ss;
tmp.sco=x;
V[id].push_back(tmp);
}
tmp.s="aaaa";
tmp.sco=-1;
for(int i=1;i<=m;i++) V[i].push_back(tmp);
for(int i=1;i<=m;i++){
sort(V[i].begin(),V[i].end());
if(V[i][1].sco>V[i][2].sco)
cout<<V[i][0].s<<" "<<V[i][1].s<<endl;
else cout<<"?"<<endl;
}
return 0;
}
/*
2 1
Ivanov 1 0
Andreev 1 0
*/
C. Tanya and Toys
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
In Berland recently a new collection of toys went on sale. This collection consists of 109 types of toys, numbered with integers from 1 to 109. A toy from the new collection of the i-th type costs i bourles.
Tania has managed to collect n different types of toys a1, a2, ..., an from the new collection. Today is Tanya's birthday, and her mother decided to spend no more than m bourles on the gift to the daughter. Tanya will choose several different types of toys from the new collection as a gift. Of course, she does not want to get a type of toy which she already has.
Tanya wants to have as many distinct types of toys in her collection as possible as the result. The new collection is too diverse, and Tanya is too little, so she asks you to help her in this.
Input
The first line contains two integers n (1 ≤ n ≤ 100 000) and m (1 ≤ m ≤ 109) — the number of types of toys that Tanya already has and the number of bourles that her mom is willing to spend on buying new toys.
The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the types of toys that Tanya already has.
Output
In the first line print a single integer k — the number of different types of toys that Tanya should choose so that the number of different types of toys in her collection is maximum possible. Of course, the total cost of the selected toys should not exceed m.
In the second line print k distinct space-separated integers t1, t2, ..., tk (1 ≤ ti ≤ 109) — the types of toys that Tanya should choose.
If there are multiple answers, you may print any of them. Values of ti can be printed in any order.
Examples
input
3 7
1 3 4
output
2
2 5
input
4 14
4 6 12 8
output
4
7 2 3 1
Note
In the first sample mom should buy two toys: one toy of the 2-nd type and one toy of the 5-th type. At any other purchase for 7 bourles (assuming that the toys of types 1, 3 and 4 have already been bought), it is impossible to buy two and more toys.
题意:一个女孩喜欢收集娃娃并且所有的娃娃都是唯一编号的(1~1e9),她现在已经收集了n个娃娃,她的妈妈准备在她生日的时候给她送娃娃,但是妈妈只送,总和m内的娃娃个数,计算总和的时候是按编号累加的,要求输出女孩满足总和在m内的收集的最多娃娃个数以及娃娃编号,answer最大有多组解答案不唯一的话任意输出所取娃娃编号
思路:map标记一下,从小开始取,每次判断一下m是否>=0
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m;
int a[1000100];
int anw[1000100];
map<int,int> M;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
M[a[i]]=1;
}
int num=0;
for(int i=1;;i++){
if(m-i<0) break;
if(M[i]==0) {
anw[++num]=i;
m-=i;
}
}
cout<<num<<endl;
for(int i=1;i<=num;i++){
cout<<anw[i]<<" ";
}
cout<<endl;
return 0;
}
D. Bicycle Race
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Maria participates in a bicycle race.
The speedway takes place on the shores of Lake Lucerne, just repeating its contour. As you know, the lake shore consists only of straight sections, directed to the north, south, east or west.
Let's introduce a system of coordinates, directing the Ox axis from west to east, and the Oy axis from south to north. As a starting position of the race the southernmost point of the track is selected (and if there are several such points, the most western among them). The participants start the race, moving to the north. At all straight sections of the track, the participants travel in one of the four directions (north, south, east or west) and change the direction of movement only in bends between the straight sections. The participants, of course, never turn back, that is, they do not change the direction of movement from north to south or from east to west (or vice versa).
Maria is still young, so she does not feel confident at some turns. Namely, Maria feels insecure if at a failed or untimely turn, she gets into the water. In other words, Maria considers the turn dangerous if she immediately gets into the water if it is ignored.
Help Maria get ready for the competition — determine the number of dangerous turns on the track.
Input
The first line of the input contains an integer n (4 ≤ n ≤ 1000) — the number of straight sections of the track.
The following (n + 1)-th line contains pairs of integers (xi, yi) ( - 10 000 ≤ xi, yi ≤ 10 000). The first of these points is the starting position. The i-th straight section of the track begins at the point (xi, yi) and ends at the point (xi + 1, yi + 1).
It is guaranteed that:
the first straight section is directed to the north;
the southernmost (and if there are several, then the most western of among them) point of the track is the first point;
the last point coincides with the first one (i.e., the start position);
any pair of straight sections of the track has no shared points (except for the neighboring ones, they share exactly one point);
no pair of points (except for the first and last one) is the same;
no two adjacent straight sections are directed in the same direction or in opposite directions.
Output
Print a single integer — the number of dangerous turns on the track.
Examples
input
6
0 0
0 1
1 1
1 2
2 2
2 0
0 0
output
1
input
16
1 1
1 5
3 5
3 7
2 7
2 9
6 9
6 7
5 7
5 3
4 3
4 4
3 4
3 2
5 2
5 1
1 1
output
6
Note
The first sample corresponds to the picture:
The picture shows that you can get in the water under unfortunate circumstances only at turn at the point (1, 1). Thus, the answer is 1.
题意:自行车围绕一片水域骑行,车的拐弯只能是东西南北的90°直角,输出有多少的点的属于危险的情况
危险的情况也就是车有可能在拐弯的时候冲进水域 这个看样例就清楚了
思路:由于只有1000个点,果断O(n*n)做啊 ,在每一次拐弯的时候在线段上突出一点点距离,再去判断这突出的一点点的这个点是否在水域内,其实刷过几何的都知道就是判断点是否在多边形内了
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
struct point {
double x,y;
point(double x=0,double y=0):x(x),y(y) {}
};
typedef point vec;
const double eps=1e-10;
const double pi=acos(-1.0);
bool cmp(point a,point b) {
if(fabs(a.x-b.x)<=eps) return a.y<b.y;
return a.x<b.x;
}
vec operator -(point a,point b) {
return vec(a.x-b.x,a.y-b.y);
}
vec operator +(point a,point b) {
return vec(a.x+b.x,a.y+b.y);
}
vec operator *(point a,double t) {
return vec(a.x*t,a.y*t);
}
vec operator /(point a,double t) {
return vec(a.x/t,a.y/t);
}
bool operator < (const point &a,const point &b) {
return a.y<b.y || (fabs(a.y-b.y)<=eps && a.x<b.x);
}
bool operator == (const point &a,const point &b) {
if(fabs(a.x-b.x)<=eps && fabs(a.y-b.y)<=eps)
return true;
return false;
}
int dcmp(double x) {
if(fabs(x)<=eps) return 0;
return x<0?-1:1;
}
double cross(vec a,vec b) {///叉积
return a.x*b.y-a.y*b.x;
}
double dot(vec a,vec b) {///点积
return a.x*b.x+a.y*b.y;
}
double disn(point a,point b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
/*两点之间的距离*/
}
double lentgh(vec a){///向量长度
return sqrt(dot(a,a));
}
bool onseg(point p,point a1,point a2){
return dcmp(cross(a1-p,a2-p))==0 && dcmp(dot(a1-p,a2-p))<0;
/*点在线段上 不包含端点(<=0)*/
}
int pointinpoly(point p,point *poly,int n){
int wn=0;
for(int i=0;i<n;i++){
if(onseg(p,poly[i],poly[(i+1)%n])) return -1;///点在边界上
int k=dcmp(cross(poly[(i+1)%n]-poly[i],p-poly[i]));
int d1=dcmp(poly[i].y-p.y);
int d2=dcmp(poly[(i+1)%n].y-p.y);
if(k>0 && d1<=0 && d2>0) wn++;
if(k<0 && d2<=0 && d1>0) wn--;
}
if(wn!=0) return 1;///内部
return 0;///外部
/*点在多边形内 p是点 poly[]是多边形
多边形的点必须是顺时针或者逆时针
n是点的个数
*/
}
vec normal(vec a){///向量的单位法向量
double L=lentgh(a);
return vec(a.x/L,a.y/L);
}
int n;
point s[100100];
int main(){
scanf("%d",&n);
for(int i=0;i<=n;i++) scanf("%lf%lf",&s[i].x,&s[i].y);
int num=0;
for(int i=0;i<n;i++){
vec v=normal(s[i+1]-s[i]);
point t=s[i+1]+v*0.5;
if(pointinpoly(t,s,n)==1) num++;
}
cout<<num<<endl;
return 0;
}
E. New Reform
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
Input
The first line of the input contains two positive integers, n and m — the number of the cities and the number of roads in Berland (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers xi, yi (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
Output
Print a single integer — the minimum number of separated cities after the reform.
Examples
input
4 3
2 1
1 3
4 3
output
1
input
5 5
2 1
1 3
2 3
2 5
4 3
output
0
input
6 5
1 2
2 3
4 5
4 6
5 6
output
1
题意:给出n个点m条边,你需要判断的是在你任意设定边的方向的情况下,使这个图内入度为0的点尽量少,输出最少有多少入度为0的点 其实我就是这样理解的
思路:两种情况
1:因为肯定存在环啊,有环的入度肯定不是0啊,所以与环相连的那一块区域对答案的贡献为0
2:没环的区域的话仔细想一下,肯定可以使入度为0的点变成一个啊,把它想象成一棵树直接拎起来,不就是只有一个根节点吗,所以这种情况对答案的贡献为1,
直接深搜当前的点所在的区域,判断环的时候直接看走到的点是否已经走过了,走过就是存在环,反之不存在
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 200100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m;
int head[MAX],vis[MAX],tot;
struct edge{
int v,next;
}e[MAX];
void init(){
mst(head,-1);
tot=0;
mst(vis,0);
}
void Add(int u,int v){
e[tot].v=v;
e[tot].next=head[u];
head[u]=tot++;
}
int ans=0,flag;
void DFS(int u,int fa){
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].v;
if(v==fa) continue;
if(!vis[v]) {
vis[v]=1;
DFS(v,u);
} else flag=1;
}
}
void solve(){
for(int i=1;i<=n;i++){
flag=0;
if(!vis[i]) {
vis[i]=1;
DFS(i,0);
if(flag==0) ans++;
}
}
cout<<ans<<endl;
}
int main(){
init();
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
Add(u,v);
Add(v,u);
}
solve();
return 0;
}