7.2
Posted -ifrush
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了7.2相关的知识,希望对你有一定的参考价值。
//单调队列 #include<deque> using namespace std; deque<int> int main( ){ int a[1000]; for( itn i=0 ;i<1000 ;i++){ if(q.empty()){ q.push_back( a[i] ); } else if( q.back()>a[i]){ while( !q.empty() && q.back()>a[i]){ q.pop_back(); } q.push_back( a[i] ); } else q.push_back( a[i] ); } return 0; }
#include <bits/stdc++.h> using namespace std; deque<int> dq; const int inf =0x3f3f3f3f; int pic[550][550] ,m ,n ,k ,q; int mx[550][550] , r, c; vector <int> mn; void init(){ for(int j=1 ;j<=m; j++){ dq.clear(); for( int i=1 ;i<=k ;i++){ //cout<<"now "<<pic[i][j]<<endl; if( dq.empty() )dq.push_back( pic[i][j] ); else{ while( !dq.empty() && dq.back()<pic[i][j] ){ //cout<<"pop "<<dq.back()<<endl; dq.pop_back(); } dq.push_back( pic[i][j] ); } } for( int i=1+k ;i<=n ;i++){ mx[i-k][j] = dq.front(); //cout<<i-k<<" "<<j<<" "<<dq.front()<<endl; if( pic[i-k][j] == dq.front() )dq.pop_front(); if( dq.empty() )dq.push_back( pic[i][j] ); else{ while( !dq.empty() && dq.back()<pic[i][j] ){ //cout<<"pop "<<dq.back()<<endl; dq.pop_back(); } dq.push_back( pic[i][j] ); } } mx[n+1-k][j] = dq.front(); //cout<<n+1-k<<" "<<j<<" "<<dq.front()<<endl; //cout<<"here "; } return; } int main( ){ ios::sync_with_stdio( false ); memset( pic ,inf ,sizeof( pic )); cin>>n>>m>>k>>q; while( q--){ cin>>r>>c; cin>>pic[r][c]; } init(); //cout<<"here"<<endl; for( int i=1 ;i<=n-k+1 ;i++){ dq.clear(); for( int j=1 ;j<=k ;j++ ){ //cout<<"now "<<mx[i][j]<<endl; if( dq.empty() )dq.push_back( mx[i][j] ); else{ while( !dq.empty() && dq.back()<mx[i][j] ){ dq.pop_back(); //cout<<"pop "<<dq.back()<<endl; } dq.push_back( mx[i][j] ); } } for( int j=1+k ;j<=m ;j++ ){ mn.push_back( dq.front() ); //cout<<i<<" "<<j-k<<" "<<dq.front()<<endl; if( mx[i][j-k]==dq.front() )dq.pop_front(); if( dq.empty() )dq.push_back( mx[i][j] ); else{ while( !dq.empty() && dq.back()<mx[i][j] ){ dq.pop_back(); } dq.push_back( mx[i][j] ); } } mn.push_back( dq.front() ); //cout<<i<<" "<<m+1-k<<" "<<dq.front()<<endl; } sort(mn.begin() ,mn.end()); if(mn[0]==inf)cout<<"-1 "; else cout<<mn[0]; return 0; }
//技巧暴力 #include <bits/stdc++.h> #define N 100005 using namespace std; typedef long long ll; ll n ,k ,ans=0; ll a[N] ,sum[N]={0}; vector<ll> pk; map<ll ,int> mp; int main(){ ios :: sync_with_stdio( false ); cin >>n>>k; for( int i=1 ;i<=n ;i++){ cin>>a[i]; sum[i] = sum[i-1]+a[i]; } pk.push_back(1); ll t = k; while( abs(k) < 1e14+5 && k!=1 ){ pk.push_back(k); k *= t; //cout<<k<<endl; } sort( pk.begin(), pk.end()); int sz = pk.size(); mp[0] = 1; for( int i=1 ;i<=n ;i++){ ll tmp; for( int j=0; j<sz ;j++){ tmp = sum[j] - sum[i]; //cout<< tmp%t<<endl; ans +=mp[sum[i] - pk[j]]; } mp[sum[i]]++; } cout<<ans; return 0; }
//思维 快速幂 #include<bits/stdc++.h> #define MOD 1000000000+7 using namespace std; typedef long long ll; ll pow(ll n ,ll p){ ll ans = 1; while( p ){ if( p&1 )ans*=n; ans %= MOD; n *= n; n %= MOD; p >>= 1; } return ans; } int main( ){ ios :: sync_with_stdio( false ); ll n; ll a=0 ,g=0 ,c=0 ,t=0 ,mx ,k; string s; cin >>n >>s; for( int i=0 ;i<n ;i++){ if( s[i]==‘A‘ )a++; else if( s[i]==‘G‘ )g++; else if( s[i]==‘C‘ )c++; else t++; } mx = max(a ,max( g ,max( c ,t ) ) ); k = (a==mx)+(g==mx)+(c==mx)+(t==mx); cout<<pow(k ,n); }
// C - Elections //三分 //意想不到的错误!a!wsl! #include<bits/stdc++.h> using namespace std; typedef long long ll; int n ,m; struct Voter{ int a,b; bool operator < (const Voter & s)const{ return b < s.b; } } voter[100050]; int can_vote[100050]={0}; int tem_vote[100050]; int vis[100050]; // 检查对象:最优票数,使得花费最少 // 1,其他候选人不得高于此票数 // 2,自身不得低于此票数 // 3,满足规则1,2的基础上, // 不难发现,最终结果最优时自身票数一定恰达到此标准 int check( int level){ //cout<<"level "<<level<<endl; int sum = 0; memset( vis ,0 ,sizeof(vis));
tem_vote[0] = 0; for( int i=0 ;i<n ;i++) tem_vote[ voter[i].a ] = can_vote[ voter[i].a ]; //难以debug的错误语句,原:tem_vote[i] = can_vote[i]; for( int i=0 ;i<n ;i++){ int can = voter[i].a, mny = voter[i].b; if( can == 0)vis[i] = 1; else{ // cout<<tem_vote[can]<<endl; if( tem_vote[can] >= level){ sum += mny; tem_vote[ can ]--; tem_vote[ 0 ]++; vis[i] = 1; //cout<<"a i "<<i<<" "<<can<<" "<<mny<<endl; //cout<<tem_vote[can]<<endl; } } } // cout<<tem_vote[0]<<endl; if( tem_vote[0] < level) for( int i=0 ;i<n ;i++){ if( vis[i] )continue; int can = voter[i].a, mny = voter[i].b; tem_vote[0]++; sum += mny; //cout<<"i "<<i<<" "<<can<<" "<<mny<<endl; if( tem_vote[0] >= level) break; } //cout<<"sum "<<sum<<endl; return sum; } int main(void){ ios::sync_with_stdio( false ); memset( can_vote ,0 ,sizeof(can_vote)); cin >> n; for( int i=0 ;i<n ;i++){ cin >>voter[i].a >>voter[i].b; //cout<< voter[i].a<<‘ ‘<<voter[i].b<<endl; can_vote[ voter[i].a ]++; } sort( voter ,voter+n ); int l = 1 ,r = n ,ans; while( l<=r ){ int l_mid = l+(r-l)/3; int r_mid = r-(r-l)/3; if( check(l_mid)<=check(r_mid) ){ ans = check(l_mid); r= r_mid-1; } else{ ans = check(r_mid); l = l_mid+1; } } cout << ans << endl; return 0; }
//cf - edu40 -D //图论 bfs球最短路的应用 #include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; int sp[1500][1500] ,edge[1500][1500] ,vis[1500]; int n ,m ,s ,t; void bfs( int x ){ memset( vis ,0 ,sizeof(vis) ); queue<int> q; q.push( x ); vis[x] = 1; while( !q.empty() ){ int t = q.front(); q.pop(); for( int i=1 ;i<=n ;i++){ if( edge[t][i] && !vis[i] ){ vis[i] = 1; sp[i][x] = sp[x][i] = sp[t][x]+1; q.push(i); } } } return ; } int main( ){ memset( sp ,inf ,sizeof(sp) ); memset( edge ,0 ,sizeof(edge) ); ios::sync_with_stdio( false ); cin >>n >>m >>s >>t; while( m-- ){ int a, b; cin>>a >>b; sp[a][b]=edge[a][b]=1; sp[b][a]=edge[b][a]=1; } //init for( int i=1 ;i<=n ;i++){ sp[i][i] = 0; } bfs( s ); bfs( t ); int ans = 0; for( int i=1 ;i<=n ;i++){ for( int j=i+1 ;j<=n ;j++){ if( !edge[i][j] ) //增边后对新增s->t路径的长度判断 if( sp[s][i]+sp[t][j]+1>=sp[s][t] && sp[s][j]+sp[t][i]+1>=sp[s][t]) ans++; } } cout<<ans; return 0; }
/* 16 0 12 0 12 0 12 0 12 2 1 2 1 3 500 3 500 3 500 3 500 3 500 4 500 4 500 4 500 4 500 4 500 */
//巧用与标准相对+-的贪心 // E-tap #include<bits/stdc++.h> #define rep(i ,x ,y) for(int i=x ;i<=y ;++i) #define repd(i ,x ,y) for(int i=y ;i>=x ;--i) #define N 200050 using namespace std; typedef long long ll; typedef long double ld; int n ,T ,a[N] ,t[N]; struct Tap{ int a ,t; Tap( int a ,int t):a(a) ,t(t){} bool operator < (const Tap & s)const{ return t < s.t; } }; vector<Tap> x[2]; int main( ){ ld ans = 0; ll sum[2] = {0 ,0}; scanf( "%d%d" ,&n ,&T); rep(i ,1 ,n)scanf( "%d" ,&a[i]); rep(i ,1 ,n){ scanf( "%d" ,&t[i] ); t[i] -= T; if( t[i]==0 )ans += a[i]; else x[t[i]<0].push_back( Tap( a[i] ,abs( t[i] )) ); } //cout<<"1 "<<ans<<endl; rep( i, 0 ,1){ for( vector<Tap>::iterator it= x[i].begin() ;it!=x[i].end() ;it++ ) sum[i] += 1ll * (* it).a * (*it).t; } if( sum[0] >sum[1] ){ swap(x[0] ,x[1]); swap(sum[0] ,sum[1]); } sort( x[1].begin() ,x[1].end() ); for( vector<Tap>::iterator it= x[0].begin() ;it!=x[0].end() ;it++ ) ans += (* it).a; //cout<<"2 "<<ans<<endl; for( vector<Tap>::iterator it= x[1].begin() ;it!=x[1].end() ;it++ ){ if( 1ll * (* it).t *(* it).a >= sum[0] ){ //cout << ans<<" "<<sum[0]<<" "<<1ll*(* it).t *(* it).a<<endl; ans += 1.0* sum[0]/(* it).t; //cout<<ans<<endl; break; } //cout << ans<<" "<<sum[0]<<" "<<1ll*(* it).t *(* it).a<<endl; ans += (* it).a ,sum[0] -= 1ll*(* it).t *(* it).a; } printf("%Lf " ,ans); return 0; }
以上是关于7.2的主要内容,如果未能解决你的问题,请参考以下文章