企鹅很忙系列~(可惜只会做3道题T_T)

A - A

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

SubmitStatusPracticeCodeForces 289A

Description

Little penguin Polo adores integer segments, that is, pairs of integers [lr] (l ≤ r).

He has a set that consists of n integer segments: [l1r1], [l2r2], ..., [lnrn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [lr] to either segment[l - 1; r], or to segment [lr + 1].

The value of a set of segments that consists of n segments [l1r1], [l2r2], ..., [lnrn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≤ x ≤ rj.

Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li andri ( - 105 ≤ li ≤ ri ≤ 105), separated by a space.

It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequality holds,min(ri, rj) < max(li, lj).

Output

In a single line print a single integer — the answer to the problem.

Sample Input

Input
2 31 23 4

Output
2

Input
3 71 23 34 7

Output
0这个题我看了好久才明白意思啊,英语真拙计,核心意思就是先给出一个数K,再给出n个区间,使得这n个区间的整数的个数和能整除k,如果不能,就要做改变,每一次改变只能增加一个整数,回答最少需要做改变的次数。

#include<stdio.h>
int main()
{int sum = 0,n,k,a,b,i,j;scanf("%d%d",&n,&k);while(n--){scanf("%d%d",&a,&b);sum += b - a + 1;}if(sum % k == 0)printf("0\n");else{printf("%d\n",k - sum %k);//k减去k的余数就是这个数还要加几才能达到k,该题中就是要改变的次数
    }return 0;
}

View Code

B - B

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

SubmitStatusPracticeCodeForces 289B

Description

Little penguin Polo has an n × m matrix, consisting of integers. Let's index the matrix rows from 1 to n from top to bottom and let's index the columns from 1 to m from left to right. Let's represent the matrix element on the intersection of row i and column j as aij.

In one move the penguin can add or subtract number d from some matrix element. Find the minimum number of moves needed to make all matrix elements equal. If the described plan is impossible to carry out, say so.

Input

The first line contains three integers nm and d (1 ≤ n, m ≤ 100, 1 ≤ d ≤ 104) — the matrix sizes and the d parameter. Next n lines contain the matrix: the j-th integer in the i-th row is the matrix element aij (1 ≤ aij ≤ 104).

Output

In a single line print a single integer — the minimum number of moves the penguin needs to make all matrix elements equal. If that is impossible, print "-1" (without the quotes).

Sample Input

Input
2 2 22 46 8

Output
4

Input
1 2 76 7

Output
-1

给一个M*N的矩阵及一个数D,小企鹅喜欢矩阵,但是喜欢每个数都一样,不过它每次都只能把矩阵中的数加或者减D,输出最少的改变次数,使矩阵的数变成一样的。如果不能达到目标就输出-1,否则输出最少的次数。用一维数组存矩阵好了(有时候不要被题目欺骗了,一维数组就能做的时候不要只想着开二维数组),然后把数组排序,中间的数就是所有的数希望变成的数(这样改变的次数才能最小)。然后计算次数。最后要说一下怎么判断这个数组不能达到目标呢。因为我写的代码是在存入数组的时候就要判断是否能达到目标,这个时候还不知道所有的数希望变成的数是什么。不如设所有的数希望变成的数是m好了,则a1 + n1 * d = m,a2 + n2 * d = m,两式相减可得(a1 - a2) / d = n2 - n1由此可以得到矩阵中任两个数的差一定是d的倍数,如果存在不是这样的数,则这个矩阵就不符合要求。

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int a[10030];
int main()
{int m,n,d,i,x,y = 0,sum = 0;cin >> m>> n>> d;for(i = 0;i < n*m;i++){cin >> a[i];if(i == 0)continue;else if((a[i] - a[i - 1]) % d != 0 && y==0){y = 1;}}if(y == 1)cout << -1<<endl;else{sort(a,a+n*m);int mid = n*m /2;for(i = 0;i < n*m;i++){x =  abs(a[mid] - a[i]);sum += x/d;}cout << sum<<endl;}return 0;
}

View Code

C - C

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status Practice CodeForces 289C

Description

Little penguin Polo adores strings. But most of all he adores strings of length n.

One day he wanted to find a string that meets the following conditions:

  1. The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct.
  2. No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn, then the following inequality holds, si ≠ si + 1(1 ≤ i < n).
  3. Among all strings that meet points 1 and 2, the required string is lexicographically smallest.

Help him find such string or state that such string doesn't exist.

String x = x1x2... xp is lexicographically less than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there is such number r(r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The characters of the strings are compared by their ASCII codes.

Input

A single line contains two positive integers n and k(1 ≤ n ≤ 106, 1 ≤ k ≤ 26) — the string's length and the number of distinct letters.

Output

In a single line print the required string. If there isn't such string, print "-1" (without the quotes).

Sample Input

Input
7 4

Output
ababacd

Input
4 7

Output
-1

小企鹅喜欢字符串,给定字符串长度n,字符串中还必须包括k个不同的字符,相邻字符还不一样。输出符合这些条件的最小字符(按字典序排列)。很明显字符串前面肯定是ab的组合,在结尾递增的加上剩余字符即可。这个题要注意的地方有 除了1 1输出是a外,其他k == 1时输出都为-1,因为不能用一个字符组成一个前后字符不相等的字符串。(虽然是很浅显的特殊情况,但有时候就是容易忽略,今后要避免这样的情况发生)。 不过这个题我要抱怨一下,一开始编译的时候,str[i] = str[i - 1] + 1;这行代码得到的不是我想要的结果,除了前面的ab其他的打印出来都是笑脸,你难道想让我陪你玩一会在提交?笑什么笑。。。不过后来在演示这一奇葩现象的时候又恢复了正常了。亏我在代码中还用了强制转换,其实根本用不上好吗。

#include<stdio.h>
char str[1000020];
int main()
{int n,k,i,j,m;scanf("%d%d",&n,&k);if(n == 1 && k==1)printf("a\n");else if(k > n || k==1)printf("-1\n");else if(k == n){for(i = 0;i < n;i++){if(i == 0)str[0] = 'a';elsestr[i] = (char)((int)str[i-1] +1);}str[n] = '\0';printf("%s\n",str);}else if(k < n){m =n - (k - 2);if(m % 2 == 0){for(i = 0;i < m-1;i = i+2){str[i] = 'a';str[i+1] = 'b';}for(;i < n;i++)str[i] = (char)((int)str[i-1] +1);}else{for(i = 0;i < m-1;i = i+2){str[i] = 'a';str[i+1] = 'b';}str[i++] = 'a';str[i++] = 'c';for(;i < n;i++)str[i] = (char)((int)str[i-1] +1);}str[n] = '\0';printf("%s\n",str);}return 0;
}

View Code

当然,这个题不用字符串存起来也行,可惜当时只想到用字符串的方法。

感觉自己写题解废话越来越多了,多写上了自己的感受,和wa的原因,主要还是因为写的这些东西都是为了留下纪念,所以废话多一点的话今后看起来比较亲切。若有路过的大神不要嫌弃啊(其实我这些渣题不会有大神来看的啦~)。

转载于:https://www.cnblogs.com/lwy-kitty/p/3206409.html

OUC_Summer Training_ DIV2_#11 722相关推荐

  1. 报错,贴图整理(1)

    这里是getElementByTagName("div")=============单复数的问题,给element加上s就可以了,仅仅是一个"s"的问题,把s加 ...

  2. JNI线程、Linux常用命令、权限、防火墙配置

    JNI_OnLoad: 调用System.loadLibrary()函数时, 内部就会去查找so中的 JNI_OnLoad 函数,如果存在此函数则调用. JNI_OnLoad会: 告诉 VM 此 na ...

  3. java se  计算机专业技能-Java专项练习(选择题)(三)

    1.关于Java语言的内存回收机制,下列选项中最正确的一项是 Java程序要求用户必须手工创建一个线程来释放内存 Java程序允许用户使用指针来释放内存 内存回收线程负责释放无用内存 内存回收线程不能 ...

  4. java选择题《每日一练》

    文章目录 (C#.JAVA)扩展方法能访问被扩展对象的public成员 正确答案: A 你的答案: A (正确) 能 不能 子类方法能否访问父类的public成员 Java 语言中创建一个对象使用的关 ...

  5. springboot Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory

    环境介绍 Spring Boot 2.0.2 Java 8 任务描述 由于Spring Boot 2.0 默认情况下是使用logback作为日志系统的,这里希望切换到log4j2. pom.xml内容 ...

  6. Objective-C---8---@property KVC

    1. @property: 1.1. 属性的声明及实现: 之前的@property只是负责setter和getter的声明,需程序员手动实现方法 : @property int age;   @syn ...

  7. 牛客网-java练习

    牛客网练习-java 1.编译 Java 源程序文件产生的字节码文件的扩展名为() 正确答案: B 你的答案: B (正确) java class html exe 解: java源文件的后缀名是.j ...

  8. NTC 100k的温感度采集。

    采用的是深圳市敏创电子的25℃下的温感阻值为100k:NTC 100K B值3950 R/ 每个型号和每个公司的的相同类型阻值表都不一样 硬件接线图如下 使用的是12位ADC  4096采集.本文只写 ...

  9. Python_Example_ Data Structures and Algorithm Analysis 学习/示例

    Author: 楚格 2018-11-19   19:05:11 IDE: Pycharm2018.02   Python 3.7 KeyWord :  Data Structures and Alg ...

  10. K-means和PAM聚类算法Python实现及对比

    K-means(K均值划分)聚类:简单的说,一般流程如下:先随机选取k个点,将每个点分配给它们,得到最初的k个分类:在每个分类中计算均值,将点重新分配,划归到最近的中心点:重复上述步骤直到点的划归不再 ...

最新文章

  1. [ CodeVS冲杯之路 ] P1044
  2. 超图数据集管理基本操作和添加删除属性表字段
  3. python支付系统开发,python支付整合开发包
  4. 电商ERP vs.传统ERP,有何不一样?
  5. 树链剖分 完美的想法
  6. Django加载静态文件
  7. 利用winrar自动备份重要资料
  8. LeetCode:67. 二进制求和(python、c++)
  9. string的operate+=
  10. Java中多态的实例
  11. snap7库C++版本对PLC数据的读写
  12. 2022-2028全球气动测试探针行业调研及趋势分析报告
  13. 作文 进入中职计算机班,我的中职生活作文(精选5篇)
  14. 郭天祥 十天搞定单片机 (2)流水灯+蜂鸣器+调试
  15. matlab中的index函数的使用方法,index函数语法说明及应用实例
  16. 互联网金融热浪下丨看看美国的互联网金融怎么玩?
  17. python地理处理包——Shapely介绍及用户手册
  18. python123用户登录c_写代码: 实现用户输入用户名和密码,当用户名为seven且密码为123时,显示登录成功,否则登录失败。...
  19. ESP8266固件烧录教程
  20. c语言实现lower_bound和upper_bound

热门文章

  1. iframe在ios设备宽度不能100%
  2. STL 关联容器的lower_bound()和upper_bound()
  3. Hibernate之复合主键映射
  4. linux 自动清理var log,Linux 系统 /var/log/journal/ 垃圾日志清理-Fun言
  5. matlab myupdatefcn,MATLAB笔记
  6. 将长方形木框拉成平行四边形_微课|人教版五年级数学上册6.1平行四边形的面积(P8690)...
  7. HP02: ssh: Could not resolve hostname hp02: Name or service not known HP01: ssh: Could not resolve h
  8. 新安装的centos使用ifconfig无效或者无法使用的解决办法
  9. pythonwin1064位_在Windows 10 64位中安装Matplotlib
  10. 计算机社团招新个人简历,大学社团招新面试自我介绍五篇