1. 程式人生 > 實用技巧 >Educational Codeforces Round 88 (Rated for Div. 2) B、New Theatre Square C、Mixing Water

Educational Codeforces Round 88 (Rated for Div. 2) B、New Theatre Square C、Mixing Water

題目連結:B、New Theatre Square

題意:

你要把所有“.” 都變成“*”,你可以有兩個選擇,第一種就是一次鋪一個方塊(1*1),第二種就是同一行一次鋪兩個(1*2)。第一種花費x,第二種花費y。問最少花費多少能把所有鋪完

題解:

如果y>=2*x,那麼就直接找到所有“.”,然後乘於x就行

否則就找倆倆一對就行了

程式碼:

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4
#include<string> 5 #include<queue> 6 #include<deque> 7 #include<string.h> 8 #include<map> 9 #include <iostream> 10 #include <math.h> 11 #define Mem(a,b) memset(a,b,sizeof(a)) 12 const double II = acos(-1); 13 const double PP = (II*1.0)/(180.00); 14 using namespace
std; 15 typedef long long ll; 16 const int INF=0x3f3f3f3f; 17 const int maxn=1000+10; 18 char s[maxn][maxn]; 19 int main() 20 { 21 int t; 22 scanf("%d",&t); 23 while(t--) 24 { 25 int n,m,x,y; 26 scanf("%d%d%d%d",&n,&m,&x,&y); 27 for(int i=1; i<=n; ++i)
28 scanf("%s",s[i]+1); 29 int sum_white=0,sum=0; 30 for(int i=1; i<=n; ++i) 31 { 32 for(int j=1; j<=m; j++) 33 { 34 if(s[i][j]=='*') continue; 35 if(s[i][j]=='.' && s[i][j+1]=='.') 36 { 37 sum_white+=2; 38 sum+=y; 39 j++; 40 continue; 41 } 42 else 43 { 44 sum_white++; 45 sum+=x; 46 j++; 47 } 48 } 49 } 50 if(2*x<=y) 51 printf("%d\n",sum_white*x); 52 else 53 { 54 printf("%d\n",sum); 55 } 56 } 57 return 0; 58 }
View Code

題目連結:C、Mixing Water

題意:

往一個無限深的桶裡面倒水,先倒入熱水再倒入涼水,熱水溫度h,涼水溫度c。給你一個溫度n,問你倒多少次水才可以是水桶內溫度最接近n

題解:

如果倒入水的次數是偶數,那麼溫度一直是 (h+c)/2

如果倒入水次數是奇數,會得到水桶內溫度 y=((x+1)*h+c*x)/(2*x+1) (x是倒入涼水次數)

可見如果將x只取奇數(1,3,5,7...)那麼這就是一個單調遞減函式。所以二分求解就可以了

也可以這樣理解,你倒入x杯涼水和x杯熱水之後溫度是(h+c)/2,那麼你有多倒入一杯熱水,那麼這杯熱水的溫度肯定被所有杯水平分,那麼x越大,每單獨一杯分配到的水溫越小

程式碼:

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<deque>
#include<string.h>
#include<map>
#include <iostream>
#include <math.h>
#define Mem(a,b) memset(a,b,sizeof(a))
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=1000+10;
const double eps=1e-6;
const double PI=acos(-1);
ll h,c,t;
int main()
{
    ll tt;
    cin>>tt;
    while(tt--)
    {
        cin>>h>>c>>t;
        if(h+c>>1>=t)
        {
            cout<<2<<endl;
        }
        else
        {
            //ll x=(h-t)*1.0/(2*t-h-c);
            //cout<<(fabs(t-get(x))<=fabs(t-get(x+1))?2*x+1:2*x+3)<<endl;
            ll l=0,r=1e9,ans=0;
            while(l+1<r)
            {
                //printf("%I64d %I64d**\n",l,r);
                ll mid=l+r>>1;
                //if(mid%2!=0)  mid--;
                double temp=((h+c)*mid*1.0+h)/(mid*2.0+1.0);
                if(temp>=t)
                {
                    ans=mid;
                    l=mid;
                }
                else
                {
                    r=mid;
                }
            }
            double temp1=((h+c)*ans*1.0+h)/(ans*2.0+1.0);
            ans++;
            double temp2=((h+c)*ans*1.0+h)/(ans*2.0+1.0);
            if(fabs(temp1-t)<=fabs(t-temp2))
            {
                printf("%I64d\n",(ans-1)*2+1);
            }
            else printf("%I64d\n",ans*2+1);
        }
    }
    return 0;
}