您的位置:首页 > 其它

地域划分

2016-06-02 14:11 323 查看
理解题意,一些控制条件判断。

//现在有一块长条形的土地,这个土地我们可以看成是由n块小方格连接而成的(这些小方格我们可以将之编号为1到n)。
//而我们需要将其划分成两个部分,分别种上不同的作物(即作物A和B),划分必须在某两个小方格之间进行,或者在土地的最左端或最右端,
//若划分在第i块到第i + 1块间进行,则划分后,第1至第i块地种A,剩下的地种B。现在有一些专家对土地进行了检测,
//他们每个人评估了每块土地适合种的作物。请你找到一个合适的划分,使得其与所有专家的评估最吻合,也就是说,
//你划分到A而专家评估为B的次数和你划分到B而专家评估为A的次数之和最小。
//
//输入描述 :
//每组数据给定一个专家评估表land(其中0为评估A,1为评估B),以及小块数量n(1≤n≤300),专家评估次数m(1≤m≤300)
//
//输出描述 :
//     请返回你的划分, 即i和i + 1。若在最左端,则输出0,1;在最右端则输出n, n + 1。若有多解输出最靠左的划分。
//
// 输入例子 :
//[[1, 1, 1, 1], [0, 0, 0, 0], [1, 0, 1, 1]], 4, 3
//
//输出例子 :
//     [0, 1]

#include<iostream>
using namespace std;
#include <vector>
class Partition {
public:
vector<int> getPartition(const vector<vector<int> >& land, int n, int m) {
// write code here
vector<int> vc;
int count=0;  //每次划分评估错误的计数
int min = 0;
int index=0;
for (int k = 0; k <= n; k++)  //遍历每一次划分
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (j<k)
{
if (land[i][j]==1)
{
count++;
}
}
if (j>=k)
{
if (land[i][j]==0)
{
count++;
}
}
}
}
if (k==0)
{
min = count;
}
else if (count<min)   //如果有小的count,记录index
{
min = count;
index = k;
}
count = 0;
}
vc = { index, index + 1 };
return vc;
}
};


减少了一些复杂度,参考:

class Partition {
public:
vector<int> getPartition(const vector<vector<int> >& land, int n, int m) {
// write code here
int min = 10000000;
int idx = -1;
for(int i = -1 ; i < n ; i++){
int count = 0;
for(int j = 0 ;j < m ; j++){
int ps = i;//我的划分
int pe = i+1;
while(ps>=0) {//左边区域对比
if(land[j][ps] == 1){
count++;
}
ps--;
}
while(pe<n) {//右边区域对比
if(land[j][pe] == 0){
count++;
}
pe++;
}
}
if(count<min){//保存最小值
min = count;
idx = i;
}
}
vector<int> ret;
ret.push_back(idx+1);
ret.push_back(idx+2);
return ret;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: