Menu Sidebar
Menu

Codeforces

Codeforces Round #308 (Div. 2) C. Vanya and Scales

原题: http://codeforces.com/contest/552/problem/C 题目大意: 给两个数字,w和m, 再给一堆砝码, 砝码的重量是w^0,w^1,w^2….w^100, 且每种只有1个.是否能放到天平上来称出m(天平平衡). 砝码能放天平的两边. 分析: 首先观察一下题, 一看就是进制问题, 上来先把m变成w进制. 然后我们知道每个砝码只有一个, 所以我们要考虑怎么放才能平衡, 举个栗子, m是7, w是3, 我用一个叫into的数组, 把m变成w进制, 变成后就是 1,2,0, 因为 1*3^0 + 2*3^1. 然后我们观察一下这个数字, 假设我们把m放在天平的左边, 那么相当于左边已经放上了1个3^0 和2个3^1, 通过观察我们发现, 如果数组元素为1, 那么我们可以通过直接放一个相同的砝码在右边的天平, 天平就可以平衡. 如果数组元素为0, 那么我们可以不放任何砝码在右边就能使天平平衡. 如果数组元素为w, 那么我们尝试去放一个比当前砝码大1的砝码到右边的天平上, 比如, 如果w=3, 数组的第二位,3^1的值是3, 那么3*3^1 = 3^2. 不失一般性的, 假设w = w, 数组第i位 w^i的值是w, 那么w*(w^i) = w^(i+1). 如果数组元素为w-1, 那么我们放当前砝码在左边, 并且尝试放一个大1的砝码在右边. 这时,相当于天平的右边欠了左边一个差为w^(i+1), […]

Codeforces Round #308 (Div. 2) B. Vanya and Books

原题:http://codeforces.com/contest/552/problem/B 题目大意: 给个数字n, 求这n个数字的digits个数的合 分析: 比如1234, digit是1的区间是[1,9], 一共9个, 2的区间是[10,99]一共90*1个digit, 3的区间是[100,999]一共900*2个digit, n的区间是[10^n,10^(n+1)-1]一共10^(n+1) – 10^(n)*(n-1)个digit. 那么1234 落在区间4, [1000,9999]中. 可以用1234-1000+1得到4个digit的数字的个数, 然后乘以4,得到digit为4的digit数, 然后与前边的区间的合相加, 就是答案 第一次写: public void solve(int testNumber, InputReader in, OutputWriter out) { long n = in.readLong(); long t = n; long res = 0; int digits = 1; while (n / 10 != 0) { res += (9 […]

Codeforces Round #308 (Div. 2) A. Vanya and Table

原题:http://codeforces.com/contest/552/problem/A 题目大意: 给[1,100]个格子, n个矩阵, 问n个矩阵覆盖格子的合是多少? 分析: 乘一下就出来了 public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); int res = 0; for (int i = 0; i < n; i++) { int x1 = in.readInt(); int y1 = in.readInt(); int x2 = in.readInt(); int y2 = in.readInt(); res += (x2-x1+1) * (y2-y1+1); […]

Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

原题: http://codeforces.com/contest/556/problem/C 题目大意: 俄罗斯套娃, 给n个套娃, 套成k组, 每组用数字表示, 大的数字套小的数字, 问怎么把它们套在一起. 分析: 这题想了好久哇. 因为开始读题不仔细, 所以开始想的时候各种WA. 我以为: 1 2 3 4 5 6 这种情况只需要一秒. 但是仔细看题才发现, 这个需要先把6拆下来, 然后把5 套在4上, 然后把6套在5上, 所以是三步. 这个地方卡了好久. 剩下的思路很明确: 首先找到最小的套娃1, 然后往后扫, 看看按照1 2 3 4 ….n这个顺序有几个. 因为把n个套娃套上需要n-1秒,所以坏情况, 就是我们拆下所有的套娃用n-1秒, 然后套上去, 用n-1秒, 这里的-1的原因是编号1的套娃不用动. 所以最坏下是n-1+n-1 = 2n-2秒. 然后我们用排除法, 这里需要把拆开和组装分开记录,一种是 一个链子中为: 1 2 3 4 5 …. 这种情况下, 我用变量b记录了有多少个不用组装的套娃. 第二种是, […]

Codeforces Round #309 (Div. 2) B. Ohana Cleans Up

原题:http://codeforces.com/contest/554/problem/B 题目大意: 给个n x n的binary矩阵, 只能翻转(0->1,1->0)列, 问如何翻转, 让全是1行的总数最大. 分析: 因为每次翻转列的时候, 每个行都要翻转, 所以只要找到最大数目的相同的行. 就可以了. public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); HashMap<String, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { String s = in.readString(); if (!map.containsKey(s)) map.put(s,0); map.put(s,map.get(s)+1); } int max = 0 […]

Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks

原题:http://codeforces.com/contest/554/problem/A 题目大意: 给一个string,表示一个相册页, 左右都能放photo, 问几种方法. 分析: n个相册夹, 左右都能放, n+1种地方, 26种photo, (n+1)*26, 当然会有重复, 重复是n个. 减去重复的就是答案 public void solve(int testNumber, InputReader in, OutputWriter out) { String s = in.readString(); int n = s.length(); out.print((n+1)*26 – n); }

Codeforces Round #309 (Div. 1) A. Kyoya and Colored Balls

原题:http://codeforces.com/contest/553/problem/A 题目大意: k个颜色, n个球, 球之间没区别. 问怎么放能至少让一个i个颜色的球排在i+1的颜色的球的前边. 分析:这题目看起来是很拗口, 我看着发愣了一会儿才明白. 颜色是有顺序的.第i+1颜色的前边至少要有一个i颜色的球, 这个i不是固定的. 1<=i<=k-1, 所以题目也可以解释成,至少一组球是有序的, 这个有序不一定是连续的球. 因为i表示的颜色是个范围, 所以要用dp来做, 假设i = 1, 这时候就一种颜色, 因为球和球之间无区别, 所以答案是1. i = 2的时候, 因为要保持以上的条件, 所以我们选一个颜色为2的球,放到最后, 然后其他的无所谓. (点一下,有清楚的图) public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); int[] nums = IOUtils.readIntArray(in,n); int max = 1002; long[][] bio = new long[max][max]; for (int […]

Codeforces Round #313 (Div. 2) D. Equivalent Strings

原题:http://codeforces.com/contest/560/problem/D 题目大意: 给字符串a和b, 定义a和b是等价的,如果: a和b相等. a等分成a1,a2,b等分成b1,b2, (a1==b1 && a2==b2) || (a2==b1 && a1==b2) 分析: 就暴呗…我也不知道为什么我暴就报错, 是因为java的原因么, 我看人家同样码用c++, 都飞快的. public void solve(int testNumber, InputReader in, OutputWriter out) { String a = in.readString(); String b = in.readString(); if (solve(a,b)) out.print(“YES”); else out.print(“NO”); } private boolean solve(String a, String b) { if (a.length() % 2 != 0 || […]

Codeforces Round #313 (Div. 2) B. Gerald is into Art

原题: http://codeforces.com/problemset/problem/560/B 题目大意:给三个矩形ABC,问能否把B和C放到A里 分析: 这题做了有40分钟, 生怕考虑不周. 其实就是从从ABC各挑出一条边,如果B,C相加小于A,并BC的另一条边分别小于A另一条边的长度. 那么就能放下. 别的放不下. 所以共2*2*2 = 8种情况. public void solve(int testNumber, InputReader in, OutputWriter out) { int[] a = IOUtils.readIntArray(in, 2); int[] b = IOUtils.readIntArray(in, 2); int[] c = IOUtils.readIntArray(in, 2); Arrays.sort(a); Arrays.sort(b); Arrays.sort(c); if (a[0] >= b[0]+c[0] && a[1] >=b[1] && a[1] >= c[1]) { out.print(“YES”); return; } else if […]

Codeforces Round #313 (Div. 2) A. Currency System in Geraldion

原题:http://codeforces.com/problemset/problem/560/A 题目大意: 一个国家, 发行n个不同面值的货币, 问他们国家能不能用这些货币找开所有的钱, 能返回-1, 不能返回最小的不能找开的钱的价值. 分析: 唉, 脑筋急转弯. 坑了我10分钟啊. 只要发行1元, 就能找开所有钱, 没有发行1元,1元本身就是最小的找不开的钱. 这次round的时间是特殊时间,估计是没睡醒.啃着面包做的. public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); int[] nums = IOUtils.readIntArray(in,n); Arrays.sort(nums); if (nums[0] == 1){ out.print(“-1”); return; }else { out.print(“1”); } }

Newer Posts
Older Posts

书脊

这青苔碧瓦堆, 俺曾睡风流觉, 将五十年兴亡看饱.

February 2025
M T W T F S S
 12
3456789
10111213141516
17181920212223
2425262728