Codeforces Round #278 (Div. 2)

54 篇文章 0 订阅
53 篇文章 0 订阅

链接:http://codeforces.com/contest/488


A. Giga Tower
time limit per test
1 second
memory limit per test
256 megabytes

Giga Tower is the tallest and deepest building in Cyberland. There are17 777 777 777 floors, numbered from - 8 888 888 888 to 8 888 888 888. In particular, there is floor0 between floor - 1 and floor1. Every day, thousands of tourists come to this place to enjoy the wonderful view.

In Cyberland, it is believed that the number "8" is a lucky number (that's why Giga Tower has8 888 888 888 floors above the ground), and, an integer islucky, if and only if its decimal notation contains at least one digit "8". For example,8,  - 180, 808 are alllucky while 42,  - 10 are not. In the Giga Tower, if you write code at a floor with lucky floor number, good luck will always be with you (Well, this round is #278, also lucky, huh?).

Tourist Henry goes to the tower to seek good luck. Now he is at the floor numbereda. He wants to find the minimumpositive integer b, such that, if he walksb floors higher, he will arrive at a floor with alucky number.

Input

The only line of input contains an integer a ( - 109 ≤ a ≤ 109).

Output

Print the minimum b in a line.

Sample test(s)
Input
179
Output
1
Input
-1
Output
9
Input
18
Output
10
Note

For the first sample, he has to arrive at the floor numbered180.

For the second sample, he will arrive at 8.

Note that b should be positive, so the answer for the third sample is10, not0.


分解找呗。。。


#include <cstdio>
#define ll long long

ll abs(int n)
{
	return n > 0 ? n : -n;
}

bool judge(ll n)
{
	while(n)
	{
		if(abs(n % 10) == 8)
			return true;
		n /= 10;
	}
	return false;
}

int main()
{
	ll n, cnt = 0;
	scanf("%I64d", &n);
	while(!judge(n + 1))
	{
		cnt++;
		n++;
	}
	printf("%I64d\n", cnt + 1);
}



B. Candy Boxes
time limit per test
1 second
memory limit per test
256 megabytes

There is an old tradition of keeping 4 boxes of candies in the house in Cyberland. The numbers of candies are special if their arithmetic mean, theirmedian and theirrange are all equal. By definition, for a set{x1, x2, x3, x4} (x1 ≤ x2 ≤ x3 ≤ x4)arithmetic mean is ,median is andrange isx4 - x1.The arithmetic mean and median are not necessary integer. It is well-known that if those three numbers are same, boxes will create a "debugging field" and codes in the field will have no bugs.

For example, 1, 1, 3, 3 is the example of4 numbers meeting the condition because their mean, median and range are all equal to2.

Jeff has 4 special boxes of candies. However, something bad has happened! Some of the boxes could have been lost and now there are onlyn (0 ≤ n ≤ 4) boxes remaining. Thei-th remaining box containsai candies.

Now Jeff wants to know: is there a possible way to find the number of candies of the4 - n missing boxes, meeting the condition above (the mean, median and range are equal)?

Input

The first line of input contains an only integer n (0 ≤ n ≤ 4).

The next n lines contain integersai, denoting the number of candies in thei-th box (1 ≤ ai ≤ 500).

Output

In the first output line, print "YES" if a solution exists, or print "NO" if there is no solution.

If a solution exists, you should output 4 - n more lines, each line containing an integer b, denoting the number of candies in a missing box.

All your numbers b must satisfy inequality1 ≤ b ≤ 106. It is guaranteed that if there exists a positive integer solution, you can always find suchb's meeting the condition. If there are multiple answers, you are allowed to print any of them.

Given numbers ai may follow in any order in the input, not necessary in non-decreasing.

ai may have stood at any positions in the original set, not necessary on lowestn first positions.

Sample test(s)
Input
2
1
1
Output
YES
3
3
Input
3
1
1
1
Output
NO
Input
4
1
2
2
3
Output
YES
Note

For the first sample, the numbers of candies in 4 boxes can be 1, 1, 3, 3. The arithmetic mean, the median and the range of them are all2.

For the second sample, it's impossible to find the missing number of candies.

In the third example no box has been lost and numbers satisfy the condition.

You may output b in any order.


分类讨论,等式化简一下,代码写的太丑了。。。

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

bool judge(int a, int b, int c, int d)
{
	if(a <= b && b <= c && c <= d)
		return true;
	return false;
}

int main()
{
	int n, a[4];
	scanf("%d", &n);
	for(int i = 0; i < n; i++)
		scanf("%d", &a[i]);
	if(n == 0)
		printf("YES\n1\n1\n3\n3\n");
	if(n == 1)
	{
		a[3] = 3 * a[0];
		a[1] = a[0];
		a[2] = 4 * a[0] - a[1];
		printf("YES\n%d\n%d\n%d\n", a[1], a[2], a[3]);
	}
	else if(n == 2)
	{
		sort(a, a + 2);
		if(4 * a[0] > a[1])
		{
			if(judge(a[0], a[1], 4 * a[0] - a[1], 3 * a[0]) || judge(a[0], 4 * a[0] - a[1], a[1], 3 * a[0]))
			{
				printf("YES\n%d\n%d\n", 4 * a[0] - a[1], 3 * a[0]);
				return 0;
			}
		}
		if(3 * a[0] == a[1])
		{
			printf("YES\n%d\n%d\n", a[0], 3 * a[0]);
			return 0;
		}
		if( (3 * (a[0] + a[1])) % 4 == 0)
		{
			if(judge((a[0] + a[1]) / 4, a[0], a[1], 3 * (a[0] + a[1]) / 4))
			{
				printf("YES\n%d\n%d\n", (a[0] + a[1]) / 4, 3 * (a[0] + a[1]) / 4);
				return 0;
			}
		}
		if( (4 * a[1] - 3 * a[0]) % 3 == 0)
		{
			if(judge(a[1] / 3, a[0], (4 * a[1] - 3 * a[0]) / 3, a[1] || judge(a[1] / 3, (4 * a[1] - 3 * a[0]) / 3, a[0], a[1])))
			{
				printf("YES\n%d\n%d\n", (4 * a[1] - 3 * a[0]) / 3, a[1] / 3);
				return 0;
			}
		}
		printf("NO\n");
	}
	else if(n == 3)
	{
		sort(a, a + 3);
		if(4 * a[0] == a[1] + a[2] && judge(a[0], a[1], a[2], 3 * a[0]))
		{
			printf("YES\n%d\n", 3 * a[0]);
			return 0;
		}
		if(3 * a[0] == a[2])
		{
			if(judge(a[0], a[1], 4 * a[0] - a[1], a[2]) || judge(a[0], 4 * a[0] - a[1], a[1], a[2]))
			{
				printf("YES\n%d\n", 4 * a[0] - a[1]);
				return 0;
			}
		}
		if(3 * (a[0] + a[1]) == 4 * a[2])
		{
			if(judge(a[2] / 3, a[0], a[1], a[2]))
			{
				printf("YES\n%d\n", a[2] / 3);
				return 0;
			}
		}
		printf("NO\n");
	}
	else if(n == 4)
	{
		sort(a, a + 4);
		int sum = a[0] + a[1] + a[2] + a[3];
		if(sum % 4 == 0 && (a[1] + a[2]) % 2 == 0)
			if(sum / 4 == (a[1] + a[2]) / 2 && sum / 4 == a[3] - a[0])
				printf("YES\n");
			else
				printf("NO\n");
		else
			printf("NO\n");
	}
}



C. Fight the Monster
time limit per test
1 second
memory limit per test
256 megabytes

A monster is attacking the Cyberland!

Master Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (HP), offensive power (ATK) and defensive power (DEF).

During the battle, every second the monster's HP decrease bymax(0, ATKY - DEFM), while Yang's HP decreases bymax(0, ATKM - DEFY), where indexY denotes Master Yang and indexM denotes monster. Both decreases happen simultaneously Once monster'sHP ≤ 0 and the same time Master Yang'sHP > 0, Master Yang wins.

Master Yang can buy attributes from the magic shop of Cyberland:h bitcoins perHP,a bitcoins perATK, andd bitcoins perDEF.

Now Master Yang wants to know the minimum number of bitcoins he can spend in order to win.

Input

The first line contains three integers HPY, ATKY, DEFY, separated by a space, denoting the initialHP,ATK andDEF of Master Yang.

The second line contains three integers HPM, ATKM, DEFM, separated by a space, denoting theHP,ATK andDEF of the monster.

The third line contains three integers h, a, d, separated by a space, denoting the price ofHP,ATK andDEF.

All numbers in input are integer and lie between 1 and 100 inclusively.

Output

The only output line should contain an integer, denoting the minimum bitcoins Master Yang should spend in order to win.

Sample test(s)
Input
1 2 1
1 100 1
1 100 100
Output
99
Input
100 100 100
1 1 1
1 1 1
Output
0
Note

For the first sample, prices for ATK and DEF are extremely high. Master Yang can buy99 HP, then he can beat the monster with1 HP left.

For the second sample, Master Yang is strong enough to beat the monster, so he doesn't need to buy anything.


暴力找最小。。


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

int main()
{
	int mHP, mATK, mDEF;
	int yHP, yATK, yDEF;
	int h, a, d;
	int m_hp, y_hp, cost, times, y_hp_need, ans = 2147483647;
	scanf("%d %d %d", &yHP, &yATK, &yDEF);
	scanf("%d %d %d", &mHP, &mATK, &mDEF);
	scanf("%d %d %d", &h, &a, &d);
	for(int i = max(yATK, mDEF + 1); i <= 200; i++)
	{
		for(int j = yDEF; j <= 200; j++)
		{
			m_hp = i - mDEF;  //每回合怪减少的血量
			y_hp = mATK - j;  //每回合人减少的血量
			times = ceil(mHP * 1.0 / m_hp); //需要打怪的次数
			y_hp_need = times * y_hp;   //人需要的血量
			cost = max(y_hp_need - yHP + 1, 0) * h + (i - yATK) * a + (j - yDEF) * d; //花费
			ans = min(ans, cost);
		}
	}
	printf("%d\n",ans); 
}



D. Strip
time limit per test
1 second
memory limit per test
256 megabytes

Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right.

Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:

  • Each piece should contain at least l numbers.
  • The difference between the maximal and the minimal number on the piece should be at most s.

Please help Alexandra to find the minimal number of pieces meeting the condition above.

Input

The first line contains three space-separated integers n, s, l (1 ≤ n ≤ 105, 0 ≤ s ≤ 109, 1 ≤ l ≤ 105).

The second line contains n integers ai separated by spaces ( - 109 ≤ ai ≤ 109).

Output

Output the minimal number of strip pieces.

If there are no ways to split the strip, output -1.

Sample test(s)
Input
7 2 2
1 3 1 2 4 1 2
Output
3
Input
7 2 2
1 100 1 100 1 100 1
Output
-1
Note

For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2].

For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.


两点法+dp


#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;

int const MAX = 1e5 + 5;
ll a[MAX], last = 0;
bool flag = true;

int main()
{
	ll n, s, l, ans = 0;
	scanf("%I64d %I64d %I64d", &n, &s, &l);
	for(int i = 0; i < n; i++) 
		scanf("%I64d", &a[i]);
	ll pos, left, right;
	for(pos = 0; pos < n; pos++)
	{
		ll maxm, minm, cnt = 1;

		maxm = a[pos];
		minm = a[pos];
		right = pos + 1;
		while(right < n)
		{
			maxm = max(maxm, a[right]);
			minm = min(minm, a[right]);
			if(maxm - minm <= s) 
				cnt++;
			else 
				break;
			right++;
		}
		right--;
	
		maxm = a[pos];
		minm = a[pos];
		left = pos - 1;
		while(left >= last)
		{
			maxm = max(maxm, a[left]);
			minm = min(minm, a[left]);
			if(maxm - minm <= s) 
				cnt++;
			else 
				break;
			left--;
		}
		left++;
		
		if(cnt < l)
		{
			flag = false;
			break;
		}
		ans++;
		last = left + l;
		pos = right;
	}
	if(!flag)
		printf("-1\n");
	else
		printf("%I64d\n", ans);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值