Codeforces Round #451 (Div. 2)【A,B,C,D,E】【C题:模拟 D题:尺取+贪心 E题:思维+优先队列维护最值】

特判最后一位即可

 1 #include<bits/stdc++.h>
 2
 3 using namespace std;
 4 #define int long long
 5
 6 signed main(){
 7     int n;cin>>n;int t=n%10;
 8     if(t==0) cout<<n;
 9     else if(t>5) {
10         cout<<(n+10-t);
11     }
12     else {
13         cout<<(n-t);
14     }
15     return 0;
16 }

暴力就OK了

 1 #include<bits/stdc++.h>
 2
 3 using namespace std;
 4 #define int long long
 5
 6 signed main(){
 7     int n,a,b;
 8     cin>>n>>a>>b;
 9     for(int i=0;;i++){
10         if((n-i*a)%b==0&&(n-i*a)>=0){
11             cout<<"YES"<<‘\n‘;
12             cout<<i<<" "<<(n-i*a)/b;
13             return 0;
14         }
15         if(a*i>n){
16             break;
17         }
18     }
19     cout<<"NO";
20     return 0;
21 }

一道模拟题。

 1 #include<bits/stdc++.h>
 2
 3 using namespace std;
 4 #define int long long
 5 vector<string> v[66666];
 6 vector<string> ans[66666];
 7 map<string,int> vis;
 8 string st[66666];
 9 signed main(){
10     int n;
11     cin>>n;
12     int cnt=1;
13     for(int i=1;i<=n;i++){
14         string str;
15         cin>>str;
16         if(!vis[str]){
17             vis[str]=cnt;
18             st[cnt]=str;
19             cnt++;
20         }
21         int m;
22         cin>>m;
23         for(int j=0;j<m;j++){
24              string tle;
25              cin>>tle;
26              v[vis[str]].push_back(tle);
27         }
28     }
29     for(int i=1;i<cnt;i++){ // vector实现去重
30         sort(v[i].begin(), v[i].end());
31         v[i].erase(unique(v[i].begin(), v[i].end()), v[i].end());
32     }
33
34     //cout<<‘\n‘;
35     for(int i=1;i<cnt;i++){
36         map<string,int> mp;
37         for(int j=0;j<v[i].size();j++){
38             int f=0;
39             string temp=v[i][j];
40             int len=temp.size();
41             for(int k=0;k<v[i].size();k++){
42                 if(temp==v[i][k]) continue;
43                 if(v[i][k].size()>temp.size()){ //后缀
44                      if(temp==v[i][k].substr(v[i][k].size()-len,len)){
45                          f=1;
46                          break;
47                      }
48                 }
49             }
50             if(f){
51                 mp[temp]=1;
52             }
53         }
54         for(int j=0;j<v[i].size();j++){
55             if(!mp[v[i][j]])
56             ans[i].push_back(v[i][j]);
57         }
58     }
59     cout<<cnt-1<<‘\n‘;
60     for(int i=cnt-1;i>=1;i--){
61         cout<<st[i]<<" "<<ans[i].size()<<" ";
62         for(int j=0;j<ans[i].size();j++){
63             if(!j) cout<<ans[i][j];
64             else cout<<" "<<ans[i][j];
65         }
66         cout<<‘\n‘;
67     }
68     return 0;
69 }

 尺取+贪心

题意 : 给你n个闹钟,分别在aiai分钟响。要求在任意m分钟内,不能多余k个闹钟响,否则就会醒来,求关闭的最少的闹钟数

 1 #include<bits/stdc++.h>
 2
 3 using namespace std;
 4 #define maxn 666666
 5 #define int long long
 6 int arr[maxn];
 7 signed main(){
 8     int n,m,k;
 9     cin>>n>>m>>k;
10     for(int i=1;i<=n;i++) cin>>arr[i];
11     sort(arr+1,arr+1+n);
12     int ans=0;
13     deque<int> q;
14     for(int i=1;i<=n;i++){
15         while(!q.empty()&&arr[i]-q.front()>=m) q.pop_front();
16         if(q.size()>=k-1){
17             ans++;
18         }else{
19             q.push_back(arr[i]);
20         }
21
22     }
23     cout<<ans;
24     return 0;
25 }

 题意:给你n个数,现在你可以进行一系列操作,每次操作可以对任意一个数加一或者减一,使得进行操作后n个数中恰好有一半是平方数还有一半是非平方数。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define int long long
 4 priority_queue<int,vector<int>, greater<int> > q1;//平方数
 5 priority_queue<int,vector<int>, greater<int> > q2;//非平方数
 6 signed main(){
 7     int n;
 8     cin>>n;
 9     int sum1=0;//平方数
10     int sum2=0;//非平方数
11     for(int i=1;i<=n;i++){
12         int t;cin>>t;
13         int cnt=(int)sqrt(t*1.0);
14         if(cnt*cnt==t){
15             sum1++;
16             if(t) q1.push(1);
17             else q1.push(2);
18         }else{
19             sum2++;
20             int temp=min((cnt+1)*(cnt+1)-t,t-cnt*cnt);
21             q2.push(temp);
22         }
23     }
24     if(sum1==sum2&&sum1==n/2){
25         cout<<"0";
26         return 0;
27     }
28     if(sum1>n/2){
29         int sum=0;
30         int _=abs(sum1-n/2);
31         while(_--){
32             sum+=q1.top();
33             q2.pop();
34         }
35         cout<<sum;
36         return 0;
37     }
38     int add=0;
39         int _=abs(sum2-n/2);
40         while(_--){
41             add+=q2.top();
42             q2.pop();
43         }
44         cout<<add;
45     return 0;
46 }

原文地址:https://www.cnblogs.com/pengge666/p/12199273.html

时间: 2024-08-29 04:29:52

Codeforces Round #451 (Div. 2)【A,B,C,D,E】【C题:模拟 D题:尺取+贪心 E题:思维+优先队列维护最值】的相关文章

Codeforces Round #451 (Div. 2) ABC

A. Rounding Vasya has a non-negative integer n. He wants to round it to nearest integer, which ends up with 0. If n already ends up with 0, Vasya considers it already rounded. For example, if n = 4722 answer is 4720. If n = 5 Vasya can round it to 0 

【Codeforces Round #451 (Div. 2) D】Alarm Clock

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 尺取法+二分. 类似滑动窗口. 即左端点为l,右端点为r. 维护a[r]-a[l]+1总是小于等于m的就好. (大于m就右移左端点) 然后看看里面的数字个数是不是小于k; 不是的话让l..r中最右边那个数字删掉就好. ->链表优化一下即可. [代码] /* 1.Shoud it use long long ? 2.Have you ever test several sample(at least therr) yourself

Codeforces Round #451 (Div. 2) F Restoring the Expression

题意: 有一个a+b=c的等式,去掉两个符号,把三个数连在一起得到一个数 给出这个数,要求还原等式,length <= 1e6 三个数不能含有前导0,保证有解 解法: 铁头过题法,分类然后各种判断 我分了5种情况 0.开头字符为0, 那么结果一定是0+a=a的形式 然后4种情况 1.len(a) >= len(b) 且 len(c) == len(a) 2.len(a) <= len(b) 且 len(c) == len(b) 3.len(a) >= len(b) 且 len(c)

Codeforces Round #451 Div. 2 C D E

C.Phone Numbers 之前没有做过字典树--感觉这个题字典树也能做--就拿来练一练字典树--板子好多地方写的都不够好,还需要继续改-- emmm这个--卡了好久啊--不过好在还是debug出来了--orz 虽然是处理后缀的问题,但是只需要reverse一下就可以变成前缀问题了2333333,然后每一次的字符串都插进去,最后从叶子到根遍历输出就可以了-- 不过我好像--连叶子到根的遍历都写不好呜呜呜--ps:这里只是为了用trie而用trie--其实用map维护一下直接暴力求解就可以--

Codeforces Round #451 (Div. 2) E

E. Squares and not squares Ann and Borya have n piles with candies and n is even number. There are ai candies in pile with number i. Ann likes numbers which are square of some integer and Borya doesn't like numbers which are square of any integer. Du

Codeforces Round #558 (Div. 2)-Cat Party (Hard Edition)-(前缀和 + 模拟)

http://codeforces.com/problemset/problem/1163/B2 题意:有n天,每天有一个颜色,截取前x天,随便抽掉一天,使剩下的各个颜色出现的次数相等. 解题,也可以解决B1: 有三种情况: 1.一种颜色出项一次,其他相等,抽掉出现1次颜色的那一天,例如13天分别是 6 222 333 444 555 2.只有两种颜色次数,次数相差1,并且最大出现次数的颜色只有1次,例如13天分别是 777 333 222 8888 3.所有颜色都只出现过1次,例如 1 2 3

Codeforces Round #250 (Div. 1) B. The Child and Zoo(排序+并查集)(常规好题)

B. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The i-th area contains 

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟

E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express

# Codeforces Round #529(Div.3)个人题解

Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Repeating Cipher 传送门 题意:第一个字母写一次,第二个字母写两次,依次递推,求原字符串是什么 题解:1.2.3.4,非常明显的d=1的等差数列,所以预处理一个等差数列直接取等差数列的每一项即可 代码: #include<bits/stdc++.h> using namespace s