Codeforces Round #266 (Div. 2)解題報告
http://codeforces.com/contest/466
A題
注意比較單位站次所花費的錢的多少,分情況討論:如果b多,則直接全部用a買;若a多,則先買整數個的b,剩下x不足m站的,和b和x*a比較決定。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int n,m,a,b;
int main()
{
while(~scanf("%d%d%d%d",&n,&m,&a,&b))
{
if(b*1.0/m<a)
{
int ans=(n-n/m*m)*a;
if(ans<b)
printf("%d\n",(n-n/m*m)*a+n/m*b);
else
printf("%d\n",n/m*b+b);
}
else
printf("%d\n",n*a);
}
return 0;
}
/*
9 9 2 546
9 8 2 5454
18
18
*/
B
我用二分做的,列舉a二分查詢b,然後在列舉b二分查詢a,值得一提的是本題的資料卡的很嚴一定要讓二分和列舉的範圍足夠才行。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long LL;
LL n,a,b,aa,bb;
int main()
{
while(~scanf("%lld%lld%lld",&n,&a,&b))
{
n=n*6;
if(n<=a*b)
{
printf("%lld\n%lld %lld\n",a*b,a,b);
continue;
}
LL minn=99999999999;
int cnt=0;
if(n>a*b)
{
for(LL i=a;i<=100000;i++)
{
LL l=b,r=10000000000,mid;
while(l<=r)
{
mid=l+(r-l)/2;
//printf("%lld\n%lld %lld\n",l,r,mid);
if(i*mid>=n)
{
if(i*mid<minn)
{
minn=i*mid;
aa=i;
bb=mid;
//if(i==11)
// printf("%lld\n%lld %lld\n",minn,aa,bb);
}
r=mid-1;
}
else if(i*mid<n)
l=mid+1;
}
}
// printf("%lld\n",minn);
for(LL i=b;i<=100000;i++)
{
LL l=a,r=10000000000,mid;
while(l<=r)
{
mid=l+(r-l)/2;
//printf("%lld\n%lld %lld\n",l,r,mid);
if(i*mid>=n)
{
if(i*mid<minn)
{
minn=i*mid;
aa=mid;
bb=i;
// printf("%lld\n%lld %lld\n",minn,aa,bb);
}
r=mid-1;
}
else if(i*mid<n)
l=mid+1;
}
}
printf("%lld\n%lld %lld\n",minn,aa,bb);
}
}
return 0;
}
C
將一段陣列平均分成三段,我把前n項的和表示出來,接下來從頭for到尾,把s[i] ==sum/3點找出來,然後在他後面找 s[j]==2/3*sum.值得一提的是我們找的時候要注意j的範圍絕對不能使開頭和結尾的兩個元素。具體實現細節,請看程式碼:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long LL;
const int N=555555;
LL s[N],a,cnt,n;
int main()
{
while(~scanf("%lld",&n))
{
cnt=0;
for(int i=0;i<n;i++)
{
scanf("%lld",&a);
cnt+=a;
s[i]=cnt;
}
LL sum=0,ans=0;
for(int ans=0,i=0;i<n;i++)
{
if((i>0)&&(i<n-1)&&(s[i]*3==s[n-1]*2))
sum+=ans;
if(s[i]*3==s[n-1])
ans++;
}
printf("%lld\n",sum);
}
return 0;
}
D
這是一道區間匹配的題目:先把a陣列變成需要加多少變成h。然後在對a陣列前向差分得出b陣列。cnt:標記到當前位置,有幾個l沒有和r匹配如果b[i]==1:說明當前位置有一個l,cnt++;如果b[i]==0:1,當前位置什麼都沒有2,當前位置有一個l,一個r。因為有一個l,所以cnt++.有一個r,所以總數*=cnt,cnt--;相當於總數*=(cnt+1);如果b[i]==-1:當前位置有一個r,所以總數*=cnt,cnt--;如果b[i]不等於上面的三種情況,說明無解!
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
int a[10005],b[10005],n,h;
int main()
{
while(~scanf("%d%d",&n,&h))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]=h-a[i];
}
for(int i=1;i<=n+1;i++)
b[i]=a[i]-a[i-1];
LL cnt=0,ans=1;
for(int i=1;i<=1+n;i++)
{
if(b[i]==1)
cnt++;
else if(b[i]==0)
ans=(cnt+1)*ans%mod;
else if(b[i]==-1)
{
ans=ans*cnt%mod;
cnt--;
}
else
{
ans=0;
break;
}
}
printf("%lld\n",ans);
}
return 0;
}
相關文章
- Codeforces Round #537 (Div. 2)解題報告
- Codeforces Round 976 (Div. 2) 題解
- Codeforces Round 983 (Div. 2) 題解
- Codeforces Round #956 (Div. 2)題解
- Codeforces Round 951 (Div. 2) 題解
- Codeforces Round 965 (Div. 2) 題解
- Codeforces Round #681 (Div. 2)E題解
- Codeforces Round 932 (Div. 2) DE 題解
- Educational Codeforces Round 93 (Rated for Div. 2)題解
- Codeforces Round #665 (Div. 2)A-C題解
- Codeforces Round #673 (Div. 2)(A-D)題解
- Codeforces Round #439 (Div. 2) A-C題解
- Codeforces Round #Pi (Div. 2) (ABCD題解)
- Codeforces Round 976 (Div. 2) 題解(A-E)
- Codeforces Round 893 (Div. 2)題解記錄
- Codeforces Round 892 (Div. 2)題解記錄
- Educational Codeforces Round 171 (Rated for Div. 2) 題解
- Codeforces Round #186 (Div. 2) (ABCDE題解)
- Codeforces Round #105 (Div. 2) (ABCDE題解)
- Codeforces Round #200 (Div. 2) (ABCDE題解)
- Codeforces Round #313 (Div. 2) (ABCDE題解)
- Codeforces Round #323 (Div. 2) (ABCD題解)
- Codeforces Round #331 (Div. 2) (ABC題解)
- Codeforces Round 899 (Div. 2)題解記錄
- Codeforces Round 979 (Div. 2) (ABCD個人題解)
- Educational Codeforces Round 168 (Rated for Div. 2) 題解
- Codeforces Round 986 (Div. 2)題解記錄(A~D)
- Codeforces Round 987 (Div. 2)題解記錄(A~D)
- Codeforces Round 984 (Div. 3) 題解
- Codeforces Round 962 (Div. 3) 題解
- Codeforces Round 946 (Div. 3) 題解
- Codeforces Round #646 (Div. 2)【C. Game On Leaves 題解】GAM
- Codeforces Round #319 (Div. 2) (ABCE題解)
- Codeforces Round #315 (Div. 2) (ABCD題解)
- Codeforces Round 975 (Div. 2)題解記錄(A,B,E)
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #682 (Div. 2)