zoukankan      html  css  js  c++  java
  • SRM 584 div2

    早早地水完了三道题,pt1000用的是dfs,开始做的时候误认为复杂度最多就O(2^25),结果被一组O(2*3^16)的数据接近1e8给cha了。继续努力。

    pt250:求两个串的前缀组成的不同串数目。set搞定。

     1 /* 
     2  *Author:       Zhaofa Fang 
     3  *Created time: 2013-07-10-18.54 
     4  *Language:     C++ 
     5  */ 
     6 #include <cstdio> 
     7 #include <cstdlib> 
     8 #include <sstream> 
     9 #include <iostream> 
    10 #include <cmath> 
    11 #include <cstring> 
    12 #include <algorithm> 
    13 #include <string> 
    14 #include <utility> 
    15 #include <vector> 
    16 #include <queue> 
    17 #include <map> 
    18 #include <set> 
    19 using namespace std; 
    20 
    21 typedef long long ll; 
    22 #define DEBUG(x) cout<< #x << ':' << x << endl 
    23 #define FOR(i,s,t) for(int i = (s);i <= (t);i++) 
    24 #define FORD(i,s,t) for(int i = (s);i >= (t);i--) 
    25 #define REP(i,n) for(int i=0;i<(n);i++) 
    26 #define REPD(i,n) for(int i=(n-1);i>=0;i--) 
    27 #define PII pair<int,int> 
    28 #define PB push_back 
    29 #define MP make_pair 
    30 #define ft first 
    31 #define sd second 
    32 #define lowbit(x) (x&(-x)) 
    33 #define INF (1<<30) 
    34 #define eps 1e-8 
    35 
    36 class TopFox{ 
    37 public : 
    38     int possibleHandles(string f, string g){ 
    39         set<string>S; 
    40         int lenf = f.length(); 
    41         int leng = g.length(); 
    42         REP(i,lenf){ 
    43             REP(j,leng){ 
    44                 string tmp = ""; 
    45                 REP(k,i+1)tmp += f[k]; 
    46                 REP(k,j+1)tmp += g[k]; 
    47                 S.insert(tmp); 
    48             } 
    49         } 
    50         return S.size(); 
    51     } 
    52 }; 
    250

    pt500:给定一幅图的连接情况,要求相邻两个点的差不大于d,求点之间的最大差。

        可以对每个点进行bfs,比较直观。也有一些人用的是floyd,道理一样的。

     1 /* 
     2  *Author:       Zhaofa Fang 
     3  *Created time: 2013-07-10-18.54 
     4  *Language:     C++ 
     5  */ 
     6 #include <cstdio> 
     7 #include <cstdlib> 
     8 #include <sstream> 
     9 #include <iostream> 
    10 #include <cmath> 
    11 #include <cstring> 
    12 #include <algorithm> 
    13 #include <string> 
    14 #include <utility> 
    15 #include <vector> 
    16 #include <queue> 
    17 #include <map> 
    18 #include <set> 
    19 using namespace std; 
    20 
    21 typedef long long ll; 
    22 #define DEBUG(x) cout<< #x << ':' << x << endl 
    23 #define FOR(i,s,t) for(int i = (s);i <= (t);i++) 
    24 #define FORD(i,s,t) for(int i = (s);i >= (t);i--) 
    25 #define REP(i,n) for(int i=0;i<(n);i++) 
    26 #define REPD(i,n) for(int i=(n-1);i>=0;i--) 
    27 #define PII pair<int,int> 
    28 #define PB push_back 
    29 #define MP make_pair 
    30 #define ft first 
    31 #define sd second 
    32 #define lowbit(x) (x&(-x)) 
    33 #define INF (1<<30) 
    34 #define eps 1e-8 
    35 
    36 class Egalitarianism{ 
    37 public : 
    38     bool vist[100]; 
    39     int mon[100]; 
    40     bool OK; 
    41     int bfs(int s,vector <string> isFriend, int d){ 
    42         int n = isFriend.size(); 
    43         queue<int>Q; 
    44         Q.push(s); 
    45         int ans = -1; 
    46         memset(vist,0,sizeof(vist)); 
    47         mon[s] = 0; 
    48         vist[s] = 1; 
    49         while(!Q.empty()){ 
    50             int now = Q.front(); 
    51             Q.pop(); 
    52             REP(i,n){ 
    53                 if(isFriend[now][i] == 'Y' && !vist[i]){ 
    54                     Q.push(i); 
    55                     mon[i] = mon[now] + d; 
    56                     vist[i] = 1; 
    57                     ans = max(ans,mon[i]); 
    58                 } 
    59             } 
    60         } 
    61         REP(i,n)if(!vist[i])return -1; 
    62         return ans; 
    63     } 
    64     int maxDifference(vector <string> isFriend, int d){ 
    65         int n = isFriend.size(); 
    66         int ans = -1; 
    67         REP(i,n)ans = max(bfs(i,isFriend,d),ans); 
    68         return ans; 
    69     } 
    70 }; 
    500

    pt1000:给定一组数kind[],从中取K个,取得的数为found[],能有多少种可能。其中一组sample如下:

    {1, 2, 1, 1, 2, 3, 4, 3, 2, 2}
    {4, 2}
    3
    Returns: 6
    The archeologist excavated one of six possible triples of building sites:
    • (1, 4, 6)
    • (1, 6, 8)
    • (1, 6, 9)
    • (4, 6, 8)
    • (4, 6, 9)
    • (6, 8, 9)

      正解dp。很简单的转移-__-。

     1 /*
     2  *Author:       Zhaofa Fang
     3  *Created time: 2013-07-10-18.54
     4  *Language:     C++
     5  */
     6 #include <cstdio>
     7 #include <cstdlib>
     8 #include <sstream>
     9 #include <iostream>
    10 #include <cmath>
    11 #include <cstring>
    12 #include <algorithm>
    13 #include <string>
    14 #include <utility>
    15 #include <vector>
    16 #include <queue>
    17 #include <map>
    18 #include <set>
    19 using namespace std;
    20 
    21 typedef long long ll;
    22 #define DEBUG(x) cout<< #x << ':' << x << endl
    23 #define FOR(i,s,t) for(int i = (s);i <= (t);i++)
    24 #define FORD(i,s,t) for(int i = (s);i >= (t);i--)
    25 #define REP(i,n) for(int i=0;i<(n);i++)
    26 #define REPD(i,n) for(int i=(n-1);i>=0;i--)
    27 #define PII pair<int,int>
    28 #define PB push_back
    29 #define MP make_pair
    30 #define ft first
    31 #define sd second
    32 #define lowbit(x) (x&(-x))
    33 #define INF (1<<30)
    34 #define eps 1e-8
    35 
    36 ll C[55][55];
    37 
    38 void pre(){
    39     C[1][0] = C[1][1] = 1;
    40     FOR(i,2,50){
    41         C[i][0] = 1;
    42         FOR(j,1,i)C[i][j] = C[i-1][j-1] + C[i-1][j];
    43     }
    44 }
    45 class Excavations2{
    46 public :
    47     int ff[55];
    48     ll dp[55][55];
    49     ll count(vector <int> kind, vector <int> found, int K){
    50         pre();
    51         int len = kind.size();
    52         memset(ff,0,sizeof(ff));
    53         REP(i,len)ff[kind[i]]++;
    54         memset(dp,0,sizeof(dp));
    55         int n = found.size();
    56         FOR(i,1,ff[found[0]]){
    57             dp[1][i] = C[ff[found[0]]][i];
    58         }
    59         FOR(i,2,n){
    60             FOR(j,1,K){
    61                 FOR(k,1,ff[found[i-1]])
    62                     dp[i][j] += dp[i-1][j-k]*C[ff[found[i-1]]][k];
    63             }
    64         }
    65         return dp[n][K];
    66     }
    67 };
    68 
    69 
    70 //int main(){
    71 //    //freopen("in","r",stdin);
    72 //    //freopen("out","w",stdout);
    73 //
    74 //    return 0;
    75 //}
    1000
  • 相关阅读:
    417 Pacific Atlantic Water Flow 太平洋大西洋水流
    416 Partition Equal Subset Sum 分割相同子集和
    415 Add Strings 字符串相加
    414 Third Maximum Number 第三大的数
    413 Arithmetic Slices 等差数列划分
    412 Fizz Buzz
    410 Split Array Largest Sum 分割数组的最大值
    409 Longest Palindrome 最长回文串
    day22 collection 模块 (顺便对比queue也学习了一下队列)
    day21 计算器作业
  • 原文地址:https://www.cnblogs.com/fzf123/p/3183725.html
Copyright © 2011-2022 走看看