A. Petr and a calendar
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Petr wants to make a calendar for current month. For this purpose he draws a table in which columns correspond to weeks (a week is seven consequent days from Monday to Sunday), rows correspond to weekdays, and cells contain dates. For example, a calendar for January 2017 should look like on the picture:

Petr wants to know how many columns his table should have given the month and the weekday of the first date of that month? Assume that the year is non-leap.

Input

The only line contain two integers m and d (1 ≤ m ≤ 12, 1 ≤ d ≤ 7) — the number of month (January is the first month, December is the twelfth) and the weekday of the first date of this month (1 is Monday, 7 is Sunday).

Output

Print single integer: the number of columns the table should have.

Examples
input
  1. 1 7
output
  1. 6
input
  1. 1 1
output
  1. 5
input
  1. 11 6
output
  1. 5
Note

The first example corresponds to the January 2017 shown on the picture in the statements.

In the second example 1-st January is Monday, so the whole month fits into 5 columns.

In the third example 1-st November is Saturday and 5 columns is enough.

题意:2017第m个月 当月第一天是星期d 问这个月的日历需要几列

题解:水

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <stack>
  7. #include <queue>
  8. #include <cmath>
  9. #include <map>
  10. #define ll __int64
  11. #define mod 1000000007
  12. #define dazhi 2147483647
  13. using namespace std;
  14. int m,d;
  15. int main()
  16. {
  17. int ans=;
  18. int exm;
  19. scanf("%d %d",&m,&d);
  20. if(m==||m==||m==||m==||m==||m==||m==)
  21. exm=;
  22. else
  23. {
  24. if(m==)
  25. exm=;
  26. else
  27. exm=;
  28. }
  29. exm=exm-(-d+);
  30. ans=ans+exm/;
  31. if(exm%)
  32. ans++;
  33. cout<<ans<<endl;
  34. return ;
  35. }
B. Frodo and pillows
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.

Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?

Input

The only line contain three integers nm and k (1 ≤ n ≤ m ≤ 109, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.

Output

Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.

Examples
input
  1. 4 6 2
output
  1. 2
input
  1. 3 10 3
output
  1. 4
input
  1. 3 6 1
output
  1. 3
Note

In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.

In the second example Frodo can take at most four pillows, giving three pillows to each of the others.

In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.

题意:n个床 m个垫子 Frodo 在第k个床上 要求相邻的两个床的相差的垫子的个数不能超过1 每个床上最少要有一个垫子  问 满足情况下 Frodo的床上最多有多少垫子

题解:二分Frodo床上的垫子数量 check 是否满足  很容想到以Frodo所在的床为中点向左向右的垫子数量都是递减的

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <stack>
  7. #include <queue>
  8. #include <cmath>
  9. #include <map>
  10. #define ll __int64
  11. #define mod 1000000007
  12. #define dazhi 2147483647
  13. using namespace std;
  14. ll n,m,k;
  15. ll fun(ll x)
  16. {
  17. x=x-;
  18. ll exm1,exm2,exm3;
  19. exm1=(+x)*x/;
  20. if((x-k)<=)
  21. exm2=;
  22. else{
  23. if((x-k)==)
  24. exm2=;
  25. else
  26. exm2=(+x-k)*(x-k)/;
  27. }
  28. if((x-(n-k+))<=)
  29. exm3=;
  30. else{
  31. if((x-(n-k+))==)
  32. exm3=;
  33. else
  34. exm3=(+(x-(n-k+)))*(x-(n-k+))/;
  35. }
  36. return n+exm1-exm2+exm1-exm3-x;
  37. }
  38. int main()
  39. {
  40. scanf("%I64d %I64d %I64d",&n,&m,&k);
  41. ll l=,r=1e9,mid;
  42. while(l<r)
  43. {
  44. mid=(l+r)>>;
  45. if(fun(mid)<=m)
  46. l=mid+;
  47. else
  48. r=mid;
  49. }
  50. if(fun(l)<=m)
  51. cout<<l<<endl;
  52. else
  53. cout<<l-<<endl;
  54. return ;
  55. }
C. Pavel and barbecue
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction.

Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions.

Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well.

There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2nplacements.

It can be shown that some suitable pair of permutation p and sequence b exists for any n.

Input

The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers.

The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers.

The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers.

Output

Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements.

Examples
input
  1. 4
    4 3 2 1
    0 1 1 1
output
  1. 2
input
  1. 3
    2 3 1
    0 0 0
output
  1. 1
Note

In the first example Pavel can change the permutation to 4, 3, 1, 2.

In the second example Pavel can change any element of b to 1.

题意:Pavel有n个地点烤肉,给你个数组p,p[i]代表可以从第i个位置到第p[i]个位置,p数组为n的一个全排列,又有个数组b,b[i]为1的时候,当Pavel走到第i个位置必须把这里的烤肉翻面。Pavel可以改变p数组或者b数组上的任意一个元素。问Pavel至少要改变多少个元素使得他可以把所有烤肉正反两面都烤一次。

题解:这题其实就是要先把路径改成一个环,因为只有成了环,Pavel才能走完两圈。由于p是全排列,因此这个路径本身就是一个或者多个环组合而成的。我们考虑n个环之间要合并,需要改动n条边,所以可以并查集或者dfs出环(联通块)的数量,这就是p数组需要改变的元素个数。然后我们考虑,假设最后合并成的环上,如果有奇数个1,Pavel就可以正反都烤到肉,如果是偶数个1就不行,因此判断下1的个数的奇偶性,就能得出b数组要改变多少次。需要特判环数为1的时候,p数组不需要改变。

#其实并不是很懂题意 看的别人的解释 orzzz#

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <stack>
  7. #include <queue>
  8. #include <cmath>
  9. #include <map>
  10. #define ll __int64
  11. #define mod 1000000007
  12. #define dazhi 2147483647
  13. using namespace std;
  14. int n;
  15. int p[];
  16. int b[];
  17. int fa[];
  18. int ans=;
  19. int exm=;
  20. int sum=;
  21. int find(int root)
  22. {
  23. if(fa[root]!=root)
  24. return fa[root]=find(fa[root]);
  25. else
  26. return root;
  27. }
  28. void uni(int a,int b)
  29. {
  30. int aa,bb;
  31. aa=find(a);
  32. bb=find(b);
  33. if(aa!=bb){
  34. fa[aa]=bb;
  35. sum--;
  36. }
  37. }
  38. int main()
  39. {
  40. scanf("%d",&n);
  41. for(int i=; i<=n; i++)
  42. scanf("%d",&p[i]);
  43. for(int i=; i<=n; i++)
  44. {
  45. scanf("%d",&b[i]);
  46. if(b[i]==)
  47. exm++;
  48. }
  49. sum=n;
  50. for(int i=; i<=n; i++)
  51. fa[i]=i;
  52. for(int i=; i<=n; i++)
  53. uni(p[i],i);
  54. if(sum==) sum=;
  55. if(exm%==)
  56. sum++;
  57. printf("%d\n",sum);
  58. return ;
  59. }

Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)A 水 B 二分 C并查集的更多相关文章

  1. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) E - Nikita and stack 线段树好题

    http://codeforces.com/contest/760/problem/E 题目大意:现在对栈有m个操作,但是顺序是乱的,现在每输入一个操作要求你输出当前的栈顶, 注意,已有操作要按它们的 ...

  2. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) D - Travel Card

    D - Travel Card 思路:dp,类似于单调队列优化. 其实可以写的更简单... #include<bits/stdc++.h> #define LL long long #de ...

  3. 8VC Venture Cup 2017 - Elimination Round

    传送门:http://codeforces.com/contest/755 A题题意是给你一个数字n,让你找到一个数字m,使得n*m+1为合数,范围比较小,直接线性筛出1e6的质数,然后暴力枚举一下就 ...

  4. 8VC Venture Cup 2017 - Elimination Round - C

    题目链接:http://codeforces.com/contest/755/problem/C 题意:PolandBall 生活在一个森林模型的环境中,定义森林由若干树组成,定义树为K个点,K-1条 ...

  5. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D. Factory Repairs 树状数组

    D. Factory Repairs 题目连接: http://www.codeforces.com/contest/635/problem/D Description A factory produ ...

  6. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) C. XOR Equation 数学

    C. XOR Equation 题目连接: http://www.codeforces.com/contest/635/problem/C Description Two positive integ ...

  7. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)B. sland Puzzle 水题

    B. sland Puzzle 题目连接: http://www.codeforces.com/contest/635/problem/B Description A remote island ch ...

  8. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A. Orchestra 水题

    A. Orchestra 题目连接: http://www.codeforces.com/contest/635/problem/A Description Paul is at the orches ...

  9. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)

    暴力 A - Orchestra import java.io.*; import java.util.*; public class Main { public static void main(S ...

随机推荐

  1. Siki_Unity_1-4_C#编程(零基础)

    1-4 C#编程(零基础) 任务1:第一章课程资料 任务2:简介 任务3:安装设置IDE工具 Unity内置IDE:MonoDevelop 推荐Visual Studio 下载/安装 VS Commu ...

  2. Java进阶知识点:协变与逆变

    一.背景 要搞懂Java中的协办与逆变,不得不从继承说起,如果没有继承,协变与逆变也天然不存在了. 我们知道,在Java的世界中,存在继承机制.比如MochaCoffee类是Coffee类的派生类,那 ...

  3. java length属性、length()、size()

    length属性 length是属性,用于说明数组的长度. String []list={"wo","shi","shuaibi"}; Sy ...

  4. 主题模型 LDA 入门

    主题模型 LDA 入门(附 Python 代码)   一.主题模型 在文本挖掘领域,大量的数据都是非结构化的,很难从信息中直接获取相关和期望的信息,一种文本挖掘的方法:主题模型(Topic Model ...

  5. tomcat下载、安装

    下载 官网地址:https://tomcat.apache.org/download-80.cgi 安装 直接安装即可.安装完毕后Tomcat的目录结构如下: bin:脚本目录 ​ 启动脚本:star ...

  6. UVa 455 - Periodic Strings - ( C++ ) - 解题报告

    1.题目大意 求一个长度不超过80的字符串的最小周期. 2.思路 非常简单,基本就是根据周期的定义做出来的,几乎不需要过脑. 3.应该注意的地方 (1) 最后输出的方式要注意,不然很容易就PE了.不过 ...

  7. apache访问403错误

    1.排查selinux 2.目录权限 3.WEB主目录是否正确

  8. lintcode-14-二分查找

    二分查找 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. 样例 在数组 ...

  9. LintCode-381.螺旋矩阵 II

    螺旋矩阵 II 给你一个数n生成一个包含1-n^2的螺旋形矩阵 样例 n = 3 矩阵为 [     [ 1, 2, 3 ],     [ 8, 9, 4 ],     [ 7, 6, 5 ] ] 标 ...

  10. Web界面和Winform界面生成,代码生成工具

    在上面一篇随笔<代码生成工具之界面快速生成>介绍了代码生成工具Database2Sharp的界面生成操作,其中介绍了Web界面(包括列表界面.内容显示.内容编辑界面的生成,另外还介绍了Wi ...