Codeforces Round #346 (Div. 2) A.B.C.D.E

A. Round House
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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.

Illustration for n = 6, a = 2, b =  - 5.

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


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
     int n,a,b;
     while(scanf("%d%d%d",&n,&a,&b)!=EOF)
     {
        int now=a+b;
        while(now<=0)
            now+=n;
        while(now>n)
            now-=n;
        printf("%d\n",now);
     }
     return 0;
}




B. Qualifying Contest
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

struct node
{
    char name[15];
    int  x;
    int fen;
}peo[100005];

int cmp(node a,node b)
{
    if(a.x<b.x)
        return 1;
    if(a.x>b.x)
        return 0;
    return (a.fen>b.fen);
}

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int i,j;
        for(i=1;i<=n;i++)
            scanf("%s%d%d",peo[i].name,&peo[i].x,&peo[i].fen);

        sort(peo+1,peo+1+n,cmp);

        for(i=1,j=1;i<=m;i++)
        {
            int num=0;
            int need=0;
            int ans=1;
            int st=j;
            int cc=0;
            while(peo[j].x==i)
            {
                need++;
                num++;
                if(need>2)
                {
                    if(peo[j].fen==peo[j-1].fen&&cc==0)
                       ans=0;
                    cc=1;
                }
                j++;
            }
            if(num<2)
                ans=0;
            if(ans==0)
                printf("?\n");
            else
                printf("%s %s\n",peo[st].name,peo[st+1].name);

        }


    }
    return 0;
}





C. Tanya and Toys
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int flag[100005];
int ans[10000005];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int i,j;
        memset(flag,0,sizeof(flag));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&flag[i]);
        }
        int num=0;

        sort(flag+1,flag+1+n);
        int st=1;
        for(i=1;  ;i++)
        {
            if(i==flag[st])
            {
                st++;
                continue;
            }
            if(m>=i)
            {
                num++;
                ans[num]=i;
                m-=i;
            }
            else
                break;
        }
        printf("%d\n",num);
        for(i=1;i<=num;i++)
        {
            if(i==num)
                printf("%d\n",ans[i]);
            else
                printf("%d ",ans[i]);
        }


    }
    return 0;
}



D. Bicycle Race
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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





这题我一开始想用扫描线算法来解决,因为会爆栈,所以用了malloc ,没想到爆堆了 ,23333

栈空间分静态分配和动态分配两种。静态分配是编译器完成的,比如自动变量(auto)的分配。动态分配由alloca函数完成。栈的动态分配无需释放(是自动的),也就没有释放函数。为可移植的程序起见,栈的动态分配操作是不被鼓励的!堆空间的分配总是动态的,虽然程序结束时所有的数据空间都会被释放回系统,但是精确的申请/释放内存是良好程序的基本要素。


我查了下函数中出现栈溢出的解决方案:

主要有两个办法:


一、改为堆变量:

int* pa = malloc(sizeof(int)*1000*1000);

然后可以将pa当数组用。(数组和指针在C里基本等同)

当然,不用了记得free。

 

二修改系统限制


我想了想选择go die


这是会爆内存的代码,写的有点乱

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stdlib.h>

using namespace std;
int n;
int x[1005],y[1005];

//int data[20005][20005];
//int flag[20005][20005];



int **flag=(int **)malloc(sizeof(int*)*20005);
int **data=(int **)malloc(sizeof(int*)*20005);


int minx,miny,maxx,maxy;
void saomiao ()
{
    int i,j;
    for(i=minx;i<=maxx;i++)
    {
        int num=0;
        int y1=999999999;
        for(j=maxy;j>=miny;j--)
        {
            if(flag[i][j])
                num++;
            if(num==1&&y1==999999999)
                y1=j;
            if(num==2)
            {
                for(int k=y1-1;k>=j;k--)
                    data[i][k]=1;
                num=0;
                y1=999999999;
            }
        }
    }
}

int jisuan()
{
    int ans=0;
    int i,j;
    int zx=x[1];
    int zy=y[1];
    for(i=2;i<=n+1;i++)
    {
        int nx=x[i];
        int ny=y[i];
        if(nx==zx)
        {
            if(ny>zy)
            {
                if(data[nx][ny]||data[nx-1][ny])
                    ans++;
            }
            else
            {
                if(data[nx-1][ny-1]||data[nx][ny-1])
                    ans++;
            }
        }
        else
        {
            if(nx>zx)
            {
                if(data[nx][ny-1]||data[nx][ny])
                    ans++;
            }
            else
            {
                 if(data[nx-1][ny-1]||data[nx][ny-1])
                    ans++;
            }

        }
        zx=nx;
        zy=ny;

    }
    return ans;
}

int main()
{
    for(int i=0;i<20002;i++)
    {
        flag[i]=(int *)malloc(sizeof(int)*20005);
    }
    for(int i=0;i<20002;i++)
    {
        data[i]=(int *)malloc(sizeof(int)*20005);
    }

    scanf("%d",&n);


        int i,j;
        minx=999999;
        miny=999999;
        maxx=-999999;
        maxy=-999999;
        memset(flag,0,sizeof(flag));
        memset(data,0,sizeof(data));
        int ux;
        int uy;
            scanf("%d%d",&x[1],&y[1]);
            x[1]+=1001;
            y[1]+=1001;
            if(x[1]>maxx)
                maxx=x[1];
            if(x[1]<minx)
                minx=x[1];
            if(y[1]>maxy)
                maxy=y[1];
            if(y[1]<miny)
                miny=y[1];
        ux=x[1];
        uy=y[1];
        for(i=2;i<=n+1;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            x[i]+=1001;
            y[i]+=1001;

            if(ux==x[i])
            {
             //   if(uy>y[i])
              //      for(int k=y[i];k<=uy;k++)
            //         flag[x[i]][k]=1;
             //   else
             //       for(int k=uy;k<=y[i];k++)
               //      flag[x[i]][k]=1;
            }
            else
            {
                if(ux>x[i])
                    for(int k=x[i];k<ux;k++)
                     flag[k][y[i]]=1;
                else
                    for(int k=ux;k<x[i];k++)
                     flag[k][y[i]]=1;
            }

            ux=x[i];
            uy=y[i];
            if(x[i]>maxx)
                maxx=x[i];
            if(x[i]<minx)
                minx=x[i];
            if(y[i]>maxy)
                maxy=y[i];
            if(y[i]<miny)
                miny=y[i];
        }
        memset(data,0,sizeof(data));
        saomiao();
        printf("%d\n",jisuan());

    return 0;
}




当然,我之后机智的发现 只要输出(n-4)/2就行了;

之前的白写了2333;

证明: 看感觉




#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
int n;

int main()
{

    while(scanf("%d",&n)!=EOF)
    {
        int i;
        int x,y;
        for(i=1;i<=n+1;i++)
            scanf("%d%d",&x,&y);
        
        printf("%d\n",(n-4)/2);
    }
    return 0;
}





E. New Reform
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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





证明:只要有环存在就可以保证相连接的所有点都可以有入度;
忘记写了个return,跑了n遍dfs,居然没有超时;
2333333;


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 200005

int first[maxn],next[maxn];
int from [maxn],to[maxn];
int visb[maxn];
int visd[maxn];
int fang[maxn];

int num=0;
int m,n;
int eid=0;
int wtf;

void init()
{

    num=0;
    memset(fang,0,sizeof(fang));
    memset(visd,0,sizeof(visd));
    memset(visb,0,sizeof(visb));
    eid=0;
    int i,j;
    for(i=0;i<=n;i++)
        first[i]=-1;
}

void addedge(int f , int t )
{
    eid++;
    to[eid]=t;
    next[eid]=first[f];
    first[f]=eid;

    eid++;
    to[eid]=f;
    next[eid]=first[t];
    first[t]=eid;
}


void dfs(int st)
{

    if(fang[st])
    {
        wtf=0;
        return;               //之前忘记写了居然没TLE
    }
    fang[st]=1;
    //printf("%d\n",st);
    int i,j;
    for(j=first[st];j!=-1;j=next[j])
    {
        if(visb[j])
            continue;
        if(j%2==0)
        {
            if(visb[j-1])
                continue;
        }
        else
        {
            if(visb[j+1])
                continue;
        }
        int t=to[j];
        if(visd[t]==0)
            num++;
        visd[t]=1;
        visb[j]=1;

        dfs(t);
    }
    return ;
}

int main()
{
    int ans=0;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        int i,j;
        for(i=1;i<=m;i++)
        {
            int f1,f2;
            scanf("%d%d",&f1,&f2);
            addedge(f1,f2);
        }
        for(i=1;i<=n;i++)
        {
            wtf=1;
            dfs(i);
            ans+=wtf;
        }
        printf("%d\n",ans);
    }
    return 0;
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值