Codeforces Round #257 (Div. 2) 题解

Problem A

A. Jzzhu and Children

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There are n children in
Jzzhu‘s school. Jzzhu is going to give some candies to them. Let‘s number all the children from 1 to n.
The i-th child wants to get at least ai candies.

Jzzhu asks children to line up. Initially, the i-th
child stands at the i-th place of the line. Then Jzzhu start distribution of the candies. He follows the algorithm:

  1. Give m candies to the first child of the line.
  2. If this child still haven‘t got enough candies, then the child goes to the end of the line, else the child go home.
  3. Repeat the first two steps while the line is not empty.

Consider all the children in the order they go home. Jzzhu wants to know, which child will be the last in this order?

Input

The first line contains two integers n,?m (1?≤?n?≤?100; 1?≤?m?≤?100).
The second line contains n integers a1,?a2,?...,?an (1?≤?ai?≤?100).

Output

Output a single integer, representing the number of the last child.

Sample test(s)

input

5 2
1 3 1 4 2

output

4

input

6 4
1 1 2 2 3 3

output

6

传送门:点击打开链接

解体思路:简单模拟题,用队列模拟这个过程即可。

代码:

#include <cstdio>
#include <queue>
using namespace std;

typedef pair<int, int> P;
queue<P> q;

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("257Ain.txt", "r", stdin);
	#endif
	int n, m, ans = 0;
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; i++)
	{
		int x;
		scanf("%d", &x);
		q.push(P(x, i + 1));
	}
	while(!q.empty())
	{
		P p = q.front(); q.pop();
		if(p.first > m)
		{
			p.first -= m;
			q.push(p);
		}
		ans = p.second;
	}
	printf("%d\n", ans);
	return 0;
}

Problem B

B. Jzzhu and Sequences

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Jzzhu has invented a kind of sequences, they meet the following property:

You are given x and y,
please calculate fn modulo 1000000007 (109?+?7).

Input

The first line contains two integers x and y (|x|,?|y|?≤?109).
The second line contains a single integer n (1?≤?n?≤?2·109).

Output

Output a single integer representing fn modulo 1000000007 (109?+?7).

Sample test(s)

input

2 3
3

output

1

input

0 -1
2

output

1000000006

传送门:点击打开链接

解体思路:简单数学公式的推导,

f(n) = f(n-1) + f(n+1), f(n+1) = f(n) + f(n+2);

两式相加得:f(n-1) + f(n+2) = 0,

由上式可推得:f(n+2) + f(n+5) = 0;

由上两式得:f(n-1) = f(n+5),所以f(n)的周期为6;

我们只需求出f的前六项即可,ps:注意一点,f(n)可能为负值,对负数取模要先对负数加mod,使负数变为正数之后再取模。

代码:

#include <cstdio>

const int mod = 1000000007;

int main()
{
	#ifndef ONLINE_JUDGE
	//freopen("257Bin.txt", "r", stdin);
	#endif
	int n, a [7];
	scanf("%d%d%d", &a[0], &a[1], &n);
	for(int i = 2; i < 7; i++)
		a[i] = a[i - 1] - a[i - 2];
	int t = a[(n - 1)% 6];
	printf("%d\n",  t >= 0 ? t % mod : (t + 2 * mod) % mod);
	return 0;
}

Problem C

C. Jzzhu and Chocolate

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Jzzhu has a big rectangular chocolate bar that consists of n?×?m unit
squares. He wants to cut this bar exactly k times. Each cut must meet the following requirements:

  • each cut should be straight (horizontal or vertical);
  • each cut should go along edges of unit squares (it is prohibited to divide any unit chocolate square with cut);
  • each cut should go inside the whole chocolate bar, and all cuts must be distinct.

The picture below shows a possible way to cut a 5?×?6 chocolate
for 5 times.

Imagine Jzzhu have made k cuts
and the big chocolate is splitted into several pieces. Consider the smallest (by area) piece of the chocolate, Jzzhu wants this piece to be as large as possible. What is the maximum possible area of smallest piece he can get with exactly k cuts?
The area of a chocolate piece is the number of unit squares in it.

Input

A single line contains three integers n,?m,?k (1?≤?n,?m?≤?109; 1?≤?k?≤?2·109).

Output

Output a single integer representing the answer. If it is impossible to cut the big chocolate k times,
print -1.

Sample test(s)

input

3 4 1

output

6

input

6 4 2

output

8

input

2 3 4

output

-1

传送门:点击打开链接

解体思路:

n行m列,在水平方向最多切n-1刀,竖直方向最多切m-1刀,如果k>n+m-2,就是不能切割的情况;我们找出沿水平方向或竖直方向可以切的最多的刀数mx,如果k>mx,我们就现在这个方向切mx刀,剩下的就是要将一条长为(mn+1)巧克力切(k - mx)刀;其他的情况就是要么就是沿着水平方向切k刀,要么就是沿着竖直方向切k刀,取两者间的大者。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int n, m, k;
	long long ans = -1;
	cin >> n >> m >> k;
	if(k > n + m -2)
		ans = -1;
	else
	{
		int mx = max(n - 1, m - 1);
		int mn = min(n - 1, m - 1);
		if(k > mx)
			ans = (mn + 1) / (k - mx + 1);
		else
			ans = max(1ll * n / (k + 1) * m, 1ll * m / (k + 1) * n);
	}
	cout << ans << endl;
	return 0;
}
时间: 2024-10-19 04:11:46

Codeforces Round #257 (Div. 2) 题解的相关文章

Codeforces Round #257 (Div. 2)

A.Jzzhu and Children 计算每个人会出现的轮次数,取最后一个且轮次数最大的,注意是用a[i]-1 % m,倒着扫一遍就行了 #include <iostream> #include <cstdio> #include <cstdlib> using namespace std; const int maxn = 100+10; int n, m; int a[maxn]; int main() { #ifdef LOCAL freopen("

Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟

E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to

Codeforces Round #257 (Div. 2) A/B/C/D

前三题早就写好了,一直在纠结D A. Jzzhu and Children 题意:就是简单的模拟,给排成一队的孩子分发糖果,每个孩子有至少要得到的糖果数. 然后每次给队头的孩子分发m个糖果,如果他已经得到了足够的糖果(大于等于他想得到的 最少糖果数)那么他就出队,否则他就去队尾.问最后一个孩子的编号. 算法:队列模拟,水题~ #include<cstdio> #include<iostream> #include<cstring> #include<queue&g

Codeforces Round #257 div.2 D or 450D Jzzhu and Cities【最短路】

Codeforces Round #257 div.2 D or 450D Jzzhu and Cities[最短路] 题目链接:点击打开 题目大意: 在一个国家中有n个城市(城市编号1~n),m条公路和k条铁路,编号为1的城市为首都,为了节约,不需要的铁路需要关闭,问在保证首都到其余所有城市的最短路不变的条件下,最多有多少条铁路是不需要的. 解法: 这个题比较麻烦,保证首都到其余城市的最短路不变,要求出最多有多少条铁路是不需要的,那肯定是从最短路的代码上下手了,我们首先考虑dijkstra算法

Codeforces Round #262 (Div. 2) 题解

A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When

Codeforces Round #FF (Div. 2) 题解

比赛链接:http://codeforces.com/contest/447 A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output DZY has a hash table with p buckets, numbered from 0 to p?-?1. He wants to insert n 

Codeforces Round #259 (Div. 2) 题解

A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n?>?1) is an n?×?n 

Codeforces Round #177 (Div. 2) 题解

[前言]咦?现在怎么流行打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 mem

Codeforces Round #257(Div.2) D Jzzhu and Cities --SPFA

题意:n个城市,中间有m条道路(双向),再给出k条铁路,铁路直接从点1到点v,现在要拆掉一些铁路,在保证不影响每个点的最短距离(距离1)不变的情况下,问最多能删除多少条铁路 分析:先求一次最短路,铁路的权值大于该点最短距离的显然可以删去,否则将该条边加入图中,再求最短路,记录每个点的前一个点,然后又枚举铁路,已经删去的就不用处理了,如果铁路权值大于该点最短距离又可以删去,权值相等时,该点的前一个点如果不为1,则这个点可以由其他路到达,这条铁路又可以删去. 由于本题中边比较多,最多可以有8x10^