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
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)
Thank you!
------from ProLights
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
input | output |
---|---|
?? 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
相关文章推荐
- 【双端队列】【栈】【队列】STL之deque、stack、queue、容器适配器
- 队列Queue、双端队列Deque
- 队列 Queue 与双端队列 Deque
- C++ deque(double-ended-queue)双端队列
- 队列Queue、双端队列Deque
- java队列Queue、双端队列Deque
- C++标准库__std::deque(双端队列),std::queue(队列),std::stack(栈)__由stl的定义我们就可以看出 queue和stack都是基于deque实现的和常用接口
- Java队列Queue、双端队列Deque
- 队列Queue、双端队列Deque
- Python 内置数据结构 —— queue(队列)、deque(双端队列)
- 20170728_双端队列deque的常用函数
- c++ deque 双端队列
- STL--双端队列(deque)和链表(list)
- 深入java集合系列:双端队列deque
- 【C++ 与 STL】双端队列:deque
- 双端队列--Deque 容器
- deque 双端队列容器
- uva 11988 - Broken Keyboard (a.k.a. Beiju Text)(双端队列deque)
- STL系列之deque双端队列+vector向量容器
- ural 1306. Sequence Median(优先级队列 priority_queue用法)