Codeforces Round #436 (Div. 2)

A. Fair Game

題目連接:http://codeforces.com/contest/864/problem/Aios

題目意思:Petya和Vasya 要分別從選擇兩種不一樣的數字,而後把給出數列中的這兩種數字的卡片分別都取走,要求取完後卡片所有被取完,並且雙方取走卡片的數量是相同的。c++

題目思路:很簡單,排個序,判斷前n個是否是同一個數字,後n個是否是另外的n個相同的數字。ide

代碼:spa

 1 //Author: xiaowuga
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 #define inf 0x3f3f3f3f
 5 #define MAX INT_MAX
 6 #define mem(s,ch) memset(s,ch,sizeof(s))
 7 const long long N=100000; 
 8 const long long mod=1e9+7; 
 9 typedef long long LL;
10 typedef int II;
11 typedef unsigned long long ull;
12 #define nc cout<<"nc"<<endl
13 #define endl "\n"
14 int main() {
15     ios::sync_with_stdio(false);cin.tie(0);
16     II a[120]={0};
17     II n;
18     cin>>n;
19     for(II i=1;i<=n;i++){
20         cin>>a[i];
21     }
22     sort(a+1,a+n+1);
23     if(a[n/2]!=a[n/2+1]&&a[n/2]==a[1]&&a[n/2+1]==a[n]){
24         cout<<"YES"<<endl;
25         cout<<a[1]<<" "<<a[n]<<endl;
26     } 
27     else cout<<"NO"<<endl;
28     return 0;
29 }
View Code

B. Polycarp and Letters

題目連接:http://codeforces.com/contest/864/problem/Bcode

題目意思:判斷兩個大寫字母之間小寫字母種類的的數量的最大值,注意開始和結尾和第一個大寫字母和最後一個大寫字母之間的也算。blog

題目思路:暴力掃一遍。ci

代碼:get

 1 //Author: xiaowuga
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 #define inf 0x3f3f3f3f
 5 #define MAX INT_MAX
 6 #define mem(s,ch) memset(s,ch,sizeof(s))
 7 const long long N=100000; 
 8 const long long mod=1e9+7; 
 9 typedef long long LL;
10 typedef int II;
11 typedef unsigned long long ull;
12 #define nc cout<<"nc"<<endl
13 #define endl "\n"
14 int main() {
15     ios::sync_with_stdio(false);cin.tie(0);
16     II n;
17     cin>>n;
18     char s[500];
19     cin>>(s+1);
20     II h[30];
21     mem(h,0);
22     II ct=0;
23     II ans=0;
24     for(II i=1;i<=n;i++){
25         if(s[i]>='A'&&s[i]<='Z'){
26             ans=max(ans,ct);
27             mem(h,0);
28             ct=0;
29         }
30         else{
31             II v=s[i]-'a';
32             if(h[v]==0) {h[v]=1,ct++;}
33         }
34     }
35     ans=max(ans,ct);
36     cout<<ans<<endl;
37     return 0;
38 }
View Code

C. Bus

題目連接:http://codeforces.com/contest/864/problem/Cit

題目意思:在一個一維直線上有一個起點爲0,終點爲a,距離爲a,車有b單位的燃料,如今記錄0-a或者a-0算是完成一次旅程,如今要完成k次旅程,在f處有一個加油站,如今問完成旅程最少須要多少加多少次油。io

題目思路:首先咱們須要假想把起點移到加油站,而後把初始油量變成b-f,而後每次從f出發到a再回到f須要2*(a-f)的汽油,從f出發到0再回到f須要2*f的汽油,要進行k次旅程就須要通過f這個加油站k次,因爲咱們把起點移到了加油站,因此只須要途徑k-1次了,而後最後在回到0或者a就行了(根據k的值),因此而後直接看代碼吧!

代碼:

 1 //Author: xiaowuga
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 #define inf 0x3f3f3f3f
 5 #define MAX INT_MAX
 6 #define mem(s,ch) memset(s,ch,sizeof(s))
 7 const long long N=100000; 
 8 const long long mod=1e9+7; 
 9 typedef long long LL;
10 typedef int II;
11 typedef unsigned long long ull;
12 #define nc cout<<"nc"<<endl
13 #define endl "\n"
14 int main() {
15     ios::sync_with_stdio(false);cin.tie(0);
16     II a,b,f,k;
17     cin>>a>>b>>f>>k;
18     II c=b-f;
19     II ans=0;
20     if(c<0) {cout<<-1<<endl; return 0;}
21     for(II i=1;i<k;i++){
22         II d=(i&1)?a-f:f;
23         if(c<2*d){
24             c=b;
25             ans++;
26         } 
27         if(c<2*d){
28             cout<<-1<<endl; return 0;
29         }
30         c-=2*d;
31     }
32     II x;
33     if(k&1) x=a-f;else x=f;
34     if(c<x){
35         ans++;
36         c=b;
37     } 
38     if(c<x) {cout<<-1<<endl; return 0;}
39     cout<<ans<<endl;
40     return 0;
41 }
View Code

D. Make a Permutation!

題目連接:http://codeforces.com/contest/864/problem/D

題目連接:給出一個數列,其中的一些數多是會重複,而後要用最少的步數使其變成一個1-n的排列,並且字典序還要是最小的。

題目思路:首先須要改變的數的數量要最少,一定是不存在的數的數量,而後咱們要找出這些數都有哪些,而後把他們從小到大,依次,填進去。具體看代碼吧!

代碼:

 1 //Author: xiaowuga
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 #define inf 0x3f3f3f3f
 5 #define MAX INT_MAX
 6 #define mem(s,ch) memset(s,ch,sizeof(s))
 7 const long long N=100000; 
 8 const long long mod=1e9+7; 
 9 typedef long long LL;
10 typedef int II;
11 typedef unsigned long long ull;
12 #define nc cout<<"nc"<<endl
13 #define endl "\n"
14 II a[200000+10];
15 II b[200000+10];
16 II c[200000+10];
17 int main() {
18     ios::sync_with_stdio(false);cin.tie(0);
19     II n;
20     cin>>n;
21     mem(b,0);
22     mem(c,0);
23     for(II i=1;i<=n;i++){
24         II t;
25         cin>>t;
26         a[i]=t;
27         b[t]++;
28     }
29     vector<int>q;
30     for(II i=1;i<=n;i++){
31         if(b[i]==0) q.push_back(i);
32     }
33     II k=0;
34     LL ans=0;
35     for(II i=1;i<=n;i++){
36         II v=a[i];
37         if(b[v]>1){
38             if(c[v]==0&&a[i]<q[k]){
39                 c[v]=1;
40             }
41             else{
42                 ans++;
43                 b[v]--;
44                 a[i]=q[k];
45                 k++; 
46             }
47         }
48     }
49     cout<<ans<<endl;
50     for(II i=1;i<=n;i++) cout<<a[i]<<' ';cout<<endl;
51     return 0;
52 }
View Code
相關文章
相關標籤/搜索