Codeforces Round #199 (Div. 2)

A. Xenia and Divisors
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Xenia the mathematician has a sequence consisting of n (n is divisible by 3) positive integers, each of them is at most 7. She wants to split the sequence into groups of three so that for each group of three a, b, c the following conditions held:

 

  • a < b < c;
  • a divides bb divides c.

 

Naturally, Xenia wants each element of the sequence to belong to exactly one group of three. Thus, if the required partition exists, then it has  groups of three.

Help Xenia, find the required partition or else say that it doesn't exist.

Input

The first line contains integer n (3 ≤ n ≤ 99999) — the number of elements in the sequence. The next line contains n positive integers, each of them is at most 7.

It is guaranteed that n is divisible by 3.

Output

If the required partition exists, print  groups of three. Print each group as values of the elements it contains. You should print values in increasing order. Separate the groups and integers in groups by whitespaces. If there are multiple solutions, you can print any of them.

If there is no solution, print -1.

Sample test(s)
input
6
1 1 1 2 2 2
output
-1
input
6
2 2 1 1 4 6
output
1 2 4
1 2 6

分情况讨论下就好。

只会出现

1 2 4

12 6

1 3 6

这几种情况。所以1的个数cnt[1]=n/3.而cnt[6]-cnt[3]+cnt[4]=cnt[2]注意cnt[6]>=cnt[3]就行了。

详细见代码:

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int cnt[10];
int main()
{
    int n,d,i;

    while(~scanf("%d",&n))
    {
        memset(cnt,0,sizeof cnt);
        for(i=0;i<n;i++)
        {
            scanf("%d",&d);
            cnt[d]++;
        }
        if(cnt[7]||cnt[5]||cnt[1]!=n/3||cnt[6]<cnt[3]||cnt[6]-cnt[3]+cnt[4]!=cnt[2])//开始没加cnt[6]<cnt[3]wa了一发
        {
            printf("-1
");
            continue;
        }
        for(i=0;i<cnt[4];i++)
            printf("1 2 4
");
        for(i=0;i<cnt[6]-cnt[3];i++)
            printf("1 2 6
");
        for(i=0;i<cnt[3];i++)
            printf("1 3 6
");
    }
    return 0;
}
B. Xenia and Spies
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.

Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.

But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ lirin). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.

You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).

Input

The first line contains four integers nms and f (1 ≤ n, m ≤ 105; 1 ≤ s, fnsfn ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ lirin). It is guaranteed that t1 < t2 < t3 < ... < tm.

Output

Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".

As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.

If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.

Sample test(s)
input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
output
XXRR


对于这道题。只能说题意太坑了。有可能我太菜了。。反正它的t要小心处理。而且最后他一定能传到所以。。。

他监视完后直接往一个方向传就行了。

详细见代码:

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int p,pre;
string way;
bool isok(int x,int l,int r)
{
    return x>r||x<l;
}
int main()
{
    int i,j,t,l,r,s,f,n,m,tmp;

    while(~scanf("%d%d%d%d",&n,&m,&s,&f))
    {
        p=s;
        pre=0;
        way="";
        for(i=0; i<m; i++)
        {
            scanf("%d%d%d",&t,&l,&r);
            if(f>p)
            {
                if(t-pre!=1)
                {
                    tmp=t-pre-1;
                    if(tmp>=f-p)
                    {
                        for(j=0;j<f-p;j++)
                            way+="R";
                        p=f;
                        continue;
                    }
                    else
                    {
                        for(j=0;j<tmp;j++)
                            way+="R";
                        p+=tmp;
                    }
                }
                if(isok(p,l,r)&&isok(p+1,l,r))
                {
                    way+="R";
                    p+=1;
                }
                else
                    way+="X";
            }
            else if(p>f)
            {
                if(t-pre!=1)
                {
                    tmp=t-pre-1;
                    if(tmp>=p-f)
                    {
                        for(j=0;j<p-f;j++)
                            way+="L";
                        p=f;
                        continue;
                    }
                    else
                    {
                        for(j=0;j<tmp;j++)
                            way+="L";
                        p-=tmp;
                    }
                }
                if(isok(p,l,r)&&isok(p-1,l,r))
                {
                    way+="L";
                    p-=1;
                }
                else
                    way+="X";
            }
            pre=t;
        }
        while(p<f)
        {
            way+="R";
            p++;
        }
        while(p>f)
        {
            way+="L";
            p--;
        }
        cout<<way<<endl;
    }
    return 0;
}
C. Cupboard and Balloons
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A girl named Xenia has a cupboard that looks like an arc from ahead. The arc is made of a semicircle with radius r (the cupboard's top) and two walls of height h (the cupboard's sides). The cupboard's depth is r, that is, it looks like a rectangle with base r and height h + rfrom the sides. The figure below shows what the cupboard looks like (the front view is on the left, the side view is on the right).

 

 

Xenia got lots of balloons for her birthday. The girl hates the mess, so she wants to store the balloons in the cupboard. Luckily, each balloon is a sphere with radius . Help Xenia calculate the maximum number of balloons she can put in her cupboard.

You can say that a balloon is in the cupboard if you can't see any part of the balloon on the left or right view. The balloons in the cupboard can touch each other. It is not allowed to squeeze the balloons or deform them in any way. You can assume that the cupboard's walls are negligibly thin.

Input

The single line contains two integers r, h (1 ≤ r, h ≤ 107).

Output

Print a single integer — the maximum number of balloons Xenia can put in the cupboard.

Sample test(s)
input
1 1
output
3
input
1 2
output
5
input
2 1
output
2


算是数学题吧。比赛时没做出来。

详细见代码:

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
const double eps=1e-10;
using namespace std;

int main()
{
    int r,h,ans;
    double rest;

    while(~scanf("%d%d",&r,&h))
    {
        rest=h%r;
        ans=(h/r)*2+1;
        if(rest>=0.5*r)
            ans+=1;
        if(rest>=0.5*sqrt(3.0)*r)
            ans+=1;
         printf("%d
",ans);
    }
    return 0;
}
E. Xenia and Tree
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Xenia the programmer has a tree consisting of n nodes. We will consider the tree nodes indexed from 1 to n. We will also consider the first node to be initially painted red, and the other nodes — to be painted blue.

The distance between two tree nodes v and u is the number of edges in the shortest path between v and u.

Xenia needs to learn how to quickly execute queries of two types:

 

  1. paint a specified blue node in red;
  2. calculate which red node is the closest to the given one and print the shortest distance to the closest red node.

 

Your task is to write a program which will execute the described queries.

Input

The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of nodes in the tree and the number of queries. Next n - 1 lines contain the tree edges, the i-th line contains a pair of integers ai, bi (1 ≤ ai, bin, aibi) — an edge of the tree.

Next m lines contain queries. Each query is specified as a pair of integers ti, vi (1 ≤ ti ≤ 2, 1 ≤ vin). If ti = 1, then as a reply to the query we need to paint a blue node vi in red. If ti = 2, then we should reply to the query by printing the shortest distance from some red node to node vi.

It is guaranteed that the given graph is a tree and that all queries are correct.

Output

For each second type query print the reply in a single line.

Sample test(s)
input
5 4
1 2
2 3
2 4
4 5
2 1
2 5
1 2
2 5
output
0
3
2


开始写了直接更新的。结果果断超时了。。后来用离线算法。1多的时候就直接找。2多的时候就更新。

据算这种算法不对。其它算法还不会。会了再补充吧。

详细见代码:

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int maxn=100100;
struct node
{
    int v;
    node *next;
} edge[maxn<<1],*head[maxn];
struct pp
{
    int fa;
    int u;
    int d;
};
int ptr,dis[maxn],op[maxn],x[maxn],cnt;
queue<pp> q;
void adde(int u,int v)
{
    edge[ptr].v=v;
    edge[ptr].next=head[u];
    head[u]=&edge[ptr++];
}
void dfs(int fa,int u,int d)
{
    int v;
    node *p=head[u];
    for(; p!=NULL; p=p->next)
    {
        v=p->v;
        if(v!=fa&&d<dis[v])
        {
            dis[v]=d;
            dfs(u,v,d+1);
        }
    }
}
void serch(int u)
{
    pp t;
    node *p;
    t.u=u;
    t.fa=u;
    t.d=0;
    while(!q.empty())
        q.pop();
    q.push(t);
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        if(dis[t.u]==0)
        {
            dis[u]=t.d;
            break;
        }
        for(p=head[t.u]; p!=NULL; p=p->next)
        {
            if(p->v!=t.fa)
            {
                pp tmp;
                tmp.d=t.d+1;
                tmp.fa=t.u;
                tmp.u=p->v;
                q.push(tmp);
            }
        }
    }
}
int main()
{
    int n,m,i,u,v;

    while(~scanf("%d%d",&n,&m))
    {
        memset(dis,0x3f,sizeof dis);
        memset(head,0,sizeof head);
        for(i=1; i<n; i++)
        {
            scanf("%d%d",&u,&v);
            adde(u,v);
            adde(v,u);
        }
        dis[1]=0;
        cnt=0;
        dfs(1,1,1);
        for(i=0; i<m; i++)
        {
            scanf("%d%d",&op[i],&x[i]);
            if(op[i]==1)
                cnt++;
        }
        if(cnt>m/2)
        {
            for(i=0; i<m; i++)
            {
                v=x[i];
                if(op[i]==1)
                    dis[v]=0;
                else
                {
                    serch(v);
                    printf("%d
",dis[v]);
                }
            }
        }
        else
        {
            for(i=0; i<m; i++)
            {
                v=x[i];
                if(op[i]==1)
                {
                    dis[v]=0;
                    dfs(v,v,1);
                }
                else
                    printf("%d
",dis[v]);
            }
        }
    }
    return 0;
}


原文地址:https://www.cnblogs.com/suncoolcat/p/3313349.html