浙大月赛ZOJ Monthly, August 2014

Abs Problem


Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge


Alice and Bob is playing a game, and this time the game is all about the absolute value!

Alice has N different positive integers, and each number is not greater than N. Bob has a lot of blank paper, and he is responsible for the calculation things. The rule of game is pretty simple. First, Alice chooses a number a1 from the N integers, and Bob will write it down on the first paper, that‘s b1. Then in the following kth rounds, Alice will choose a number ak (2 ≤ k ≤ N), then Bob will write the number bk=|ak-bk-1| on the kth paper. |x| means the absolute value of x.

Now Alice and Bob want to kown, what is the maximum and minimum value of bN. And you should tell them how to achieve that!

Input

The input consists of multiple test cases;

For each test case, the first line consists one integer N, the number of integers Alice have. (1 ≤ N ≤ 50000)

Output

For each test case, firstly print one line containing two numbers, the first one is the minimum value, and the second is the maximum value.

Then print one line containing N numbers, the order of integers that Alice should choose to achieve the minimum value. Then print one line containing N numbers, the order of integers that Alice should choose to achieve the maximum value.

Attention: Alice won‘t choose a integer more than twice.

Sample Input

2

Sample Output

1 1
1 2
2 1

题意:找出序列使b[n]最小和最大;很容易发现最小值不是1就是0,最大值不是n就是n - 1;又由于这是特殊判断题,So。。。
#include<stdio.h>
#include<math.h>
int main()
{
    int N,i,j,max,min;
    while(scanf("%d",&N)!=EOF)
    {
        min=N%4;
        max=(N-1)%4;
        if(min==0||min==3)
            min=0;
        else min=1;
        if(max==0||max==3)
            max=0;
        else max=1;
        printf("%d %d\n",min,N-max);

        printf("%d",N);
        for(i=N-1; i>0; i--)
            printf(" %d",i);
        printf("\n");

        printf("%d",N-1);
        for(i=N-2; i>0; i--)
            printf(" %d",i);
        printf(" %d\n",N);
    }
    return 0;
}

比赛的时候一直纠结与绝对值,没好好思考。。。真是太失败了!

加油!!!

Machine


Time Limit: 2 Seconds Memory Limit: 65536 KB


In a typical assembly line, machines are connected one by one. The first machine‘s output product will be the second machine‘s raw material. To simplify the problem, we put all machines into a two-dimension shelf. Every machine occupied exactly one grid and has two input ports and only one output port. One input port can get material from only one machine.

Pipes will be used to connect between these machines. There are two kinds of pipes : ‘I‘ kind and ‘L‘ kind. We should notice that the ‘I‘ kind pipe can be linked one by one. Each pipe will also occupied one grid.

In Bob‘s factory, each machine will get raw materials from zero, one or two other machines. Some machines don‘t need any input materials, but any machine must have an output. Machines are coded by numbers from 1 to n. The output of the machines with greater code can be the input of the machines with less code. The machine NO.1‘s output product will be the final product, and will not be any other machine‘s input. Bob‘s factory has a shelf with infinite height, but finite width. He will give you the dependency relationship of these machines, and want you to arrange these machines and pipes so that he can minimize the width of the shelf.

Here‘s an example for you to help understand :

Products will falling from higher machine to lower machine through the pipes. Here, machine 1 gets materials from machine 2 and machine 3. The whole width of this system is 2.

Input

For each case, the first line will be an integer n indicates the number of the machines (2≤ n≤ 10000). The following line will include n-1 numbers. The i-th number ai means that the output of machine i+1 will be the input of machine ai (aii). The same code will be appeared at most twice. Notice machine 1‘s output will be the final output, and won‘t be any machine‘s input.

Output

For each case, we need exactly one integer as output, which is the minimal width of the shelf.

Sample Input

3
1 1
7
1 1 2 2 3 3

Sample Output

2
3

Hint

Case 1 is the example.
Case 2:

This problem contains massive input and output, please use efficient IO
methods. 

题意:就是说有2种管子,管子的高度可以无限长,但宽度一致,问最小的宽度;输入第i个数表示第i+1个容器将要输向哪个容器;

思路:其实就是dfs,每次dfs下去,把子树宽度保存下来,然后找最大值,如果有多个,就是最大值+cnt宽度;

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
vector<int>map[10005];
int dp[20005];
void dfs(int p)
{
    int i,j,flag,len;
    len=map[p].size();
    dp[p]=0;
    for(i=0;i<len;i++)
    {
        j=map[p][i];
        dfs(j);
        if(dp[p]<dp[j])flag=dp[j];//取大流
        else if(dp[p]==dp[j])flag=dp[p]+1;
        dp[p]=flag;
    }
    if(dp[p]==0)dp[p]=1;
}
int main()
{
  int n,a;
  while(scanf("%d",&n)>0)
  {
      for(int i=0;i<=n;i++)map[i].clear();
      for(int i=2;i<=n;i++)
      {
          scanf("%d",&a);
          map[a].push_back(i);
      }
      dfs(1);
      printf("%d\n",dp[1]);
  }
}

比赛的时候也没做出来,总是WA原因是找分支的时候应该取大的;

加油!!!

 
时间: 2024-10-22 02:41:49

浙大月赛ZOJ Monthly, August 2014的相关文章

135 - ZOJ Monthly, August 2014

135 - ZOJ Monthly, August 2014 A:构造问题,推断序列奇偶性.非常easy发现最小值不是1就是0.最大值不是n就是n - 1,注意细节去构造就可以 E:dp.dp[i][j]表示长度i,末尾状态为j的最大值,然后每一个位置数字取与不取,不断状态转移就可以 G:就一个模拟题没什么好说的 H:dfs,每次dfs下去,把子树宽度保存下来,然后找最大值,假设有多个.就是最大值+cnt宽度 I:构造,假设r * 2 > R,肯定无法构造.剩下的就二分底边.按等腰三角形去构造就

ZOJ Monthly, August 2014

H Machine http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337 树的深搜.邻接表快一些 1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include<algorithm> 5 #define mt(a,b) memset(a,b,sizeof(a)) 6 using namespace std; 7

记次浙大月赛 134 - ZOJ Monthly, June 2014

链接 虽做出的很少,也记录下来,留着以后来补..浙大题目质量还是很高的 B 并查集的一些操作,同类和不同类我是根据到根节点距离的奇偶判断的,删点是直接新加一个点,记得福大月赛也做过类似的,并差集的这类关系题目还是比较常见的,有空深究一下. 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6

ZOJ Monthly, June 2014——Grouping

题目连接 题意: n个点,m条边 每条边两个整数a.b,表示a到b的有向边 求,至少需要几个集合,使得:每个集合中的元素互相不能到达 N(1≤ N≤ 100000), M(1≤ M≤ 300000) 分析: 相连的两个点不能在同一个集合中,那么,对于一个长度为n的链,至少需要n个集合:如果链中有环,相当于把环展开,这个就需要缩点处理 就是缩点之后求点权最长路 注意:模板中scc_cnt是从1开始的,如果使用缩点后的图,初始化时需要初始化总点数加一 因为总点数有限,用拓扑排序每次删除所有入度为零的

ZOJ Monthly, June 2014 解题报告

A.Another Recurrence Sequence B.Gears 题目大意:有n个齿轮,一开始各自为一组,之后进行m次操作,包括以下4种类型: 1.合并两组齿轮,合并的两个应该反向旋转 2.把某个齿轮从所在组删除,自为一组,但不影响同组其它齿轮的状态与关系 3.询问两个齿轮是同向.反向或无关系(即不在同一组) 4.询问某个齿轮所在组的齿轮总数 分析:典型的并查集操作,但是注意两点: 1.由于操作3要询问两个齿轮的相对状态,因此对并查集中每个元素应当保存它的状态信息.状态是相对的,只需要

ZOJ Monthly, June 2014 月赛BCDEFGH题题解

比赛链接:点击打开链接 上来先搞了f.c,,然后发现状态不正确,一下午都是脑洞大开,, 无脑wa,无脑ce...一样的错犯2次.. 硬着头皮搞了几发,最后20分钟码了一下G,不知道为什么把1直接当成不能加油的站就会wa..太弱.. 唔···太懒第二天才发题解.. B:Gears 并查集 题解:点击打开链接 C:Consecutive Blocks 离散化一下然后模拟 题解:点击打开链接 D:An Easy Game 设dp[i][j]为前i个位置已经匹配了j个位置的方法数. #include <

ZOJ Monthly, November 2014

做了一次月赛,没想到这么难,加上后来补上的题目也只有3个题.第一名也只有4个题啊啊啊啊~.其中两道还是水题.留坑慢慢补上来. 3832 Tilt Cylinder 给定如图所示有盖圆柱体,R,H,水面高度h,倾角a,求水得体积. 分析:明显的数值积分题,这样考虑.圆下底面即A点与地面高度lim1, 圆上底面一点B与地面高度lim2,h所处的范围进行讨论从而确定积分几何体的两边的高度.我们积分的几何体应该是一个圆柱体被削掉一部分了. h>lim1时,几何体左半部分可以减掉一个圆柱,对剩下部分积分,

ZOJ Monthly, August 2012 题解

A: 题目链接:点击打开链接 Alice's present #include <cstdio> #include <iostream> #include <cstring> #include <string> #include <map> #include <queue> #include <set> #include <algorithm> using namespace std; int n, m; in

Incircle and Circumcircle(二分+几何)浙大月赛zoj3806(详解版)图

Incircle and Circumcircle Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A triangle is one the basic shapes in geometry. It's a polygon with three vertices and three sides which are line segments. A triangle with vertices A, B, C is denot