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.
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 one number — minimum distance in meters Winnie must go through to have a mealn times.
3 2 3 1
3
1 2 3 5
0
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));
}
}
}
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.
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.
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.
3 2 3 1 8 4
Yes 1 4
3 3 3 1 8 4
No
4 3 5 2 7 7 7
Yes 2 7 7
先贴一个过不了的代码以后在研究吧
/*
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");
}
}
其实超级简单的我在比赛的时候吧时间都浪费到了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]);
}
}
}