第一题有点傻眼,不像想象中的那么简单,后来发现可以通过列出两个不等式限制条件,然后去满足限制条件的最大值。
#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>
using namespace std;
class TheArray {
public:
int find(int, int, int, int);
};
int TheArray::find(int n, int d, int first, int last) {
int res = max(last, first);
d = max(d, -d);
for(int i=1; i<n-1; i++){
int num1 = first+i*d;
int num2 = last+(n-1-i)*d;
res = max(res, min(num1, num2));
}
return res;
}
int main(){
TheArray array;
int n = 3;
int d = 1;
int first = 12;
int last = 13;
cout<<array.find(n, d, first, last)<<endl;
system("pause");
}
//<%:testing-code%>
//Powered by [KawigiEdit] 2.0!
第二题一看就是二分搜索,我迅速敲出代码,测试了一下样例也是对的,就提交了。
然后,然后,然后。。。。尼玛!!!原来有一个等号写错位置了!!!
没办法,只好改正之后重新提交,可惜只有150分了,原先可是450分啊。。。。
不过也是因祸得福,我猜想肯定有不少人没考虑到这点,于是搞出来了一个test case,在challenge阶段,果断cha了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>
using namespace std;
class TheOlympiadInInformatics {
public:
vector<int> v;
int len;
int find(vector <int>, int);
int check(int middle){
int res = 0;
for(int i=0; i<len; i++)
res += v[i]/(middle+1);
return res;
}
};
int TheOlympiadInInformatics::find(vector <int> sums, int k) {
len = sums.size();
v = sums;
int left = 0;
int right = -1;
for(int i=0; i<len; i++)
right = max(right, sums[i]);
int counter = 0;
while(left < right-1){
int middle = (left+right)/2;
counter = check(middle);
//it is improtant to take care
//of boundary cases!!
//300 scores!! what a pity!!!
if(counter > k)
left = middle;
else if(counter <= k)
right = middle;
}
cout<<"right: "<<check(right)<<endl;
cout<<"left: "<<check(left)<<endl;
if(check(left) <= k)
return left;
else return right;
}
int main(){
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
TheOlympiadInInformatics oi;
vector<int> sums;
int k, n;
cin>>n;
for(int i=0; i<n; i++){
int tmp;
cin>>tmp;
sums.push_back(tmp);
}
cin>>k;
cout<<oi.find(sums, k)<<endl;
return 0;
}
//<%:testing-code%>
//Powered by [KawigiEdit] 2.0!
第三题没什么思路,就放弃了。。。。主要花时间验证前两题的正确性。