C.Hidden Word 链接:http://codeforces.com/contest/725/problem/C
昨天打比赛时没弄清楚题意,不过现在对题意还有怀疑,You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. 我理解的意思是 给的每个字母最少在字符串中出现一次,但看了所有数据才知道正确意思是 26个字母每个字母最少出现1次……昨天就按着自己的思路一直写,最后就GG了。
本题其实很好构造的,如果两个相同的字母相邻,肯定不可以。然后就把两个相邻的字母夹的一段,对半折叠,放到最后,再去拿剩余的首段和末段去凑前面的。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 30; 7 char s[maxn]; 8 char s1[maxn]; 9 char s2[maxn]; 10 int main() 11 { 12 scanf("%s",s); 13 int cnti = 0; 14 int cntj = 0; 15 for(int i=0;i<27;i++) 16 { 17 for(int j=i+1;j<27;j++) 18 { 19 if(s[i]==s[j]) 20 { 21 cnti = i; 22 cntj = j; 23 } 24 } 25 } 26 if(cnti+1==cntj) 27 { 28 printf("Impossible "); 29 } 30 else 31 { 32 33 int half = (cntj-cnti-1)/2; 34 int nex = cnti; 35 for(int i=12-half;i<=12;i++) 36 { 37 s1[i] = s[nex++]; 38 } 39 int cur = cnti; 40 for(int i=12-half-1;i>=0&&cur>0;i--) 41 { 42 s1[i] = s[--cur]; 43 } 44 for(int i=12;i>=0&&nex<27;i--) 45 { 46 if(s[cnti]==s[nex]) 47 { 48 nex++; 49 } 50 s2[i] = s[nex++]; 51 } 52 if(cnti<26-cntj) 53 { 54 for(int i=0;nex<27;i++) 55 { 56 s1[i] = s[nex++]; 57 } 58 } 59 else if(cnti>26-cntj) 60 { 61 for(int i=0;cur>0;i++) 62 { 63 s2[i] = s[--cur]; 64 } 65 } 66 67 printf("%s ",s1); 68 printf("%s ",s2); 69 } 70 return 0; 71 } 72 /* 73 ABCDEFGIJKLMNOPQRSGTUVWXYZH 74 */
D. Contest Balloons 链接:http://codeforces.com/contest/725/problem/D
这题本来想着优先队列做,结果被sort排序搞炸了,待会再说。
先对全部的结构体排个序,以气球数量从大到小。先把大于Limak气球数量的人压入队列,然后将Limak的气球逐渐减少,每次都减小为下一个数。前后的差值即为送他人的气球。然后这一过程一直取最小值。还要注意Limak跑到最后时,可以把气球全部贡献出来。
我被sort搞炸了。
bool cmp(node A,node B)
{
if(A.l!=B.l) return A.l>B.l;
// else return A.k<B.k;
}
这样就wa,
bool cmp(node A,node B)
{
if(A.l!=B.l) return A.l>B.l;
else return A.k<B.k;
}
这样就AC。
k值是按输入的顺序赋值的,可以看代码,
就是我本来以为
11 13 1
12 13 2
11 13 3
11 13 4
对第一个数排完序后,
12 13 2
11 13 1
11 13 3
11 13 4
后面1 3 4的相对顺序不变。结果就这样炸了。希望路过的大牛可以指点下。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 struct node 8 { 9 int k; 10 long long l,r,x; 11 friend bool operator < (node A,node B) 12 { 13 return A.x>B.x; 14 } 15 }; 16 priority_queue<node> q; 17 bool cmp(node A,node B) 18 { 19 if(A.l!=B.l) return A.l>B.l; 20 else return A.k<B.k; 21 } 22 const int maxn = 3e5+5; 23 node arr[maxn]; 24 int main() 25 { 26 int n; 27 cin>>n; 28 long long x = 0; 29 for(int i=1;i<=n;i++) 30 { 31 scanf("%I64d %I64d",&arr[i].l,&arr[i].r); 32 arr[i].x = arr[i].r-arr[i].l+1; 33 arr[i].k = i; 34 } 35 x = arr[1].l; 36 sort(arr+1,arr+n+1,cmp); 37 int i = 0; 38 for(i=1;i<=n;i++) 39 { 40 if(arr[i].k==1) 41 { 42 break; 43 } 44 q.push(arr[i]); 45 } 46 int best = q.size(); 47 node cur; 48 if(!q.empty()) 49 cur = q.top(); 50 long long cost = 0; 51 for(i++;i<=n;i++) 52 { 53 cost += x-arr[i].l; 54 x = arr[i].l; 55 if(!q.empty()) 56 cur = q.top(); 57 while(!q.empty()&&cur.x<=cost) 58 { 59 q.pop(); 60 cost-=cur.x; 61 if(q.empty()) break; 62 cur = q.top(); 63 } 64 int tt = q.size(); 65 // printf("%d ",tt); 66 best = min(best,tt); 67 if(q.empty()) break; 68 q.push(arr[i]); 69 } 70 if(!q.empty()) 71 cur = q.top(); 72 cost += x; 73 while(!q.empty()&&cur.x<=cost) 74 { 75 q.pop(); 76 cost-=cur.x; 77 if(q.empty()) break; 78 cur = q.top(); 79 } 80 int tt = q.size(); 81 best = min(best,tt); 82 printf("%d ",best+1); 83 return 0; 84 } 85 /* 86 7 87 10 13 88 40 41 89 40 41 90 40 41 91 40 41 92 40 41 93 40 41 94 12 13 2 95 11 13 1 96 11 13 3 97 11 13 4 98 */