SRM617
戦績
easyは自分にしては早解き!! medium通らなかった。惜しい!悔しい!
easy
最近ラムダ式でコードを書くのにはまっている。 イージー問題。
#include <vector>
using namespace std;
class MyLongCake
{
public:
int cut(int n) {
vector<int> s;
for(int i=1; i<n; i++){
if(n%i==0){
int& div=i;
s.push_back(n/div);
}
}
int ans=0;
for(int i=1; i<n+1; i++){
auto f=[&]() {
for(auto length:s){
if(i%length==0){
return true;
}
}
return false;
};
if(f()==true)ans++;
}
return ans;
}
};
medium
オイラー路の復元問題(より簡単)。 どうやるのか知らないので、適当に一筆書きしまくってます。 2回ループを回せば、通ったなんて。。。
#include <vector>
#include <functional>
using namespace std;
class PieOrDolphin
{
public:
vector <int> Distribute(vector <int> choice1, vector <int> choice2) {
int N=choice1.size();
vector<int> ans(N, 0);
function<auto(int, int)->void > start=[&](int e, int b) {
if(b==1)for(int i=0; i<N; i++){
if(choice1[i]==e && ans[i]==0){
ans[i]=1;
return start(choice2[i], 1);
} else if(choice2[i]==e && ans[i]==0){
ans[i]=2;
return start(choice1[i], 1);
}
} else for(int i=0; i<N; i++){
if(choice1[i]==e && ans[i]==0){
ans[i]=2;
return start(choice2[i], 2);
} else if(choice2[i]==e && ans[i]==0){
ans[i]=1;
return start(choice1[i], 2);
}
}
};
for(int i=0;i<50;i++){
start(i,1);
start(i,2);
}
for(int i=0;i<50;i++){
start(i,1);
start(i,2);
}
return ans;
}
};