SRM 494 DIV.1 总结

250p:暴力。。。直接O(n^5)。。n<=50。

刚开始在想O(n^3)算法。。。搞了半天没搞出,最后还是用最暴力的方法写的,下次应该记得直接超暴力!


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>

using namespace std;

class Painting {
public:
	int largestBrush(vector <string>);
};

int min (int a, int b)  {
	return a > b ? b : a;
}
int max (int a, int b)  {
	return a > b ? a : b;
}

bool vis[55][55];
int Painting::largestBrush(vector <string> p) {
	int i, j, k, l, o;
	int n = p.size();
	int m = p[0].size();
	int tot = min(n, m);
	for(i = tot;i >= 1 ;i--) {
		memset(vis, 0, sizeof(vis));
		for(j = 0;j < n; j ++) {
			for(k = 0;k < m; k++) {	
				if(j+i <= n && k+i <= m) {
					int ok = 1;
					for(l = j;l < j+i;l++) {
						for(o = k;o < k+i;o ++) {
							if(p[l][o] == 'W')
								ok = 0;
						}
					}
					if(ok) {
						for(l = j;l < j+i;l++) {
							for(o = k;o < k+i;o ++) {
								vis[l][o] = 1;
							}
						}
					}
				}
			}
		}
		int ok = 1;
		for(j = 0;j < n; j++) {
			for(k = 0;k < m; k++) {
				if(p[j][k] == 'B' && !vis[j][k])
					ok = 0;
			}
		}
		if(ok)
			return i;
	}
}


// Powered by FileEdit
// Powered by TZTester 1.01 [25-Feb-2003]
// Powered by CodeProcessor


题意:定义相邻两数严格单增单减交替的数列为交替数列,定义美值为交替数列中所有相邻两数差值绝对值的总和。

现在有最多50个数,每个数的值是是个随机整数,在区间low[i], high[i],其中1<=low<=high<=1e5。如果这个数列的值组成的序列不是交替序列,则需要任意删掉一些使得其变成交替序列,求最后的序列的美值的期望是多少。


其实对于非交替数列,可以画图知道,无论怎么删除,总的美值大小还是等于所有相邻两数的差值绝对值总和,知道了这个,题目就顿时变得非常简单了。。。可以枚举前一个数的范围域,利用等差数列求和可得。


#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <sstream>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <bitset>
#include <algorithm>
using namespace std;
#define clr(a, x) memset(a, x, sizeof(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
template <class T> void checkmax(T &t, T x) { if (x > t) t = x; }
template <class T> void checkmin(T &t, T x) { if (x < t) t = x; }
template <class T> void _checkmax(T &t, T x) { if (t == -1 || x > t) t = x; }
template <class T> void _checkmin(T &t, T x) { if (t == -1 || x < t) t = x; }
typedef long long ll;
class AlternatingLane { 
    public: 
    double expectedBeauty(vector <int> low, vector <int> high);
    
// BEGIN CUT HERE
	public:
	void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
	private:
	template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
	void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
	void test_case_0() { int Arr0[] = {1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {100}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = 0.0; verify_case(0, Arg2, expectedBeauty(Arg0, Arg1)); }
	void test_case_1() { int Arr0[] = {1, 1, 1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2, 2, 2}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = 1.0; verify_case(1, Arg2, expectedBeauty(Arg0, Arg1)); }
	void test_case_2() { int Arr0[] = {1, 3, 5, 7, 9}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2, 4, 6, 8, 10}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = 8.0; verify_case(2, Arg2, expectedBeauty(Arg0, Arg1)); }
	void test_case_3() { int Arr0[] = {4, 3, 3, 7}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {10, 7, 7, 7}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = 6.171428571428572; verify_case(3, Arg2, expectedBeauty(Arg0, Arg1)); }

// END CUT HERE
 
}; 

double cal(int n) {
	return (double)n*(n+1)/2;
}
double AlternatingLane::expectedBeauty(vector <int> l, vector <int> h) {
	int n = l.size();
	double ans = 0;
	int i, j, k;
	for(i = 0;i < n-1; i++) {
		double cur = 0;
		for(j = l[i];j <= h[i]; j++) {
			if(j < l[i+1])
				cur += cal(h[i+1]-j) - cal(l[i+1]-j-1);
			else if(j > h[i+1])
				cur += cal(j-l[i+1]) - cal(j-h[i+1]-1);
			else
				cur += cal(h[i+1]-j) + cal(j-l[i+1]);
		}
		cur /= (h[i]-l[i]+1);
		cur /= (h[i+1]-l[i+1]+1);
		ans += cur;
	}
	cout<<ans<<endl;
	return ans;
}

    // BEGIN CUT HERE 
int main() {
    AlternatingLane ___test; 
    ___test.run_test(-1); 
} 
    // END CUT HERE 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
org.csource.common.MyException: getStoreStorage fail, errno code: 2 at org.csource.fastdfs.StorageClient.newReadableStorageConnection(StorageClient.java:1767) at org.csource.fastdfs.StorageClient.download_file(StorageClient.java:1219) at org.csource.fastdfs.StorageClient.download_file(StorageClient.java:1206) at com.wzdigit.framework.utils.FastDFSUtil.downFile(FastDFSUtil.java:209) at com.wzdigit.srm.dsr.utils.FileUtil.getSingleFile(FileUtil.java:51) at com.wzdigit.srm.dsr.service.bidding.BiddingorderService.getVendorQuotation(BiddingorderService.java:796) at com.wzdigit.srm.dsr.service.bidding.BiddingorderService.sendEmail(BiddingorderService.java:746) at com.wzdigit.srm.dsr.service.bidding.BiddingorderService$$FastClassBySpringCGLIB$$ebfcbd5a.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at com.alibaba.druid.support.spring.stat.DruidStatInterceptor.invoke(DruidStatInterceptor.java:73) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) at com.wzdigit.srm.dsr.service.bidding.BiddingorderService$$EnhancerBySpringCGLIB$$80ace30.sendEmail(<generated>) at com.wzdigit.srm.dsr.service.bidding.BiddingorderService$$FastClassBySpringCGLIB$$ebfcbd5a.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
最新发布
06-13

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值