您的位置:首页 > 产品设计 > UI/UE

URAL 2026 Dean and Schedule 贪心、双端队列(deque)、队列(queue)

2016-07-31 00:44 381 查看
C - Dean and Schedule
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice URAL
2026

Description

A new academic year approaches, and the dean must make a schedule of classes for first-year students. There must be n classes in the schedule. The dean should take into account the following interesting observation made
in recent years: students skip all classes with
even numbers and attend all classes with odd numbers (the classes are numbered from 1). Of course the dean wants students to attend
as many important classes as possible, so he tries to assign subjects that are more important to places with odd numbers and subjects
that are less important to places with even numbers. The method of estimating the quality of the schedule at the Department of Mathematics and Mechanics must be as formal as possible.

The first-year schedule may contain any of 26 subjects taught at the department. We denote them by English letters from a to z. The importance of a subject corresponds to its position in the English alphabet.
Thus, subject a has importance 1, and subject z has importance 26. The quality of a schedule is the sum of importances of subjects in it, where subjects on odd places are counted with a
plus sign, and subjects on even places are counted with a minus sign.

Unfortunately, a shedule has some restrictions due to administrative reasons. First, the schedule must contain at least k different
subjects, so the dean cannot occupy all odd places with mathematical analysis and all even places with philosophy. Second, certain subjects must be assigned to certain places. Help the dean to make a schedule of maximum quality
under these restrictions.

Input

The first line contains a string of length n (1 ≤ n ≤ 10 5) consisting of lowercase English letters and question marks. The string
specifies the subjects that are already in the schedule. The letters denote these subjects, and the question marks stand for vacant places.
In the second line you are given an integer k (1 ≤ k ≤ 26), which is the minimum number of different subjects in the schedule.

Output

If it is impossible to replace all question marks by lowercase English letters so that the string would contain at least k different letters, output “-1” (without quotation marks). Otherwise, output any of the resulting
strings that maximizes the quality of the schedule given by the string.

Sample Input

inputoutput
??
1

za

??
3

-1

aza1

aza

aza3

-1

Notes

In the first sample the dean can make any schedule with two subjects (even identical), but the quality of the schedule “za” is 26 − 1 = 25, and this is the maximum possible value of the quality.

In the second sample it is impossible to make a schedule consisting of two classes with three different subjects.

In the third sample the dean has only one variant. Though the schedule is bad (1 − 26 + 1 = −24), nothing better can be proposed.

In the fourth sample the only possible variant doesn’t contain three different subjects.

Source

UESTC 2016 Summer Training #17 Div.2

URAL 2026

My Solution

贪心, 双端队列、队列

先扫一遍记录各种字母出现的次数, 然后在扫一遍字母数组(从大到小),依次记录没有出现过的字母

然后 扫一遍 分别记录奇数位置的'?' qo.push(i), 偶数位置的'?' qe.push(i) 同时如果'?'的个数 + 已经出现的种类数 < k 则 输出 -1否则就可以了, 然后每次 if(26 - deq.front() > deq.back()) 来判断应该填优先填 minus的位置还是 plus的位置,(同时应该先判断是否容器为空)

具体见代码

复杂度 O(n)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <deque>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 8;

char val[maxn];
int letter[26];

deque<int> deq;
queue<int> qe, qo;
int main()
{
#ifdef LOCAL
freopen("a.txt", "r", stdin);
//freopen("b.txt", "w", stdout);
int T = 5;
while(T--){
#endif // LOCAL
memset(letter, 0, sizeof letter);
int k, len, sz = 0, cnte = 0, cnto = 0;
scanf("%s", val);
len = strlen(val);
scanf("%d", &k);
for(int i = 0; i < len; i++){
if(val[i] != '?'){
letter[val[i] - 'a']++;
if(letter[val[i] - 'a'] == 1) sz++;
}
else{
if(i&1) qe.push(i);
else qo.push(i);
}
}
cnte = qe.size(), cnto = qo.size();
//cout<<cnte<<" "<<cnto<<endl;
if(sz + cnte + cnto < k) printf("-1");
else if(cnte + cnto == 0) printf("%s", val);
else{
//cout<<cnt<<endl;
for(int j = 26 - 1; j >= 0; j--){
if(letter[j] == 0) deq.push_back(j);
}
while(sz < k){
if(26 - deq.front() > deq.back()){
if(!qe.empty()){
val[qe.front()] = (deq.back() + 'a');
deq.pop_back();
qe.pop();
sz++;
}
else{
val[qo.front()] = (deq.front() + 'a');
deq.pop_front();
qo.pop();
sz++;
}
}
else{
if(!qo.empty()){
val[qo.front()] = (deq.front() + 'a');
deq.pop_front();
qo.pop();
sz++;
}
else{
val[qe.front()] = (deq.back() + 'a');
deq.pop_back();
qe.pop();
sz++;
}
}
}

while(!qo.empty()){
val[qo.front()] = 'z';
qo.pop();
}
while(!qe.empty()){
val[qe.front()] = 'a';
qe.pop();
}

printf("%s", val);

}
#ifdef LOCAL
printf("\n");
deq.clear();
while(!qe.empty()) qe.pop();
while(!qo.empty()) qo.pop();
}
#endif // LOCAL
return 0;
}


Thank you!

------from ProLights
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: