240 lines
4.8 KiB
C++
240 lines
4.8 KiB
C++
/* Check cf5-opt.vim defs.
|
|
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
|
|
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
|
|
VIM: let g:argv=""
|
|
*/
|
|
#include <iostream>
|
|
#include <exception>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include <string>
|
|
|
|
int testinput[] = {
|
|
1,0,0,0,1,0,0,0,0
|
|
};
|
|
|
|
// find bug.
|
|
int solution3(std::vector<int>& A) {
|
|
int n = A.size();
|
|
int i = n - 1;
|
|
int result = -1;
|
|
int k = 0;
|
|
int maximal = 0;
|
|
while (i > 0)
|
|
{
|
|
if (A[i] == 1)
|
|
{
|
|
k = k + 1;
|
|
if (k >= maximal)
|
|
{
|
|
maximal = k;
|
|
result = i;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
k = 0;
|
|
}
|
|
i = i - 1;
|
|
}
|
|
if (A[i] == 1 && k + 1 > maximal) // <- bug is here. there should be k+1 >= maximal
|
|
result = 0;
|
|
return result;
|
|
}
|
|
|
|
const char * a1 = "abc";
|
|
const char * a2 = "Abc";
|
|
|
|
int solution4(const std::string & s1, const std::string & s2)
|
|
{
|
|
std::string::const_iterator s1it = s1.begin();
|
|
std::string::const_iterator s1et = s1.end();
|
|
std::string::const_iterator s2it = s2.begin();
|
|
std::string::const_iterator s2et = s2.end();
|
|
for ( ; s1it!=s1et && s2it!=s2et; ++s1it, ++s2it )
|
|
{
|
|
char c1 = toupper(*s1it);
|
|
char c2 = toupper(*s2it);
|
|
if ( c1 < c2 )
|
|
return -1;
|
|
else if ( c2 < c1 )
|
|
return 1;
|
|
}
|
|
|
|
if ( s1it==s1et && s2it==s2et )
|
|
return 0;
|
|
else if ( s1it==s1et )
|
|
return -1;
|
|
else
|
|
return 1;
|
|
}
|
|
|
|
int solution2(int blx1, int bly1, int trx1, int try1, int blx2, int bly2, int trx2, int try2)
|
|
{
|
|
if ( blx1 > trx1 )
|
|
std::swap( blx1, trx1 );
|
|
if ( bly1 > try1 )
|
|
std::swap( bly1, try1 );
|
|
if ( blx2 > trx2 )
|
|
std::swap( blx2, trx2 );
|
|
if ( bly2 > try2 )
|
|
std::swap( bly2, try2 );
|
|
|
|
if ( trx1 < blx2
|
|
|| trx2 < blx1
|
|
|| try1 < bly2
|
|
|| try2 < bly1 )
|
|
return 0;
|
|
|
|
long long x[4] = { blx1,trx1,blx2,trx2 };
|
|
std::sort( &x[0], &x[4]);
|
|
|
|
long long y[4] = { bly1,try1,bly2,try2 };
|
|
std::sort( &y[0], &y[4]);
|
|
|
|
unsigned long long a = (unsigned long long)(x[2]-x[1])*(unsigned long long)(y[2]-y[1]);
|
|
return ( a <= 2147483647 ) ? (int)a : -1;
|
|
}
|
|
|
|
int solution1( std::vector<int> & a )
|
|
{
|
|
typedef std::vector<int> v_t;
|
|
|
|
std::sort(a.begin(),a.end());
|
|
|
|
long long s = 0;
|
|
|
|
v_t::const_iterator it=a.begin();
|
|
v_t::const_iterator et=a.end();
|
|
v_t::const_iterator jt;
|
|
for ( jt = it; it != et && *it == *jt; ++it );
|
|
long long ic = it - jt;
|
|
s += ic*(ic-1)/2;
|
|
|
|
for ( ; it != et; )
|
|
{
|
|
for ( jt = it; it != et && *it == *jt; ++it );
|
|
long long pc = ic;
|
|
ic = it - jt;
|
|
s += ic*(ic-1)/2;
|
|
s += ic*pc;
|
|
}
|
|
|
|
const long long lim = 100000000;
|
|
return ( s <= lim ) ? s : -1;
|
|
}
|
|
|
|
// you can also use includes, for example:
|
|
int solution_demo1( std::vector<int> & a )
|
|
{
|
|
if ( !a.size() )
|
|
return -1;
|
|
|
|
typedef std::vector<int>::const_iterator iterator;
|
|
typedef std::vector<long long> ub_t;
|
|
|
|
int s = 0;
|
|
ub_t ub;
|
|
for ( iterator it=a.begin(), ib=a.begin(), ie=a.end();
|
|
it!=ie; ++it )
|
|
{
|
|
int c = it-ib;
|
|
long long l = c-*it;
|
|
long long r = c+*it;
|
|
|
|
ub_t::iterator jt = std::lower_bound( ub.begin(), ub.end(), l);
|
|
s += ub.end()-jt;
|
|
if ( s > 10000000 )
|
|
return -1;
|
|
|
|
jt = std::upper_bound( ub.begin(), ub.end(), r);
|
|
ub.insert(jt, r);
|
|
}
|
|
return s;
|
|
}
|
|
|
|
// you can also use includes, for example:
|
|
int solution_demo2( const std::vector<int> & a )
|
|
{
|
|
typedef std::vector<int>::const_iterator iterator;
|
|
//
|
|
// There is no solution if array 'a' has no elements. However,
|
|
// per task condition 'a' is non-empty. So, I don't consider that
|
|
// case.
|
|
//
|
|
std::vector<bool> m( a.size(), false );
|
|
//
|
|
// This is the index.
|
|
//
|
|
int cvrp = 0;
|
|
//
|
|
// Iterate over array and find the index that inserts
|
|
// element into m. That will be wanted index.
|
|
//
|
|
for ( iterator it=a.begin(), ib=a.begin(), ie=a.end();
|
|
it!=ie; ++it )
|
|
{
|
|
if ( !m[*it] )
|
|
{
|
|
m[*it]=true;
|
|
cvrp = it - ib;
|
|
}
|
|
}
|
|
//
|
|
// Make it index.
|
|
//
|
|
return cvrp;
|
|
}
|
|
|
|
int main ( void )
|
|
{try{
|
|
|
|
|
|
// std::cout << solution( 0,0,2,2, 1,1,3,3 ) << std::endl;
|
|
// std::cout << solution( -2147483648,-2147483648,2147483647,2147483647, -2147483640,-2147483640,2147483640,2147483640 ) << std::endl;
|
|
|
|
|
|
// std::cout << solution( std::string(a1), std::string(a2) ) << std::endl;
|
|
|
|
#if 1
|
|
int cnt = sizeof(testinput)/sizeof(testinput[0]);
|
|
std::vector<int> a;
|
|
//std::cout << solution( a ) << std::endl;
|
|
|
|
a.reserve(cnt);
|
|
for ( int i = 0; i < cnt; ++i )
|
|
a.push_back(testinput[i]);
|
|
std::cout << solution3( a ) << std::endl;
|
|
|
|
/*
|
|
{
|
|
std::vector<int> b(100000,1);
|
|
std::cout << solution( b ) << std::endl;
|
|
}
|
|
{
|
|
std::vector<int> b(10000,1);
|
|
std::cout << solution( b ) << std::endl;
|
|
}
|
|
{
|
|
std::vector<int> b(1000,1);
|
|
std::cout << solution( b ) << std::endl;
|
|
}
|
|
*/
|
|
#endif
|
|
|
|
return 0;
|
|
}
|
|
catch ( const std::exception& e )
|
|
{
|
|
std::cerr << std::endl
|
|
<< "std::exception(\"" << e.what() << "\")." << std::endl;
|
|
return 2;
|
|
}
|
|
catch ( ... )
|
|
{
|
|
std::cerr << std::endl
|
|
<< "unknown exception." << std::endl;
|
|
return 1;
|
|
}}
|
|
|