Skip to main content
  1. Posts/

hdu 6034 2017 Multi-University Training Contest - Team 1 B Balala Power! (贪心)

·3 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

http://acm.hdu.edu.cn/showproblem.php?pid=6034

题意:
#

有一个仅由小写字母组成的字符串,要求将a..z的字母,对应到0..25,每个数字只能被一个字母对应,得到一个26进制的数,现在问这个数最大是多少。注意不允许有前导0,除非这个数本身就是0.

思路:
#

贪心。看哪个字母的权值最大。

可以用类似高精度加法的方式处理。

对于前导0的部分,用一个bool标记首位(如果字符串长度为1就不标记)

对于26个高精度数的排序,可以用交换id的技巧。

以及,如果排序之后,发现权值最小的数被对应到数字0,但是又在某个长度大于1的字符串的首字母位置出现过。

我初始想当然得,把该字母和最小的可以出现在首位的字母交换….

然而这很错啊?

实际上,更优秀做法是,找到第一个可以出现在首位的字母,然后从该字母后面到结尾,依次前移一位,最后把这个可以出现在首位的字母放在最后。

比如这组数据:

7 abcdefghijklmnopqrstuvwxyz zz yy xx ww uu vv

权值按照从大到小排列

z u v w x y t 显然不如  u v w x y z t  优

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2017年11月01日 星期三 00时56分26秒
  4File Name :6034.cpp
  5************************************************ */
  6
  7#include <bits/stdc++.h>
  8#define PB push_back
  9#define fst first
 10#define sec second
 11#define lson l,m,rt<<1
 12#define rson m+1,r,rt<<1|1
 13#define ms(a,x) memset(a,x,sizeof(a))
 14typedef long long LL;
 15#define pi pair < int ,int >
 16#define MP make_pair
 17
 18using namespace std;
 19const double eps = 1E-8;
 20const int dx4[4]={1,0,0,-1};
 21const int dy4[4]={0,-1,1,0};
 22const int inf = 0x3f3f3f3f;
 23const int N=1E5+7;
 24const LL mod =1E9+7;
 25int n;
 26string st[N];
 27int cnt[26][N];
 28int maxlen;
 29int id[26];
 30int power[26];
 31LL base26[N];
 32bool Beg[26];
 33void init()
 34{
 35    ms(Beg,false);
 36    ms(cnt,0);
 37    maxlen = 0 ;
 38    for ( int i = 0 ; i < 26 ; i++) id[i] = i;
 39}
 40bool small(int *a,int *b)
 41{
 42    for ( int i = maxlen-1 ; i >= 0 ; i--)
 43    {
 44    if (a[i]<b[i]) return true;
 45    if (a[i]>b[i]) return false;
 46    }
 47    return false;
 48}
 49int main()
 50{
 51    #ifndef  ONLINE_JUDGE
 52    freopen("./in.txt","r",stdin);
 53  #endif
 54    int cas = 0 ;
 55    base26[0] = 1;
 56    for ( int i = 1 ; i < N ; i++) base26[i] = base26[i-1] * 26 % mod;
 57    while (~scanf("%d",&n))
 58    {
 59        init();
 60        for ( int i = 1 ; i <= n ; i++)
 61        {
 62        cin>>st[i];
 63        int len = st[i].length();
 64    //  cout<<"len:"<<len<<endl;
 65        maxlen = max(len,maxlen);
 66        for ( int j = 0 ; j < len ; j++)
 67        {
 68            int v = st[i][j]-'a';
 69            cnt[v][len-j-1]++;
 70        }
 71        //如果只有一个字母,不要打首字母标记
 72        if (len>1)
 73        Beg[st[i][0]-'a'] = true;
 74        //忘了不能右前导0的条件了。。。
 75        }
 76        for ( int i = 0 ; i < 26 ; i++)
 77        {
 78        for ( int j = 0 ; j < maxlen ; j++)
 79        {
 80            cnt[i][j+1] += cnt[i][j]/26;
 81            cnt[i][j]%=26;
 82        }
 83        while (cnt[i][maxlen]>0)
 84        {
 85            cnt[i][maxlen+1] += cnt[i][maxlen]/26;
 86            cnt[i][maxlen]%=26;
 87            maxlen++;
 88        }
 89        }
 90
 91        for ( int i = 0 ; i < 26 ; i++)
 92        for ( int j = i+1 ; j < 26 ; j++)
 93            if (small(cnt[id[i]],cnt[id[j]]))
 94            swap(id[i],id[j]);
 95//  for (int i = 0 ; i < 26 ; i++) printf("id[%d]:%c%c",i,char(id[i]+'a'),i==25?'\n':'\n');
 96
 97    int notBEG = -1;
 98        if (Beg[id[25]])
 99        {
100        for ( int i = 24 ;  i >= 0 ; i--)
101        {
102            if (!Beg[id[i]])
103            {
104            notBEG = i;
105            break;
106            }
107        }
108        }
109        //cout<<"notBEG:"<<notBEG<<endl;
110        if (notBEG!=-1)
111        {
112        int tmp = id[notBEG];
113        for ( int i = notBEG +1 ; i < 26 ; i++) id[i-1] = id[i];
114        id[25] = tmp;
115        }
116        //for ( int i = 0 ; i < 26 ; i++) printf("id[%d]:%c\n",i,char(id[i]+'a'));
117
118        for ( int i = 0 ; i < 26 ; i++) power[id[i]] = 25-i;
119        LL ans = 0;
120        for ( int i = 1 ; i <= n ; i++)
121        {
122        int len = st[i].length();
123        for ( int j = 0 ; j < len ; j++)
124        {
125            int pos = len-j-1;
126            int v = st[i][j]-'a';
127            LL tmp = (base26[pos]*power[v])%mod;
128        //    cout<<"tmp:"<<tmp<<endl;
129            ans = (ans + tmp)%mod;
130        }
131        }
132        printf("Case #%d: %lld\n",++cas,ans);
133
134    }
135
136
137  #ifndef ONLINE_JUDGE
138  fclose(stdin);
139  #endif
140    return 0;
141}

Related

codeforces #413 B T-shirt buying (贪心)

·2 mins
题目链接 题意:有n个T恤,每个价格都不同,有三种颜色,分别用1,2,3表示,每件T恤给出前xiong和后背的颜色。现在有m个顾客排成一队,对于每个顾客,给出他喜欢的颜色,只要一个T恤的前xiong或者后背的颜色之一满足该颜色即可。顾客总希望买符合他喜欢颜色的T恤中价格最低的。现在问每个顾客买到的T恤的价格,如果某个顾客没有买T恤,输出-1

codeforces #375 D. Lakes in Berland (dfs)

·2 mins
题目链接 题意:nm个格子,有和.两种类型。定义一个湖为边相邻的只有.组成的最大点集合,且任何一个.不在边界上。现在给出一个nm的图保证至少有k个湖。问填多少个.成,才能使得恰好有k个湖。

whust 2016 #1 D Zhenya moves from the dormitory (贪心,模拟)

·1 min
题目链接 傻逼模拟。。读完题就ac了。。。 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月07日 星期日 18时04分18秒 4File Name :code/whust2016/#1/D.cpp 5************************************************ */ 6#include <cstdio> 7#include <cstring> 8#include <iostream> 9#include <algorithm> 10#include <vector> 11#include <queue> 12#include <stack> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <deque> 19#include <ctime> 20#define fst first 21#define sec second 22#define lson l,m,rt<<1 23#define rson m+1,r,rt<<1|1 24#define ms(a,x) memset(a,x,sizeof(a)) 25typedef long long LL; 26#define pi pair < int ,int > 27#define MP make_pair 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=280; 34int n,m; 35int total,adva,advb; 36struct Friend 37{ 38 int money; 39 int adv; 40}f[N]; 41struct Room 42{ 43 int type; 44 int cost; 45 int adv; 46}r[N]; 47struct Ans 48{ 49 int val; 50 int rid; 51 int fid; 52 bool operator < (Ans b)const 53 { 54 return val>b.val; 55 } 56}ans[300*300]; 57int main() 58{ 59 #ifndef ONLINE_JUDGE 60 freopen("code/in.txt","r",stdin); 61 #endif 62 cin>>total>>adva>>advb; 63 cin>>n; 64 for ( int i = 1 ; i <= n ; i++) 65 scanf("%d %d",&f[i].money,&f[i].adv); 66 scanf("%d",&m); 67 for ( int i = 1 ; i <= m ; i++) 68 scanf("%d%d%d",&r[i].type,&r[i].cost,&r[i].adv); 69 int cnt = 0 ; 70 for ( int i = 1 ; i <= m ; i++) 71 { 72 if (r[i].type==1) 73 { 74 if (r[i].cost<=total) 75 { 76 cnt++; 77 ans[cnt].val = r[i].adv+adva; 78 ans[cnt].rid = i; 79 ans[cnt].fid = -1; 80 } 81 continue; 82 } 83 else 84 { 85 for ( int j = 0 ; j <= n ; j++) 86 { 87 if (j==0) //自己住双人间 88 { 89 if (r[i].cost<=total) 90 { 91 cnt++; 92 ans[cnt].val = r[i].adv+advb; 93 ans[cnt].rid = i ; 94 ans[cnt].fid = -1; 95 } 96 } 97 else 98 { 99 if (r[i].cost<=total*2&&r[i].cost<=f[j].money*2) 100 { 101 cnt++; 102 ans[cnt].val = r[i].adv+f[j].adv; 103 ans[cnt].rid = i ; 104 ans[cnt].fid = j; 105 } 106 } 107 } 108 } 109 } 110// for ( int i = 1 ; i <= cnt ; i++) 111// { 112// printf("val:%d room: %d friend : %d \n",ans[i].val,ans[i].rid,ans[i].fid); 113// } 114 if (cnt==0) 115 { 116 puts("Forget about apartments. Live in the dormitory."); 117 }else 118 { 119 sort(ans+1,ans+cnt+1); 120 if (r[ans[1].rid].type==1) 121 { 122 printf("You should rent the apartment #%d alone.\n",ans[1].rid); 123 } 124 else 125 { 126 if (ans[1].fid==-1) 127 { 128 printf("You should rent the apartment #%d alone.\n",ans[1].rid); 129 } 130 else 131 { 132 133 printf("You should rent the apartment #%d with the friend #%d.\n",ans[1].rid,ans[1].fid); 134 135 } 136 } 137 } 138 #ifndef ONLINE_JUDGE 139 fclose(stdin); 140 #endif 141 return 0; 142}