#16(total 5 problems) gdgzoi.com-contest2286【C++】

200 篇文章 7 订阅
31 篇文章 0 订阅
 

第一题:切蛋糕

Description
小明今天生日,邀请了一些朋友过来开 生日会。妈妈专门去买了一个大蛋糕,蛋糕为一个n*m的矩形,现在想把这个蛋糕分成1*2的小块,并且要求必须是完整的小块,不能拼接。问一共能分多少块?
Input
一行,两个正整数n,m(oj中长整形要用%lld来进行输入输出,清橙测试时用的是%I64d,比赛时如果题目没做要求,用那种方式输入输出都可以)
Output
一行,一个整数x,表示最多能分多少块。
Sample Input
7 8
Sample Output
28
HINT
50% 数据 0<n,m<10000
100% 数据 0<n,m<10^9

分析:这么简单······floor(n*m/2)即可,不多说

 
#include <iostream>

using namespace std;

int main()
{
	long long a, b, res;
	
	cin >> a >> b;
	
	res = a * b / 2;
	
	cout << res << endl;
	
	return 0;
}

第二题:谁会赢

Description
kqp发明了一个好玩的游戏,叫czy一起玩。但czy玩了十几盘,总是输,他想知道是不是从一开始他就注定要输。
这个游戏是这样的,kqp先写下一排数(既然是一排,当然有首尾咯)。
kqp和czy每次只能从这排数的头或尾取一个数。最后谁取的数的和多,谁就赢了。如果两人的数的总和一样多,先取者胜。
有天FW看到他们俩在玩这个游戏,很好奇。他想知道,在两人总是做出最优决策的情况下(两个人的智商都是很高的……),谁能取得最终的胜利呢?

Input
第一行为一个数k(k<=10),表示有k组测试数据;
以下k组测试数据:
每组测试数据中,第一行仅有一个偶数n(0<n<=100000),第二行也仅有一个数,0表示kqp先取数,1表示czy先取数,第三行有n个数,是kqp给出的一排数。这n个数的绝对值均不超过10^6。

Output
对每组测试数据输出一行,表示在两人总是做出最优决策的情况下,最终的胜利者的名字,即"kqp"或"czy"(引号不输出)。
Sample Input
2
2
1
1 3
2
0
1 3
Sample Output
czy
kqp
HINT
【数据范围】
30%,k=1,n<=10; 100%,如题所述。

分析:先把这些格子按照1到n排好,奇数格染黑色,偶数格染白色······

经过一些高(jian)深(dan)的推理,可以得出两个人总是一个取所有黑格内的数,一个取所有白格内的数······

因为n是偶数,所以一开始能取的一定是一黑一白······

先手只需比较所有黑格和白格内的数的总数,就能赢了······

所以先手一定赢。

真是水题一发。

#include <iostream>
#include <string>

#define SIZE 100005

using namespace std;

string s[2] = {"kqp", "czy"};

int main()
{
	int k, i, j, t, w;
	
	cin >> k;
	while (k--)
	{
		cin >> j >> w;
		for (i = 1; i <= j; i++)
		{
			cin >> t;
		}
		cout << s[w] << endl;
	}
	
	return 0;
}

第三题:阿尔法狗

Description
最近备受关注的人机大战——谷歌机器人AlphaGo对战围棋大师李世石。经过五盘的对决,最终AlphaGo以4:1战胜李世石,并且使得它的排名一举上升为世界第二,仅次于中国选手柯洁。为了准备迎接柯洁的挑战,必须让AlphaGo提升自身的处理能力,但由于时间有限,仅能临时采购一些性能不一的处理器,现在知道每种处理器的处理能力和发热量,由于机器过热可能会导致AlphaGo程序崩溃,必须要控制好它的最大发热量才行,这个艰巨的任务落在你的头上,必须选出一些处理器来尽可能的提供最强的处理能力。
Input
第一行两个正整数n,t,表示可选择的处理器种类和最大发热量,注意,每种处理器可以采购多个
接下来n行,每行两个正整数,分别表示每种处理器的处理能力和发热量(数值均小于100)

Output
一行,一个正整数,表示AlphaGo的最大处理能力。
Sample Input
3 5
2 2
4 3
1 5
Sample Output
6
HINT
50% 数据 n<=30
100% 数据 n<=300,t<=10000

分析:完全背包······

#include <iostream>

#define SIZE 10005
#define NUM 501

using namespace std;

int v[NUM], c[NUM], res[SIZE];

int main()
{
	int n, t, i, j;
	
	cin >> n >> t;
	for (i = 1; i <= n; i++)
	{
		cin >> c[i] >> v[i];
	}
	
	for (i = 1; i <= n; i++)
	{
		for (j = v[i]; j <= t; j++)
		{
			res[j] = max(res[j], res[j-v[i]] + c[i]);
		}
	}
	
	cout << res[t] << endl;
	
	return 0;
}

第四题:数字分组

Description
小明的数学计算能力超强,常常在同学们面前表面得很骄傲。数学科代表实在看不下去了,决定出道很麻烦的题,好好“折磨”他一下。
数学科代表决定给他一些数,让他分组。从第一个数开始分组,且每组必须是连续的一段数,要求每组和相等,问每组和最小可以是多少。(当然这些数一定可以被分组,大不了直接分成一组。)

Input
第一行为一个数N

第二行为N个整数(每个数均小于等于1000),两个数间用空格隔开。

测试点

n

1

n = 10

2

n = 100

3

n = 1000

4

n = 200000

5

n = 200000

6

n = 1000000

7

n = 1000000

8

n = 1000000

9

n = 1000000

10

n = 1000000

Output
一行,最小的和

Sample Input
样例输入1:
6 
2 5 1 3 3 7 

样例输入2:
6 
1 1 2 3 2 3 
Sample Output
样例输出1:
7

样例输出2:
12
HINT
【样例1说明】
分成三组(2,5) (1,3,3) (7) 和为7,不存在比7更小的和。

分析:用check函数顺序查找,硬是得了100分暴力分。

#include <iostream>

#define SIZE 1000001

using namespace std;

int a[SIZE], n;

bool check(int k)
{
	int i, sum = 0;
	
	for (i = 1; i <= n; i++)
	{
		sum += a[i];
		if (sum > k)
		{
			return false;
		}
		if (sum == k)
		{
			sum = 0;
		}
	}
	
	return !sum;
}

int main()
{
	int i, res = -1, l = 0, r = 0, mid;
	bool flag = false;
	
	cin >> n;
	for (i = 1; i <= n; i++)
	{
		cin >> a[i];
		r += a[i];
	}
	
	for (i = 1; i <= r; i++)
	{
		if (r % i == 0)
		{
			if (check(i))
			{
				break;
			}
		}
	}
	
	cout << i << endl;
	
	return 0;
}

第五题:兰姐姐的姐姐

Description
兰姐姐是来自火星的女王。相信你们一定对兰姐姐不熟悉,她统领整个火星,在各方面拥有最高权力。很久很久以前,兰爸爸是火星的国王,去世以后,两个女儿争夺王位。火星上最聪明的人是辣椒酱,他帮助兰姐姐夺得了王位,而兰姐姐的姐姐Horse没有得到王位,便离开火星前往地球修行。
几年后,兰姐姐越来越思念姐姐,便决定到地球上找姐姐。
今天,她找到了自己失散已久的姐姐Horse的家,但是要进门就必须答对一个大难题,作为一个大犇犇犇,她很快就解出来了,你行吗?
题目是这样的:
现在有一个序列a,a的长度为n,一开始a[i]=i(1≤i≤n),现在有m个操作,每个操作的格式是这样的:x y表示把当前的a[x]与a[y]交换。我们把这m个操作叫做一轮操作,现在问,在经过多少轮操作之后,序列a又会回到原来的样子(原来的样子就是指a[i]=i(1≤i≤n))

Input
第一行,两个整数n,m,n表示a的长度,m表示操作数
接下来m行,每行一个操作x y,表示把当前的ax与ay交换保证(1≤x,y≤n)

Output
只有一个数,表示在经过多少轮之后,序列a又会回到原来的样子
Sample Input
样例输入1:
4 4
1 4
3 4
2 3
1 4
样例输入2:
5 3
1 2
2 3
4 5
Sample Output
样例输出1:
3
样例输出2:
6
HINT
50%数据保证1≤n,m≤1000,答案小于等于1000
100%数据保证1≤n,m≤500000,答案小于等于2^31-1

起先总是超时,兰姐姐的脑子里面是装了台电脑吗(暴力分:80)······

后来发现很简单

#if 0
4 4
1 4
3 4
2 3
1 4

5 3
1 2
2 3
4 5
#endif

#include <iostream>

#define SIZE 500001

using namespace std;

int a[SIZE], temp[SIZE], t[SIZE];
bool b[SIZE];

int func(int n, int s, int k)
{
	b[n] = true;
	if (n == s)
	{
		return k;
	}
	t[n] = func(temp[n], s, ++k);
	
	return t[n];
}


int gcd(int a, int b)
{
	return (a % b) ? gcd(b, a % b) : b;
}

int main()
{
	int n, m, i, res;
	
	cin >> n >> m;
	for (i = 1; i <= n; i++)
	{
		temp[i] = a[i] = i;
	}
	while (m--)
	{
		cin >> i >> res;
		swap(temp[i], temp[res]);
	}
	
	for (i = 1; i <= n; i++)
	{
		if (!b[i])
		{
			if (temp[i] == i)
			{
				continue;
			}
			func(temp[i], i, 1);
		}
	}
	res = 1;
	for (i = 1; i <= n; i++)
	{
		if (t[i] == 0)
		{
			continue;
		}
		res = res / gcd(res, t[i]) * t[i];
	}
	
	cout << res << endl;
	
	return 0;
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
The problem statement can be found at Codeforces website. Approach: Let's start by looking at some examples: - 1, 2, 3, 4, 5 → No moves needed. - 2, 1, 3, 5, 4 → One move needed: swap index 1 and 2. - 5, 4, 3, 2, 1 → Two moves needed: swap index 1 and 5, then swap index 2 and 4. We can observe that in order to minimize the number of moves, we need to sort the array in non-descending order and keep track of the number of swaps we make. We can use bubble sort to sort the array and count the number of swaps. Let's see how bubble sort works: - Start from the first element, compare it with the second element, and swap them if the second element is smaller. - Move to the second element, compare it with the third element, and swap them if the third element is smaller. - Continue this process until the second-to-last element. At this point, the largest element is in the last position. - Repeat the above process for the remaining elements, but exclude the last position. In each iteration of the above process, we can count the number of swaps made. Therefore, the total number of swaps needed to sort the array can be obtained by summing up the number of swaps made in each iteration. Implementation: We can implement the above approach using a simple bubble sort algorithm. Here's the code: - First, we read the input array and store it in a vector. - We define a variable to keep track of the total number of swaps made and set it to 0. - We run a loop from the first element to the second-to-last element. - In each iteration of the above loop, we run another loop from the first element to the second-to-last element minus the current iteration index. - In each iteration of the inner loop, we compare the current element with the next element and swap them if the next element is smaller. - If a swap is made, we increment the total number of swaps made. - Finally, we output the total number of swaps made. Time Complexity: The time complexity of bubble sort is O(n^2). Therefore, the overall time complexity of the solution is O(n^2). Space Complexity: We are using a vector to store the input array. Therefore, the space complexity of the solution is O(n). Let's see the implementation of the solution.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值