10.5個人賽
C - Gas Pipeline CodeForces - 1207C (思維)
傳送門
題目大意:給一串01序列代表道路,1代表有車通過,需要在路上設管道,管道由柱子駕着,在1上的管道高度必須爲2,以保證車可以通過,在管道上升或者下降時多佔1單位長度,分別給出架設單位管道和柱子單位長度的費用,求出最小總架設費用
思路:見代碼註釋
#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include<time.h>
#include <stack>
#include <list>
#include <set>
#include <sstream>
#include <iterator>
#define ll long long int
#define fro(i,a,n) for(ll i=a;i<n;i++)
#define pre(i,a,n) for(ll i=n-1;i>=a;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
#define fi first
#define se second
#define s_d(a) scanf("%d",&a)
#define s_lld(a) scanf("%lld",&a)
#define s_s(a) scanf("%s",a)
#define s_ch(a) scanf("%c",&a)
using namespace std;
typedef pair<ll,ll> P;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
const double PI = 3.1415926535897932;
const double EPS=1e-6;
const int INF=0x3f3f3f3f;
const int maxn = 1e3+10;
int lowbit(int x){return x&(-x);}
int main()
{
int t;
cin>>t;
while(t--)
{
ll len,b,c;
string s;
cin>>len>>b>>c;
cin>>s;
ll sum=(len+1)*c+len*b;//初始化總和爲全爲長度1的高架和管子
int i=0,j=len-1;
while(s[i]!='1'&&i<len)//從頭找高出的那段,即‘1’
i++;
while(s[j]!='1'&&j>0)//從尾開找
j--;
if(j!=0)
sum+=(j-i+2)*c+2*b;//把區間內高度再加一倍,管子*2
int len2=0;//中間0的長度
for(;i<=j;i++)
{
if(s[i]=='0')
len2++;
else if(s[i]=='1'&&len2!=0)
{
if((len2-1)*c>b*2)//費用比較
sum=sum-(len2-1)*c+2*b;//高度減一,長度*2
len2=0;
}
}
cout<<sum<<endl;
}
return 0;
}
A - MP3 CodeForces - 1198A(尺取法)
傳送門
題目大意:就是給你兩個數字n和l,n代表n個長度的序列,l代表給你的內存大小(單位是byte=8位),然後你要取一個區間範圍,在範圍之外的全部改成範圍的左邊界和右邊界,使得儘量把給的空間塞滿,問你最少改多少個。(使用的內存大小 = nk)
思路:找到最大的K後,pow(2,k)和不同的個數比較,如果大輸出0。先求得每個區間不同數的個數,相當於前綴和,最後尺取法解決區間查詢(尺取法複雜度爲線性)。
#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include<time.h>
#include <stack>
#include <list>
#include <set>
#include <sstream>
#include <iterator>
#define ll long long int
#define fro(i,a,n) for(ll i=a;i<n;i++)
#define pre(i,a,n) for(ll i=n-1;i>=a;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
#define fi first
#define se second
#define s_d(a) scanf("%d",&a)
#define s_lld(a) scanf("%lld",&a)
#define s_s(a) scanf("%s",a)
#define s_ch(a) scanf("%c",&a)
using namespace std;
typedef pair<ll,ll> P;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
const double PI = 3.1415926535897932;
const double EPS=1e-6;
const int INF=0x3f3f3f3f;
const int maxn = 2e5+10;
int lowbit(int x){return x&(-x);}
P sum[maxn];
int tree[maxn<<2];
int a[maxn];
int b[maxn];
int n;
int main()
{
ios::sync_with_stdio(0);
ll n,i;
cin>>n>>i;
ll ii=i*8;
set<ll> s;
vector<ll> vec;
map<int,int> m;
fro(i,1,n+1)
{
ll a;
cin>>a;
s.insert(a);
vec.push_back(a);
}
ll len=s.size();
int k=ii/n;
k=min(18,k);
k=pow(2,k);
if(k>=len)
cout<<0<<endl;
else
{
//cout<<"???"<<endl;
sort(vec.begin(),vec.end());
b[0]=1;
fro(i,1,vec.size()+1)//每個區間不同的個數
{
if(vec[i]==vec[i-1])
b[i]=b[i-1];
else
{
b[i]=b[i-1]+1;
}
}
//cout<<"???"<<endl;
ll l=0,r=0;
ll ans=INF;
int sum=0;
while(r<n)
{
sum=b[r];
if(sum-b[l]>=k)
sum-=b[l++];
if(sum==k)//比較取最小
ans=min(ans,n-(r-l+1));
r++;//頭++
}
cout<<ans<<endl;
}
return 0;
}
B - Welfare State CodeForces - 1198B (線段樹+懶標記,重點理解)
傳送門
題目大意:題目大意爲有n個市民,每個市民有ai點數財富,以下有q次操作,操作類型爲兩類,1類:把第p個市民的財富改爲x,2類:把所有財富小於x的市民的財富變爲x。
思路:比賽時一直想線段樹,但苦於能力不足,無法實現,比賽後發現是自己線段樹精華沒有學到---------懶標記,這個還要慢慢理解理解,先上代碼。
#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include<time.h>
#include <stack>
#include <list>
#include <set>
#include <sstream>
#include <iterator>
#define ll long long int
#define fro(i,a,n) for(ll i=a;i<n;i++)
#define pre(i,a,n) for(ll i=n-1;i>=a;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
#define fi first
#define se second
#define s_d(a) scanf("%d",&a)
#define s_lld(a) scanf("%lld",&a)
#define s_s(a) scanf("%s",a)
#define s_ch(a) scanf("%c",&a)
using namespace std;
typedef pair<ll,ll> P;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
const double PI = 3.1415926535897932;
const double EPS=1e-6;
const int INF=0x3f3f3f3f;
const int maxn = 2e5+10;
int lowbit(int x){return x&(-x);}
int tree[maxn<<2];
int a[maxn];
int lazy[maxn<<2];
void pushup(int rt)
{
tree[rt]=min(tree[rt<<1],tree[rt<<1|1]);
}
void pushdown(int rt)
{
if(lazy[rt])
{
if(tree[rt<<1]<lazy[rt])
{
tree[rt<<1]=lazy[rt];
lazy[rt<<1]=lazy[rt];
}
if(tree[rt<<1|1]<lazy[rt])
{
tree[rt<<1|1]=lazy[rt];
lazy[rt<<1|1]=lazy[rt];
}
lazy[rt]=0;
}
}
void buildtree(int l,int r,int rt)
{
if(l==r)
{
tree[rt]=a[l];
return ;
}
int mid=l+(r-l)/2;
buildtree(ls);
buildtree(rs);
pushup(rt);
}
void update1(int pos,int a,int l,int r,int rt)
{
if(l==r)
{
tree[rt]=a;
return;
}
int mid=l+(r-l)/2;
pushdown(rt);
if(pos<=mid)
update1(pos,a,ls);
else
update1(pos,a,rs);
pushup(rt);
}
void update2(int a,int b,int c,int l,int r,int rt)
{
if(a<=l&&b>=r)
{
if(tree[rt]>=c)
return;
tree[rt]=c;
lazy[rt]=c;
}
pushdown(rt);
int mid=l+(r-l)/2;
if(b<=mid)
update2(a,b,c,ls);
else if(mid<a)
update2(a,b,c,rs);
pushup(rt);
}
int query(int pos,int l,int r,int rt)
{
if(l==r)
return tree[rt];
pushdown(rt);
int mid=l+(r-l)/2;
if(pos<=mid)
return query(pos,ls);
else
return query(pos,rs);
}
int main()
{
ios::sync_with_stdio(0);
int n;
cin>>n;
fro(i,1,n+1)
{
cin>>a[i];
}
mem(lazy,0);
buildtree(1,n,1);
int m;
cin>>m;
int aa,b,c;
fro(i,0,m)
{
cin>>aa;
if(aa==1)
{
cin>>b>>c;
update1(b,c,1,n,1);
}
else
{
cin>>b;
update2(1,n,b,1,n,1);
}
}
//cout<<tree[4]<<" "<<tree[5]<<" "<<tree[6]<<" "<<tree[7]<<endl;
fro(i,1,n+1)
{
if(i==1)
cout<<query(i,1,n,1);
else
cout<<" "<<query(i,1,n,1);
}
cout<<endl;
return 0;
}