Esempio n. 1
0
int Len(TIntVector v, bool print) {
    int result = 0;
    if (print) {
        Out(v);
        printf("\n");
    }
    for (size_t i = 0; i < v.size(); ++i) {
        size_t j = 0;
        while ( v[j] != i )
            ++j;
        if (j != i) {
            TIntVector nw;
            for (size_t k = 0; k < i; ++k)
                nw.push_back(v[k]);
            if (j + 1 != v.size()) {
                for (size_t k = j; k < v.size(); ++k)
                    nw.push_back(v[k]);
                for (size_t k = i; k < j; ++k)
                    nw.push_back(v[i + (j - k) - 1]);
                result += 2;
            } else {
                for (size_t k = i; k < v.size(); ++k)
                    nw.push_back(v[v.size() - k + i - 1]);
                ++result;
            }
            v.swap(nw);
            if (print) {
                Out(v);
                printf(" %d\n", result);
            }
        }
    }
    return result;
}
Esempio n. 2
0
	void end( const TWallVertexVector& vb, TIntVector& polygon, TIntVector& ib )
	{
		assert( polygonCount );
		if( polygonCount == 1 ) {
			
			// trivial case, just output input polygon
			polygon.resize( 0 );
			polygon.reserve( vertices.size() );
			int idx0 = *vertices.begin();
			int idx = idx0;
			do {
				polygon.push_back( idx );
				const TIntVector& vnext = vertexNexts[idx];
				assert( vnext.size() == 1 );
				idx = vnext[0];
			} while( idx != idx0 );


			triangulator::process( vb, polygon, ib );

		} else {
			
			// mark vertex types
			markVertexTypes();

			// trace and triangulate the polygon(s)
			traceBorder( vb, polygon, ib );
		}
	}
Esempio n. 3
0
void CWall3D::fracturePiecesInYRange( float t, float y1, float y2, TIntVector& pcs )
{
	if( !mPiecesInited )
		initPieces();

	// fetch the pieces
	pcs.resize( 0 );

	// TODO: optimize, right now linear search!
	float pieceRestoreTime = t + 1.0e6f;

	int n = mWall2D.getPieceCount();
	for( int i = 0; i < n; ++i ) {
		const CWallPiece2D& p = mWall2D.getPiece( i );
		SVector2 c = p.getAABB().getCenter();
		float y = c.y;
		if( mMatrix.getAxisY().y < 0.5f ) {
			SVector3 wc;
			D3DXVec3TransformCoord( &wc, &SVector3(c.x,c.y,0), &mMatrix );
			y = wc.y;
		}
		if( y >= y1 && y <= y2 ) {
			mPieceRestoreTimes[i] = pieceRestoreTime;
			if( mFracturedPieces[i] )
				continue;
			pcs.push_back( i );
			fractureOutPiece( i );
		}
	}
}
Esempio n. 4
0
int main()
{
	string strProblemName = "stamps";
	string strInFile = strProblemName + ".in";
	string strOutFile = strProblemName + ".out";
	ofstream fout ( strOutFile.c_str() );
	ifstream fin ( strInFile.c_str() );

	if( !fin )
	{
		cout << "open input file fail!" << endl;
		return 0;
	}

	int nMaxStampNum, nStampValueNum;
	fin >> nMaxStampNum >> nStampValueNum;
	for ( int i=0; i<nStampValueNum; ++i )
	{
		fin >> arStampValue[ i ];
	}

	int *arValueSumFlag = new int[MAX_VALUE_SUM+1];
	memset( arValueSumFlag, 0, sizeof( arValueSumFlag[0] ) * MAX_VALUE_SUM+1 );

	typedef std::vector<int> TIntVector;
	TIntVector tCurSumValue;
	tCurSumValue.push_back( 0 );

	for ( int i=0; i<nMaxStampNum; ++i )
	{
		TIntVector tNewSumValue;
		for ( int k=0; k<tCurSumValue.size(); ++k )
		{
			for ( int j = 0; j<nStampValueNum; ++j )
			{
				int nSum = tCurSumValue[k] + arStampValue[ j ];
				if ( !arValueSumFlag[nSum] )
				{
					tNewSumValue.push_back( nSum );
					arValueSumFlag[nSum] = true;
//					tAllSumValue.insert( nSum );
				}
			}

		}
		tCurSumValue = tNewSumValue;
	}

	// 找出连续最多的.
	int nMaxContinueNum = 0;
	int nMaxContinueTail = 0;
	int nLastNum = -1;
	int nCurContinueNum = 1;
	//	int nCurContinueHead = 0;

	for ( int i=0; i<MAX_VALUE_SUM; ++i  )
	{
		if ( !arValueSumFlag[i] )
		{
			continue;
		}
		int nCurNum = i;

		if ( nLastNum+1 == nCurNum )
		{
			// 连续.
			nCurContinueNum ++;
		}
		else
		{
			// 不连续了.	
			nCurContinueNum = 1;
		}

		nLastNum = nCurNum;

		if ( nCurContinueNum > nMaxContinueNum )
		{
			nMaxContinueNum = nCurContinueNum;
			nMaxContinueTail = nLastNum;
		}


	}
	
	delete[] arValueSumFlag;

	fout << nMaxContinueTail << endl;

	fin.close();
	fout.close();

#ifdef THINKINGL

	cout << "use clock: " << clock() << " / " << CLOCKS_PER_SEC << endl;

	cout << "-----------begin--dump--output--file----------------" << endl << endl;
	system( ( string( "type " ) + strOutFile ).c_str() );
	cout << endl;
	system( "pause" );
#endif

	return 0;
}
Esempio n. 5
0
int main()
{
#ifndef ONLINE_JUDGE
    ifstream fIn("input.txt");
    cin.rdbuf( fIn.rdbuf() );
#endif

    int n;
    int m;
    cin >> n >> m;

    TBoolVector dummy(2*n, false);
    TMatrix g(2*n, dummy);
    for (int i = 0; i < m; ++i)
    {
        int a;
        int b;
        cin >> a >> b;
        --a;
        --b;
        g[a][b] = true;
        g[b][a] = true;
    }
    for (int i = 0; i < 2 * n; ++i)
    {
        g[i][i] = true;
    }

    TIntVector s;
    s.push_back(0);
    int next = 1;
    do
    {
        int i = next;
        for (; i < 2 * n; ++i)
        {
            int j = 0;
            while (j < s.size() && !g[s[j]][i])
            {
                ++j;
            }
            if (j == s.size())
            {
                break;
            }
        }
        if (i == 2 * n)
        {
            next = s.back() + 1;
            s.pop_back();
        }
        else
        {
            next = 1;
            s.push_back(i);
        }
    } while (!s.empty() && s.size() != n);

    if (s.size() == n)
    {
        for (int i = 0; i < 2 * n; ++i)
        {
            if (find(s.begin(), s.end(), i) != s.end())
            {
                cout << i + 1 << " ";
            }
        }
        cout << endl;
        for (int i = 0; i < 2 * n; ++i)
        {
            if (find(s.begin(), s.end(), i) == s.end())
            {
                cout << i + 1 << " ";
            }
        }
        cout << endl;
    }
    else
    {
        cout << "IMPOSSIBLE\n";
    }

    return 0;
}
Esempio n. 6
0
int main()
{
    TIntVector primes;
    {
        TBoolVector erato(N + 1, true);
        for (size_t i = 2; i < N; ++i)
        {
            if (erato[i])
            {
                if (i > 1000)
                {
                    primes.push_back(static_cast<int>(i));
                }
                for (size_t j = i + i; j < erato.size(); j += i)
                {
                    erato[j] = false;
                }
            }
        }
    }

    printf("Primes: %d\n", static_cast<int>(primes.size()));

    /*
    TLLIVector powerLB1;
    FactorialFactorization(LB, primes, &powerLB1);
    TLLIVector powerLB2;
    FactorialFactorization(UB - LB, primes, &powerLB2);
    TLLIVector powerUB;
    FactorialFactorization(UB, primes, &powerUB);

    TLLIVector primePowers(primes.size());
    size_t zeroes = 0;
    for (size_t i = 0; i < primes.size(); ++i)
    {
        TLLI primePower = powerUB[i] - powerLB1[i] - powerLB2[i];
        if (0 == primePower)
        {
            ++zeroes;
        }
        primePowers[i] = primePower;
        printf("%d %d\n", static_cast<int>(primes[i]), static_cast<int>(primePower));
    }
    printf("Zeroes: %d\n", static_cast<int>(zeroes));


    for (size_t i = 0; i < primes.size(); ++i)
    {
        if (!primePowers[i])
        {
            printf("...%d\n", static_cast<int>(i));
            int modi = BinomialModPrime(UB, LB, primes[i]);
            for (size_t j = 0; j < primes.size(); ++j)
            {
                if (!primePowers[j])
                {
                    int modj = BinomialModPrime(UB, LB, primes[j]);
                    for (size_t k = 0; k < primes.size(); ++k)
                    {
                        if (!primePowers[k])
                        {
                            int modk = BinomialModPrime(UB, LB, primes[k]);
                        }
                    }
                }
            }
        }
    }
    */

    for (size_t i = 0; i < primes.size(); ++i)
    {
        printf("...%d\n", static_cast<int>(i));
        int modi = BinomialModPrime(UB, LB, primes[i]);
        for (size_t j = 0; j < primes.size(); ++j)
        {
            int modj = BinomialModPrime(UB, LB, primes[j]);
            {
                for (size_t k = 0; k < primes.size(); ++k)
                {
                    int modk = BinomialModPrime(UB, LB, primes[k]);
                    printf("%d %d %d\n", modi, modj, modk);
                }
            }
        }
    }

    return 0;
}
Esempio n. 7
0
void CWall3D::fracturePiecesInSphere( float t, const SVector3& pos, float radius, TIntVector& pcs,
		float restoreAfter, float restoreDuration, bool noRestore )
{
	if( !mPiecesInited )
		initPieces();

	pcs.resize( 0 );
	
	// to local space
	SVector3 locPos;
	D3DXVec3TransformCoord( &locPos, &pos, &mInvMatrix );

	if( locPos.z < -radius || locPos.z > radius )
		return;

	// remember restore times
	if( mResTimeGrid && !noRestore ) {
		float rad = radius*2.0f;
		float lx1 = (locPos.x - rad) / mWall2D.getSize().x * RESGRID_X;
		float lx2 = (locPos.x + rad) / mWall2D.getSize().x * RESGRID_X;
		float ly1 = (locPos.y - rad) / mWall2D.getSize().y * RESGRID_Y;
		float ly2 = (locPos.y + rad) / mWall2D.getSize().y * RESGRID_Y;
		int ix1 = (int)clamp( lx1, 0, RESGRID_X-1 );
		int ix2 = (int)clamp( lx2, 0, RESGRID_X-1 );
		int iy1 = (int)clamp( ly1, 0, RESGRID_Y-1 );
		int iy2 = (int)clamp( ly2, 0, RESGRID_Y-1 );
		float dx = mWall2D.getSize().x / RESGRID_X;
		float dy = mWall2D.getSize().y / RESGRID_Y;
		for( int iy = iy1; iy <= iy2; ++iy ) {
			float* resval = mResTimeGrid + RESGRID_X*iy + ix1;
			float fy = iy * dy;
			for( int ix = ix1; ix <= ix2; ++ix, ++resval ) {
				float fx = ix * dx;

				// don't touch restore grid outside the circle
				float diffX = fx-locPos.x;
				float diffY = fy-locPos.y;
				float diffR2 = diffX*diffX + diffY*diffY;
				if( diffR2 > rad*rad )
					continue;

				// restore time for this grid point - start at
				// t+restoreAfter at circle boundaries, later at circle
				// center
				float resTime = t + restoreAfter + (1.0f-diffR2/(rad*rad)) * restoreDuration;

				if( *resval < 0.0f )
					*resval = resTime;
				else
					*resval = max( (*resval), resTime );
			}
		}
	}

	// fetch the pieces

	float pieceRestoreTime;
	if( noRestore ) {
		pieceRestoreTime = t + 1.0e9f;
	} else {
		pieceRestoreTime = t + restoreAfter + restoreDuration;
	}

	// TODO: optimize, right now linear search!
	int n = mWall2D.getPieceCount();
	for( int i = 0; i < n; ++i ) {
		const CWallPiece2D& p = mWall2D.getPiece( i );
		SVector2 c = p.getAABB().getCenter();
		SVector3 tocenter = locPos - SVector3(c.x,c.y,0);
		if( tocenter.lengthSq() < radius*radius ) {
			mPieceRestoreTimes[i] = pieceRestoreTime;
			if( mFracturedPieces[i] )
				continue;
			pcs.push_back( i );
			fractureOutPiece( i );
		}
	}
}
Esempio n. 8
0
	void	traceBorder( const TWallVertexVector& vb, TIntVector& polygon, TIntVector& ib )
	{
		static int traceID = 0;
		++traceID;

		int idx0 = getBorderIndex();
		assert( idx0 >= 0 && idx0 < vertexTypes.size() );
		
		ib.resize( 0 );
		polygon.resize( 0 );
		polygon.reserve( vertices.size()/2 );

		TIntVector localPolygon;
		localPolygon.reserve( 128 );
		TIntVector localIB;
		localIB.reserve( 128 );

		int idxPrev = idx0;
		int idx = idx0;
		int debugCounter = 0;

		int debugLoopCounter = 0;

		do {

			localIB.resize( 0 );

			bool willFormLoop;
			do{

				localPolygon.push_back( idx );
				borderVertices.erase( idx );
				vertexTraceID[idx] = traceID;
				assert( ++debugCounter <= vertices.size()*2 );

				// Next vertex is the neighbor of current, that is not interior
				// and that is not the previous one.
				// When there are many possible ones, trace based on angle.

				int idxNext = -1;

				SVector2 prevToCurr = vb[idx] - vb[idxPrev];
				if( prevToCurr.lengthSq() < 1.0e-6f )
					prevToCurr.set( -0.01f, -0.01f );

				TIntIntsMap::const_iterator it;
				it = vertexNexts.find( idx );
				assert( it != vertexNexts.end() );
				const TIntVector& vnext = it->second;
				int n = vnext.size();
				float bestAngle = 100.0f;
				for( int i = 0; i < n; ++i ) {
					int idx1 = vnext[i];
					if( idx1 != idxPrev && vertexTypes[idx1] != VTYPE_INTERIOR ) {
					//if( idx1 != idxPrev ) {
						SVector2 currToNext = vb[idx1] - vb[idx];
						float ang = signedAngle2D( prevToCurr, currToNext );
						if( ang < bestAngle ) {
							bestAngle = ang;
							idxNext = idx1;
						}
					}
				}
				assert( bestAngle > -4.0f && bestAngle < 4.0f );
				assert( idxNext >= 0 );

				willFormLoop = (vertexTraceID[idxNext] == traceID);

				// Optimization: if best angle is zero, then we're walking
				// in a straight line. Optimize out the current vertex.
				if( bestAngle == 0.0f && idx != idx0 && !willFormLoop ) {
					localPolygon.pop_back();
				}

				idxPrev = idx;
				idx = idxNext;

			} while( !willFormLoop );

			assert( localPolygon.size() >= 3 );
			//if( localPolygon.size() < 3 ) {
			//	return;
			//}

			assert( ++debugLoopCounter < vertices.size() );

			if( idx == idx0 ) {
				// The polygon is simple or we found the last loop.
				// Triangulate local and append to results.
				triangulator::process( vb, localPolygon, localIB );
				polygon.insert( polygon.end(), localPolygon.begin(), localPolygon.end() );
				ib.insert( ib.end(), localIB.begin(), localIB.end() );

				// We can have separated other loops. Try fetching them as well.
				idx0 = getBorderIndex();
				if( idx0 == -1 ) {
					return;
				} else {
					localPolygon.resize( 0 );
					idxPrev = idx0;
					idx = idx0;
				}
				
			} else {

				// The polygon must be complex, and we just found a closed loop.
				// Take only the loop, triangulate it, append to results, continue.
				TIntVector::const_iterator itLoopStart = 
					std::find( localPolygon.begin(), localPolygon.end(), idx );
				assert( itLoopStart != localPolygon.end() );

				// append to results
				TIntVector loopPolygon( itLoopStart, localPolygon.end() );
				triangulator::process( vb, loopPolygon, localIB );
				polygon.insert( polygon.end(), loopPolygon.begin(), loopPolygon.end() );
				ib.insert( ib.end(), localIB.begin(), localIB.end() );

				// continue - remove the looped polygon from local
				localPolygon.resize( itLoopStart - localPolygon.begin() );

			}

		} while( true );
	}