2018 7-7 计蒜客训练赛 BAPC 2014 Preliminary

 A. Choosing Ice Cream

题目链接:https://nanti.jisuanke.com/t/28201

题目大意:给出n个冰激凌,一个k面的骰子,求投掷多少次可使得每个冰激凌获得相同的概率,(1<=n,k<=10^9)

解答:k^x%n==0,求x. 当n==1时,x=0.当n!=1时,由于数据最大10^9,顶多循环32次方,期间不断取模(不然会溢出)

(a*b)%n=((a%n)*b)%n

#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<set>
#define maxn 1505
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define LL long long
#define e 2.71828182
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
       LL n,k;
       LL tt=1;
       int ans;
       int flag=0;
       cin>>n>>k;
       if(n==1)
           cout << 0 << endl;
	   else
       {
           for(int i=1;i<=31;i++)
           {
              tt=(tt*k)%n;
              if(tt==0)
              {
                 flag=1;
                 ans=i;
                 break;
              }
              
           }
           if(flag==0)
           {
              cout << "unbounded\n";
           }
           else
             cout << ans << endl;
       }
       
    }
    return 0;
}

D. Lift Problems

题目链接:https://nanti.jisuanke.com/t/28204

题目大意:n层的电梯,每一层有想下电梯的人数,给电梯一定的stop(不定),使得所有人的愤怒值降到最大,求这个最小的愤怒值,愤怒值的计算方法定义:如果在某一层停,其后所有楼层的人愤怒值加一,前面还没有下楼的人的愤怒值是与其自身楼层差的累加

题解:万万没想到是动态规划,本身就不熟,关键是要找好状态的转移方案

以一个anger[maxn+1]数组来存储停在i层时的最小愤怒值,anger[0]=0 , 对于i层前面的所有楼层分别停下时的状态,确定i层的最小愤怒值

#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<set>
#define maxn 1505
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define LL long long
#define e 2.71828182
using namespace std;
int a[maxn];
int main()
{
    int t;
    int n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int ss=0;
        for(int i=1;i<=n;i++)
        {
           cin>>a[i];
           ss+=a[i];
        }
        int anger[maxn+1];
        anger[0]=0;
        int skipanger;
        for(int i=1;i<=n;i++)
        {
           ss-=a[i];
           skipanger=0;
           anger[i]=INF;
           for(int j=i-1;j>=0;j--)
           {
              anger[i]=min(anger[i],anger[j]+skipanger+ss);
              skipanger+=(i-j)*a[j];
           }
        }
        cout << anger[n] << endl;
    }
    return 0;
}

 J. Word Search

https://nanti.jisuanke.com/t/28210

题目大意:给出一个矩阵格子,每个格子里头有字母,然后给出n个单词,要求在这个矩形格子中匹配(有8个方向)根据不同情况,输出结果。

题解:暴力匹配查找,关键要在每一个坐标对每个单词匹配,从8个反向,注意如果是回文单词,匹配结果数量要/2,如果是单个字符,/8

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<set>
#define maxn 35
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define LL long long
#define e 2.71828182
using namespace std;
int n,h,w;
char Grid[maxn][maxn];
int dir[8][2]={{0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
char word[maxn];
bool book[maxn][maxn];
bool isHw(char s[])
{
   int len=strlen(s);
   for(int i=0;i<len/2;i++)
   {
      if(s[i]!=s[len-1-i])
         return false;
   }
   return true;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
       cin>>n>>h>>w;
       for(int i=0;i<h;i++)
         cin>>Grid[i];
      bool nosolution,ambigous;
      nosolution=false;ambigous=false;
      memset(book,false,sizeof(book));
      for(int c=0;c<n;c++)
      {
         cin>>word;
         int len=strlen(word);
         int x,y;
         int cnt=0;
         for(int i=0;i<h;i++)
         {
            for(int j=0;j<w;j++)
            {
               for(int d=0;d<8;d++)
               {
                  int flagz=0;
                  for(int k=0;k<len;k++)
                  {
                     x=i+k*dir[d][0];
                     y=j+k*dir[d][1];
                     if(Grid[x][y]!=word[k]||x<0||y<0||x>=h||y>=w)
                     {
                        flagz=1;
                        break;
                     }
                  }
                  if(flagz==0)
                  {
                     cnt++;
                     for(int k=0;k<len;k++)
                     {
                        book[i+k*dir[d][0]][j+k*dir[d][1]]=true;
                     }
                  }
               }
            }
         }
         //cout << cnt << endl;
         if(isHw(word)&&len>1)
         {
            cnt/=2;
         }
         if(len==1)
         {
            cnt/=8;
         }
         if(cnt==0)
            nosolution=true;
         else if(cnt>1)
            ambigous=true;
      }
      if(nosolution==true)
         cout << "no solution\n";
      else if(ambigous==true)
         cout << "ambiguous\n";
      else
      {
         bool isEmpty=true;
         for(int i=0;i<h;i++)
         {
            for(int j=0;j<w;j++)
            {
               if(book[i][j]==false)
               {
                  isEmpty=false;
                  cout << Grid[i][j];
               }
            }
         }
         if(isEmpty)
            cout << "empty solution";
         cout << endl;
      }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wwwlps/article/details/80957042