Codeforces Round #441 (Div. 2)A + B + C

A. Trip For Meal
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. Winnie has got three best friends: Rabbit, Owl and Eeyore, each of them lives in his own house. There are winding paths between each pair of houses. The length of a path between Rabbit's and Owl's houses is a meters, between Rabbit's and Eeyore's house isb meters, between Owl's and Eeyore's house isc meters.

For enjoying his life and singing merry songs Winnie-the-Pooh should have a mealn times a day. Now he is in the Rabbit's house and has a meal for the first time. Each time when in the friend's house where Winnie is now the supply of honey is about to end, Winnie leaves that house. If Winnie has not had a meal the required amount of times, he comes out from the house and goes to someone else of his two friends. For this he chooses one of two adjacent paths, arrives to the house on the other end and visits his friend. You may assume that when Winnie is eating in one of his friend's house, the supply of honey in other friend's houses recover (most probably, they go to the supply store).

Winnie-the-Pooh does not like physical activity. He wants to have a meal n times, traveling minimum possible distance. Help him to find this distance.

Input

First line contains an integer n (1 ≤ n ≤ 100) — number of visits.

Second line contains an integer a (1 ≤ a ≤ 100) — distance between Rabbit's and Owl's houses.

Third line contains an integer b (1 ≤ b ≤ 100) — distance between Rabbit's and Eeyore's houses.

Fourth line contains an integer c (1 ≤ c ≤ 100) — distance between Owl's and Eeyore's houses.

Output

Output one number — minimum distance in meters Winnie must go through to have a mealn times.

Examples
Input
3
2
3
1
Output
3
Input
1
2
3
5
Output
0
Note

In the first test case the optimal path for Winnie is the following: first have a meal in Rabbit's house, then in Owl's house, then in Eeyore's house. Thus he will pass the distance2 + 1 = 3.

In the second test case Winnie has a meal in Rabbit's house and that is for him. So he doesn't have to walk anywhere at all.


A题的意思还是比较好理解的 比起第四题  是这样的 维尼小熊想去朋友家吃饭  然而 他跟他俩朋友是个三角形的地理位置分布 所以 如果他要吃n次饭问你走的最短距离是多少 如果你当前去了 b吃饭 再去a吃饭 然后你还能原路返回b 就这样 找找最短路是那一条边 然后特判下情况就好了


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int main()
{
	int n , a ,b , c;
	cin >> n >> a >> b >> c;
	if(n == 1) {
		printf("0\n");
	} else if (n == 2) {
		printf("%d\n",min(a , b));
	} else if (n >= 3) {
		int minn = min(min(a , b) , c);
		if(minn == c) {
			printf("%d\n",min(a , b) + c * (n - 2));
		}
		else {
			printf("%d\n",min(a , b) * (n - 1));
		}
	}
}


B. Divisiblity of Differences
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a multiset of n integers. You should select exactlyk of them in a such way that the difference between any two of them is divisible bym, or tell that it is impossible.

Numbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in multiset of selected numbers should not exceed the number of its occurrences in the original multiset.

Input

First line contains three integers n, k and m (2 ≤ k ≤ n ≤ 100 000,1 ≤ m ≤ 100 000) — number of integers in the multiset, number of integers you should select and the required divisor of any pair of selected integers.

Second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the numbers in the multiset.

Output

If it is not possible to select k numbers in the desired way, output «No» (without the quotes).

Otherwise, in the first line of output print «Yes» (without the quotes). In the second line printk integersb1, b2, ..., bk — the selected numbers. If there are multiple possible solutions, print any of them.

Examples
Input
3 2 3
1 8 4
Output
Yes
1 4 
Input
3 3 3
1 8 4
Output
No
Input
4 3 5
2 7 7 7
Output
Yes
2 7 7 


B题题目也比较好懂 是这样的  给你三个数组 分别是 n , k , m
然后给你n个数字  让你从中选出恰好k个  这k个数字两两之差取余m等于0 


这道题用同余定理比较好做  但是我第一眼看到这个题的时候用了一种很奇怪的方法
是这样的 我本来想开个桶排序的这种东西 然后去从零开始 每次枚举m个的倍数这样去搜索   
但是发现这个数字的大小是1e9 所以不能开桶排序   于是用字典树哈希 开一个1e6的数组来记录这些数字出现的次数 
真的是蠢的不行  思维定势太严重了

其实后来发现这道题根本不用这么麻烦  通过余数我们可以很容易的发现如果两个数字取余同一个数字得到的余数相同那么这两个数字的差取余这个数字j就是零了是不是很神奇
数论还是很神奇的~~~

先贴一个过不了的代码以后在研究吧

/*
3 2 3
1 8 4
Output
Yes
1 4
Input
3 3 3
1 8 4
Output
No
Input
4 3 5
2 7 7 7
Output
Yes
2 7 7
*/


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 100005;

int cnt , nums[N];// T[N];
int array[N];
int tree[N][10];

int Insert(int x)
{
    int i , k;
    i = k = 0;
    while (x) {
        int pos = x % 10;
        if(!tree[k][pos])
            tree[k][pos] = cnt ++;
        k = tree[k][pos];
        x /= 10;
    }
    nums[k]++;
    return k;
}
int Find(int x)
{
    int i , k;
    i = k = 0;
    while (x) {
        int pos = x % 10;
        if(!tree[k][pos])
            return 0;
        k = tree[k][pos];
        x /= 10;
    }
    return k;
}
int main()
{
	int n , k , m, temp, Max = -1;
	cnt = 1;
	scanf("%d%d%d",&n,&k,&m);
	for (int i = 0; i < N ; i ++) {
        array[i] = nums[i] = 0;
        for (int j = 0; j < 10; j ++) tree[i][j] = 0;
	}
	//memset(array , 0, sizeof(array));
//	memset(tree , 0, sizeof(tree));
	//memset(T , 0, sizeof(T));
//	memset(nums , 0, sizeof(nums));
	for (int i = 0; i < n; i ++) {
        scanf("%d",&array[i]);
        Max = max(Max , array[i]);
        int pos = Insert(array[i]);
  //      printf("cnt == %d\n",pos);
	}
	sort(array , array + n);
	bool flag = false;
	int P = 0;
	int kk = n / k;
	int aa[100000], A = 0; 
	for (int i = 0; i < kk; i ++) {
        int pos = array[i];
		int ans = 0;
		A = 0;
     //   printf("GHG");
	//	while (pos < Max && !T[Find(pos)])pos++;
        P = pos;
		while (ans < k) {//printf("pos == %d\n",pos);
		    if(pos > Max) break;
		 //   if(!nums[Find(pos)])break;
		 	int temp = nums[Find(pos)];
		 	if(temp) {
		 		ans += temp;
		 		aa[A ++] = pos;
			 }
			pos += m;
	//		printf("pos == %d\n",pos);
			if(pos > Max)break;
		}
		if(ans >= k) {
			flag = true;break;
		}
	}
	if(!flag) {
		printf("No\n");
	}
	else {
		printf("Yes\n");
		int done = 0;
		for (int i = 0; i < A && done < k; i ++) {
			for (int j = 0; j < nums[Find(aa[i])]; j ++) {
				printf("%d ",aa[i]);done++;
				if(done >= k)break;
			}
			if(done >= k)break;
		}
	}
}
/*
5 3 1
1 2 3 4 5
*/


下面这份是ac代码

#include <cstdio>
#include <cstring>

const int N = 1000005;
int index[N] , array[N] , ans[N] ,cnt = 0;
int main()
{

	int n , m , k;
	scanf("%d%d%d",&n,&k,&m);
	for (int i = 0; i < n; i ++) {
		scanf("%d",&array[i]);
		++index[array[i] % m];
	}
	for (int i = 0; i < m; i ++) {
		if(index[i] >= k) {
			for (int j = 0; j < n; j ++) {
				if(array[j] % m == i) {
					ans[cnt ++] = array[j];
				}
				if(cnt == k)break;
			}
		}
		if(cnt == k) {
			break;
		}
	}
	if(cnt == k) {
		printf("Yes\n");
		for (int i = 0; i < k ; i ++) {
			printf("%d ",ans[i]);
		}printf("\n");
	}
	else {
		printf("No\n");
	}
}

在写个c题 c题

其实超级简单的我在比赛的时候吧时间都浪费到了b上

感觉c没啥好说的 直接暴力就过了 真的是无脑  但是d题我根本读不懂题目 很奇怪 

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int digits(int x)
{
	if(x < 10)return 1;
	else return 1 + digits(x/10);
}

int sum(int x)
{
	int ans = 0;
	while (x) {
		ans += x % 10;
		x /= 10;
	}
	return ans;
}

int main()
{
	int n ;
	scanf("%d",&n);
	int array[10001] , cnt = 0;
	int dig = digits(n);
	//printf("%d\n",dig);
	for (int i = n - 1; i >= (n - dig * 10) && i > 0; i --) {
         //   printf("%d == %d\n",i,sum(i));
		if(i + sum(i) == n) {
			array[cnt ++] = i;
		}
	}
	if(cnt == 0) {
		printf("0");
	}
	else {
		printf("%d\n",cnt);
		for (int i = cnt - 1; i >=0; i --) {
			printf("%d ",array[i]);
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值