1. 程式人生 > >題目(2) 樓 building

題目(2) 樓 building

明月樓高休獨倚   相思淚

對於樓的暴搜 用了深搜  深搜時間複雜度 O(n!) 當n<20 可以考慮深搜

>20一定超時

暴力:能減就減 迫不得已加一次

關於減減減 常對應取模

正解

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,m,u,d,nw,mn=10000000;
int main() {
	freopen("building.in","r",stdin);
	freopen("building.out","w",stdout);
	int i,j;
	cin>>n>>m;
	for(i=1;i<=m;i++) {
		cin>>u>>d;
		nw=0;
		for(j=1;j<=n;j++) {
			if(nw>=d) nw-=d;
			 else nw+=u;
		}
		if(nw<mn) mn=nw;
	}
	cout<<mn<<endl;
	fclose(stdin);
	fclose(stdout);
	return 0;
}