#include <algorithm> #include <stdlib.h> #include <iostream> #include <vector> #include <assert.h> using namespace std; typedef long long int int64; struct cycle_t { int sum; int minSum; vector< int > sums; int getIndexOf2( int startIndex, int sumValue ) const { for ( int i = startIndex; i < sums.size(); ++i ) { if ( sums[ i ] == sumValue ) { return i; } } return -1; } vector< int > tree; int pow; int startIndex; int sumValue; int getIndexOf( const int _startIndex, const int _sumValue ) { if ( tree.empty() ) { pow = 1; while ( pow < sums.size() ) { pow *= 2; } const int treeSize = pow * 2; tree.resize( treeSize ); for ( int i = 0, j = pow; i < pow; ++i, ++j ) { tree[ j ] = i < sums.size() ? sums[ i ] : 10000000; } for ( int i = pow - 1; i >= 1; --i ) { tree[ i ] = min( tree[ 2 * i ], tree[ 2 * i + 1 ] ); } } startIndex = _startIndex; sumValue = _sumValue; return find( 1, pow, pow ); } int find( const int index, const int i, const int p ) const { if ( i - 1 < startIndex ) { return -1; } if ( tree[ index ] > sumValue ) { return -1; } if ( index >= pow ) { return tree[ index ] == sumValue ? ( index - pow ) : -1; } const int result = find( index * 2, i - p / 2, p / 2 ); if ( result != -1 ) { return result; } return find( index * 2 + 1, i, p / 2 ); } }; int main() { // Read in input int n, m; cin >> n; vector< int > savings( n ); for ( int i = 0; i < n; ++i ) { cin >> savings[ i ]; } cin >> m; vector< bool > wins( m ); for ( int i = 0; i < m; ++i ) { char c; cin >> c; wins[ i ] = c == 'W'; } // Build cycles vector< int > cycleId( m, -1 ); vector< int > idInCycle( m, -1 ); vector< cycle_t > cycles( m ); int numCycles = 0; for ( int i = 0; i < m; ++i ) { if ( cycleId[ i ] != -1 ) { continue; } cycle_t& cycle = cycles[ numCycles ]; cycle.sum = 0; cycle.minSum = 10000000; cycle.sums.push_back( 0 ); int j = i; int id = 0; while ( cycleId[ j ] == -1 ) { cycle.sum += wins[ j ] ? 1 : -1; if ( cycle.sum < cycle.minSum ) { cycle.minSum = cycle.sum; } cycle.sums.push_back( cycle.sum ); cycleId[ j ] = numCycles; idInCycle[ j ] = id++; j = ( j + n ) % m; } ++numCycles; } // Solve int64 minSteps = -1; for ( int i = 0, j = 0; i < n; ++i, j = ( j + 1 ) % m ) { cycle_t& cycle = cycles[ cycleId[ j ] ]; const int64 startIndex = idInCycle[ j ]; int64 saving = savings[ i ]; const int64 startSum = cycle.sums[ startIndex ]; const int64 zeroingIndex = cycle.getIndexOf( startIndex, startSum - saving ); int64 steps = i; if ( zeroingIndex != -1 ) { steps += zeroingIndex * n - ( n - 1 ); } else { const int64 cycleLength = cycle.sums.size() - 1; steps += ( cycleLength - startIndex ) * n; saving += cycle.sums.back() - startSum; if ( cycle.sum >= 0 && saving > -cycle.minSum ) { continue; } const int64 numCycles = ( saving + cycle.minSum - cycle.sum - 1 ) / -cycle.sum; steps += numCycles * cycleLength * n; saving += numCycles * cycle.sum; const int64 zeroingIndex2 = cycle.getIndexOf( 0, -saving ); assert( zeroingIndex2 != -1 ); steps += zeroingIndex2 * n - ( n - 1 ); } if ( minSteps == -1 || steps < minSteps ) { minSteps = steps; } } cout << minSteps; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | #include <algorithm> #include <stdlib.h> #include <iostream> #include <vector> #include <assert.h> using namespace std; typedef long long int int64; struct cycle_t { int sum; int minSum; vector< int > sums; int getIndexOf2( int startIndex, int sumValue ) const { for ( int i = startIndex; i < sums.size(); ++i ) { if ( sums[ i ] == sumValue ) { return i; } } return -1; } vector< int > tree; int pow; int startIndex; int sumValue; int getIndexOf( const int _startIndex, const int _sumValue ) { if ( tree.empty() ) { pow = 1; while ( pow < sums.size() ) { pow *= 2; } const int treeSize = pow * 2; tree.resize( treeSize ); for ( int i = 0, j = pow; i < pow; ++i, ++j ) { tree[ j ] = i < sums.size() ? sums[ i ] : 10000000; } for ( int i = pow - 1; i >= 1; --i ) { tree[ i ] = min( tree[ 2 * i ], tree[ 2 * i + 1 ] ); } } startIndex = _startIndex; sumValue = _sumValue; return find( 1, pow, pow ); } int find( const int index, const int i, const int p ) const { if ( i - 1 < startIndex ) { return -1; } if ( tree[ index ] > sumValue ) { return -1; } if ( index >= pow ) { return tree[ index ] == sumValue ? ( index - pow ) : -1; } const int result = find( index * 2, i - p / 2, p / 2 ); if ( result != -1 ) { return result; } return find( index * 2 + 1, i, p / 2 ); } }; int main() { // Read in input int n, m; cin >> n; vector< int > savings( n ); for ( int i = 0; i < n; ++i ) { cin >> savings[ i ]; } cin >> m; vector< bool > wins( m ); for ( int i = 0; i < m; ++i ) { char c; cin >> c; wins[ i ] = c == 'W'; } // Build cycles vector< int > cycleId( m, -1 ); vector< int > idInCycle( m, -1 ); vector< cycle_t > cycles( m ); int numCycles = 0; for ( int i = 0; i < m; ++i ) { if ( cycleId[ i ] != -1 ) { continue; } cycle_t& cycle = cycles[ numCycles ]; cycle.sum = 0; cycle.minSum = 10000000; cycle.sums.push_back( 0 ); int j = i; int id = 0; while ( cycleId[ j ] == -1 ) { cycle.sum += wins[ j ] ? 1 : -1; if ( cycle.sum < cycle.minSum ) { cycle.minSum = cycle.sum; } cycle.sums.push_back( cycle.sum ); cycleId[ j ] = numCycles; idInCycle[ j ] = id++; j = ( j + n ) % m; } ++numCycles; } // Solve int64 minSteps = -1; for ( int i = 0, j = 0; i < n; ++i, j = ( j + 1 ) % m ) { cycle_t& cycle = cycles[ cycleId[ j ] ]; const int64 startIndex = idInCycle[ j ]; int64 saving = savings[ i ]; const int64 startSum = cycle.sums[ startIndex ]; const int64 zeroingIndex = cycle.getIndexOf( startIndex, startSum - saving ); int64 steps = i; if ( zeroingIndex != -1 ) { steps += zeroingIndex * n - ( n - 1 ); } else { const int64 cycleLength = cycle.sums.size() - 1; steps += ( cycleLength - startIndex ) * n; saving += cycle.sums.back() - startSum; if ( cycle.sum >= 0 && saving > -cycle.minSum ) { continue; } const int64 numCycles = ( saving + cycle.minSum - cycle.sum - 1 ) / -cycle.sum; steps += numCycles * cycleLength * n; saving += numCycles * cycle.sum; const int64 zeroingIndex2 = cycle.getIndexOf( 0, -saving ); assert( zeroingIndex2 != -1 ); steps += zeroingIndex2 * n - ( n - 1 ); } if ( minSteps == -1 || steps < minSteps ) { minSteps = steps; } } cout << minSteps; return 0; } |