CSU 1343: Long Long

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nameofcsdn/article/details/79964836

题目:

Description

    现在有两个单调递增序列,第一个序列有N个整数,第二个序列有M个整数,现在你可以从第一个序列中选一个数x,然后从第二个序列中选一个数y,那么有多少种情况满足x+y<=K呢?

Input

    输入包含多组数据。
    对于每组测试数据,第一行包含两个整数NM , K (1 <= NM <= 105, 1 <= K <= 109),含义同上。接下来一行包含N个在[1, 109]范围内的整数,依次描述了第一个序列中的各个整数。再接下来一行包含M个在[1, 109]范围内的整数,依次描述了第二个序列中的各个整数。

Output

    对于每组数据,输出有多少种情况满足x + y <= K

Sample Input

2 3 5
3 7
3 5 7
4 5 6
1 2 3 4
1 2 3 4 5
4 5 9
1 2 3 4
1 2 3 4 5

Sample Output

0
14
20

Hint

    其实如果两个序列并不是有序的,我们也可以先通过排序使其变得有序,然后就和现在的这个题目一样了,不过由于这个题目并不是为了介绍排序,所以干脆就直接给出有序的序列了。

    由于int的范围有限,当要读入的数或者要输出的数超出了int的表示范围但没有超出long long的表示范围时,我们就要使用long long了。如果OJ的服务器是Linux的,那么用“%lld”(前面是两个小写的L)来控制long long的读入和输出,而如果OJ的服务器是Windows的,则需要用“%I64d”(前面是一个大写的i)来控制long long的读入和输出,这一点一般会在OJ的“F.A.Q”有所说明的。由于我们的服务器是采用的Linux操作系统,所以使用“%lld”就OK了。

    比如这个题目,极端情况就所有情况都满足,也就是N*M中情况,当N和M都取极限值时,显然结果就需要用long long来保存并输出了。

    那么这个题目具体要怎么做呢?

    也许你一眼就看出来了,两层for循环就可以搞定嘛,不过很可惜这样估计要妥妥超时,要不试一下?

    我们不妨仔细梳理一下思路,两层for循环做法的基本思想应当是首先枚举第一个序列中拿出的数x,然后再寻找第二个序列中有多少个整数y满足x+y<=K。其实我们不难发现,每次只要找到满足x+y<=K最大的y就可以了,不妨记为max_y,那么max_y以及max_y之前的数是一定都满足x+y<=K的,这样根据max_y所在的位置,我们就能直接计算出对于当前枚举到的x有多少个y满足x+y<=K了。而找这个max_y的过程真的需要一层for循环吗?你可能已经想到了,我们完全可以用“二分”的方法去找这个max_y,这样就能快很多了。如果你对“二分”还没有任何概念也没关系,因为我们还有另外一种办法也可以达到这个目的。

    我们不妨设先后枚举到的两个相邻的x分别为x1和x2,并设x1对应的max_y为max_y1,x2对应的max_y为max_y2,由于x1<x2,我们可以得到max_y2<=max_y1。但这个有什么用呢?当然有用了,我们在找max_y2的时候就可以直接从max_y1开始递减去找了。如果再枚举到x3呢?那么一样有max_y3<=max_y2,我们直接从max_y2开始递减去找max_y3。不过也许你可能会怀疑了:这样真的能比两层for循环快吗?好像这样也是两个变量不停地在循环呀?

    至于为什么会快还是交由你自己来思考吧,如果你对刚刚说的思路还没有什么概念的话不妨结合下面我给出的示例代码来看一下。


代码:

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

int listn[100000];
int listm[100000];

int main()
{
	ios_base::sync_with_stdio(false);
	int n, m, k, temp;
	while (cin >> n >> m >> k)
	{
		for(int i = 0; i < n; i++)cin >> listn[i];
		for (int i = 0; i < m; i++)
		{
			cin >> temp;
			listm[m - 1 - i] = k - temp;
		}
		long long sum = m;
		sum *= n;
		int key = 0;
		for (int i = 0; i < n; i++)
		{
			while (listm[key] < listn[i] && key < m)key++;
			sum -= key;
		}
		cout << sum << endl;
	}
	return 0;
}

Long Long Message

12-31

DescriptionnnThe little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a big country, and he has to spend 16 shours on train to his hometown), he decided only to send SMS with his mother. nnThe little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out: nn1. All characters in messages are lowercase Latin letters, without punctuations and spaces. n2. All SMS has been appended to each other – (i+1)-th SMS comes directly after the i-th one – that is why those two messages are quite long. n3. His own SMS has been appended together, but possibly a great many redundancy characters appear leftwards and rightwards due to the broken computer. nE.g: if his SMS is “motheriloveyou”, either long message printed by that machine, would possibly be one of “hahamotheriloveyou”, “motheriloveyoureally”, “motheriloveyouornot”, “bbbmotheriloveyouaaa”, etc. n4. For these broken issues, the little cat has printed his original text twice (so there appears two very long messages). Even though the original text remains the same in two printed messages, the redundancy characters on both sides would be possibly different. nnYou are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat. nnBackground: nThe SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be. nnWhy ask you to write a program? There are four resions: n1. The little cat is so busy these days with physics lessons; n2. The little cat wants to keep what he said to his mother seceret; n3. POJ is such a great Online Judge; n4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :( nInputnnTwo strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.nOutputnnA single line with a single integer number – what is the maximum length of the original text written by the little cat.nSample InputnnyeshowmuchiloveyoumydearmotherreallyicannotbelieveitnyeaphowmuchiloveyoumydearmothernSample Outputnn27

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试