设为首页 加入收藏

TOP

Codeforces Round #177 (Div. 2) 题解(一)
2015-07-20 18:07:47 来源: 作者: 【 】 浏览:38
Tags:Codeforces Round #177 Div. 题解

【前言】咦?现在怎么流行打CF了?于是当一帮大爷在执着的打div 1的时候,我偷偷的在刷div 2。至于怎么决定场次嘛,一般我报一个数字A,随便再拉一个人选一个数字B。然后开始做第A^B场。如果觉得机密性不高,来点取模吧。然后今天做的这场少有的AK了。(其实模拟赛只做完了4题,最后1题来不及打了)

等等,话说前面几题不用写题解了?算了,让我难得风光一下啦。

【A】

A. Polo the Penguin and Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

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

He has a set that consists of n integer segments: [l1; r1],?[l2; r2],?...,?[ln; rn]. 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 [l; r] to either segment[l?-?1; r], or to segment [l; r?+?1].

The value of a set of segments that consists of n segments [l1; r1],?[l2; r2],?...,?[ln; rn] 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 test(s) input
2 3
1 2
3 4
output
2
input
3 7
1 2
3 3
4 7
output
0

这道题到底有什么意思呢?反正题目是模模糊糊的看懂的。好像是给定N个段和数K,你可以把某一段的L减一,把某一段的R加一,都算一次操作。最终要使得总长度%K=0,求最少操作次数。直接贴代码算了。

#include
  
   
using namespace std;
int n,k,i,ans,x,y;
int main()
{
  scanf("%d%d",&n,&k);
  for (i=1;i<=n;i++)
    scanf("%d%d",&x,&y),ans+=y-x+1;
  if (ans%k==0) puts("0");
  else printf("%d",k-ans%k);
  return 0;
}
  

【B】

B. Polo the Penguin and Matrix time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

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 n, m 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 test(s) input
2 2 2
2 4
6 8
output
4
input
1 2 7
6 7
output
-1

题意是给出N*M个带权方格,每次操作只能对每个格子+或-d。求把所有方块变成相同的权值最小操作次数,无解输出-1。判是否有解解很简单,只要权值A%d是否是定值,或者ΔA%d是否=0。如果有解的话一定是改成中位数。

#include
  
   
#include
   
     using namespace std; int a[100005],num,n,m,d,i,j,tot,ans; int main() { scanf("%d%d%d",&n,&m,&d); scanf("%d",&a[tot=1]);num=a[1]%d; for (i=1;i<=n;i++) for (j=1;j<=m;j++) { if (i==1&&j==1) continue; scanf("%d",&a[++tot]); if (a[tot]%d!=num) {puts("-1");return 0;} } sort(a+1,a+tot+1); for (i=1;i<=tot;i++) ans+=abs(a[i]-a[(tot+1)>>1])/d; printf("%d",ans); return 0; }
   
  

【C】

C. Polo the Penguin and Strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

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 strin
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++模版基于包含模型之外的显示实.. 下一篇leetcode――Search a 2D Matrix ..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: