程序竞赛SDAU 第十周总结

这周作业是训练二分的算法和应用,题目还没完成,但自己能感觉出来应用题的类钟还是跟清晰地。二分的算法就是一个while循环left<=right基本都这样

while(high - low > 1.0e-6)
{
	 mid = (high + low)/2;
    if(Caculate(mid)<x)
        low=mid;
    else
        high=mid;
}

应用题目里的主要是在while循环里的改变,下面贴几个应用题。
例一:
给定一个数列,要求把数列切分成m个段,使得总和最大的一段的总和最小。求这个总和。
(这个题课件作业里面都有这个,感觉这个例题很典型了。)
思路:首先确定left和right left就是max(a[i]),而right是sum(a[i]);
因为题目是要找分成m段后,里面段的和的最大值最小。
那你肯定要在里面去for循环了。因为设mid是符合的,设cnt=0;所以当某一段的和>mid的时候就cnt++,然后分完之后
如果cnt>m说明段分得多了,left=mid+1;
else right=mid-1;

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
using namespace std;
int a[1000041];
int main()
{
	int m, n;
	cin >> n >> m;
	int maxx = 0, sum = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		maxx = a[i] > maxx ? a[i] : maxx;
		sum += a[i];
	}
	int left = maxx, right = sum;
	int mid, s;
	while (left <= right)
	{
		sum = 0; s = 0;
		mid = (left + right) / 2;
		for (int i = 1; i <= n; i++)
		{
			sum += a[i];
			if (sum > mid)
			{
				s++;
				sum = 0; i--;
				continue;
			}
		}
		s++;
		if (s > m)
			left = mid + 1;
		else
			right = mid - 1;
		
	}
	cout << left << endl;


}

上边的这个题目跟那个去石头的那个题感觉思路都差不多。
例二
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .
Input
The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2 28 ) that belong respectively to A, B, C and D .
Output
For each input file, your program has to write the number quadruplets whose sum is zero.
Sample Input
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45
Sample Output
5
Hint
Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).
这个题目是做作业的时候遇见的,感觉还是有必要写一下的。
题意:就是给四个数组abcd问你有多少a+b+c+d=0;
思路:当时没反应出来哪里用到二分了?
后来看了下同学的代码,感觉思路挺奇特的。
这个思路就是a+b+c+d=0;等价于满足 a+b=-(c+d);然后我再去开辟两个新数组来记录上边两种。那这个题目不就变成找相同的数嘛
问有多少中
用到了下面这个小知识

最好是从0开始的数组这样不出错否则就算你里边没有和x相等的,他照样等于1;
【一个有序数组num【6】,你想判断x=7在这个数组里出现了几次

int pos1=lower_bound(num,num+6,7)-num;
返回数组中第一个大于或等于被查数的值 下标
int pos2=upper_bound(num,num+6,7)-num;
返回数组中第一个大于被查数的值下标
所以 次数=pos2-pos1;】
这个数组的范围啊,wr了 好几次才改成这个样的。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
using namespace std;
long long  a[5000], b[5000], c[5000], d[5000],x[4005*4005],y[4005 * 4005];
int main()
{
	int n; cin >> n;
	for (int i =0; i < n; i++)
	{
		cin >> a[i] >> b[i] >> c[i] >> d[i];
	}
	int n1 = 0;
	for(int i=0;i<n;i++)
		for (int j =0; j <n; j++)
		{
		
			x[n1]=a[i]+b[j];
			y[n1] = -(c[i]+d[j]);
			n1++;
		}
	int s = 0;
	sort(x , x+n1);
	sort(y , y + n1);
	for (int i =0; i < n1; i++)
	{
		s = s + upper_bound(y, y + n1, x[i]) - lower_bound(y , y + n1, x[i]);
	}
	cout << s << endl;
	


}

例三:
Alice is providing print service, while the pricing doesn’t seem to be reasonable, so people using her print service found some tricks to save money.
For example, the price when printing less than 100 pages is 20 cents per page, but when printing not less than 100 pages, you just need to pay only 10 cents per page. It’s easy to figure out that if you want to print 99 pages, the best choice is to print an extra blank page so that the money you need to pay is 100 × 10 cents instead of 99 × 20 cents.
Now given the description of pricing strategy and some queries, your task is to figure out the best ways to complete those queries in order to save money.
Input
The first line contains an integer T (≈ 10) which is the number of test cases. Then T cases follow.
Each case contains 3 lines. The first line contains two integers n, m (0 < n, m ≤ 10 5 ). The second line contains 2n integers s 1, p 1 , s 2, p 2 , …, s n, p n (0=s 1 < s 2 < … < s n ≤ 10 9 , 10 9 ≥ p 1 ≥ p 2 ≥ … ≥ p n ≥ 0)… The price when printing no less than s i but less than s i+1 pages is p i cents per page (for i=1…n-1). The price when printing no less than s n pages is p n cents per page. The third line containing m integers q 1 … q m (0 ≤ q i ≤ 10 9 ) are the queries.
Output
For each query q i, you should output the minimum amount of money (in cents) to pay if you want to print q i pages, one output in one line.
Sample Input
1
2 3
0 20 100 10
0 99 100
Sample Output
0
1000
1000
题意:你去复印东西,然后它根据页数制定价格。
先输入t 样例数。输入n 是几种价格 m是你输入的复印件数
思路:这个题就是你复印的越多的话,平均下来单页的价格就越便宜,所以你在考虑的时候要注意,你可以多打印,这样可能付的钱少。
这个题我TL 了十次,每次都有一些收获。
首先 我刚开始的思路是 先用二分来求出第一个大于等于page的 a[i]的角标i,然后我不放心 就让zb=i-1;从i-1开始 算。
写了个for循环 如下图

for (int i = q - 1; i< n; i++)
					{
						if (a[i] <= page)
						{
							money = page * b[i];
						}
						if (a[i] > page)
						{
							money = a[i] * b[i];
						}
						if (money < minn)
						{
							minn = money;
						}
						else
							break;
					}

结果超时。然后我又想到了用upper_bound来找角标(第一个大于page的坐标),然后这样就省去了找角标的时间。但是还是超时。后来问同学,其实我不应该光去试,因为这样会浪费好多的时间,你还不如直接把a[i]*b[i]先存起来,s[n-1]=a[n-1]*b[n-1]然后 从i=n-2;s[i]=min{s[i+1],a[i]b[i]};
然后最后你找出角标后,如果角标zb!=n-1
minmoney=min(page
b[zb-1],s[zb]);然后还超时 然后我又把所有的cin cout都改成c语言,竟然就对了。。。。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f3f;
typedef long long ll;
ll a[100009], b[100099],s[100091];
int main()
{
	int t;// cin >> t;
	scanf("%d", &t);
	while (t--)
	{
		int n, m; //cin >> n >> m;
		scanf("%d%d", &n,&m);
		for (int i =0; i<n;i++)
		{
			//cin >> a[i] >> b[i];
			scanf("%d%d", &a[i], &b[i]);
		}
		s[n - 1] = a[n - 1] * b[n - 1]; 
		for (int i = n - 2; i >= 0; i--)
			s[i] = min(s[i + 1], a[i] * b[i]);
		while (m--)
		{
			ll page; scanf("%d", &page);
			//cin >> page;
			int zb = upper_bound(a, a+n, page)-a ;
			if (zb==n)
			{
				//cout << page * b[n - 1] << endl;
				printf("%lld\n", page * b[n - 1]);
			}
			else
			{
			//	cout << min(page * b[zb-1], s[zb]) << endl;
				printf("%lld\n", min(page * b[zb - 1], s[zb]));
			}
		}
	}


}

反思与总结:
这部分的题感觉就是你先想想left和right的最值,然后就是利用二分法来枚举,无限接近答案。那个while的终止条件像:left<=right Or right-left<=0.00001(这个一般是有精度要求的时候才这样的);类似于这两种条件。然后一般就相当于你先把答案固定在一个区间内,然后多次二分来试,把规模为n的问题分成若干个规模较小的问题,枚举出来答案,还要注意的是二分算法应用的条件是有序性。
二分算法里面比较重要的是cout的东西,一般是left ,mid,right,具体我也没怎么搞明白,每次都是错了之后再去试其他的。
二分的应用
题做的不够快,而且题目总结的不够多,还有好几个没弄完的,等我做完这些题我会把总结继续写上。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值