problemscpp
A collection of my answers to algorithm problems in c++.
函数
acwing::acwing1659 命名空间参考

  1. 社交距离 I
更多...

函数

int main (istream &cin, ostream &cout)
 
 TEST (acwing1659, case1)
 

详细描述

  1. 社交距离 I

函数说明

◆ main()

int acwing::acwing1659::main ( istream &  cin,
ostream &  cout 
)

在文件 acwing.cpp5661 行定义.

5661 {
5662 int n;
5663 cin >> n;
5664 vector<bool> pen(n);
5665 string str;
5666 cin >> str;
5667 bool has1 = false;
5668 for(int i = 0; i < n; i++) {
5669 pen[i] = str[i] == '1';
5670 if(pen[i]) {
5671 has1 = true;
5672 }
5673 }
5674 if(!has1) {
5675 cout << n - 1;
5676 return 0;
5677 }
5678 vector<int> spaces2;
5679 vector<int> spaces1;
5680 int current = 0;
5681 int left = 0;
5682 int right = n - 1;
5683 while(!pen[left]) {
5684 left++;
5685 }
5686 if(left > 0) {
5687 spaces1.push_back(left);
5688 }
5689 left++;
5690 while(!pen[right]) {
5691 right--;
5692 }
5693 if(right < n - 1) {
5694 spaces1.push_back(n - 1 - right);
5695 }
5696 for(; left <= right; left++) {
5697 if(!pen[left]) {
5698 current++;
5699 } else {
5700 spaces2.push_back(current);
5701 current = 0;
5702 }
5703 }
5704 int minimum = 0;
5705 int limit = n;
5706 sort(spaces2.begin(), spaces2.end());
5707 sort(spaces1.begin(), spaces1.end());
5708 if(!spaces2.empty()) {
5709 limit = *spaces2.begin();
5710 //两个都在2
5711 //两个都在2的最大
5712 const int a = (*spaces2.rbegin() - 2) / 3;
5713 minimum = max(minimum, a);
5714 //两个分别在2的最大和第二大
5715 if(spaces2.size() > 1) {
5716 const int b = (spaces2[spaces2.size() - 2] - 1) / 2;
5717 minimum = max(minimum, b);
5718 }
5719 //两个分别在2的最大和1的最大
5720 if(!spaces1.empty()) {
5721 const int a = (*spaces2.rbegin() - 1) / 2;
5722 int b = *spaces1.rbegin() - 1;
5723 minimum = max(minimum, min(a, b));
5724 //两个都在1的最大
5725 b = (*spaces1.rbegin() - 2) / 2;
5726 minimum = max(minimum, b);
5727 //两个分别在1的最大和第二大
5728 if(spaces1.size() > 1) {
5729 const int a = *spaces1.begin() - 1;
5730 minimum = max(minimum, a);
5731 }
5732 }
5733 } else {
5734 //两个分别在1的最大和第二大
5735 if(spaces1.size() > 1) {
5736 const int a = *spaces1.begin() - 1;
5737 minimum = max(minimum, a);
5738 }
5739 //两个都在1的最大
5740 const int b = (*spaces1.rbegin() - 2) / 2;
5741 minimum = max(minimum, b);
5742 }
5743 minimum = min(limit, minimum);
5744 cout << minimum + 1;
5745 return 0;
5746 }

被这些函数引用 TEST().

◆ TEST()

acwing::acwing1659::TEST ( acwing1659  ,
case1   
)

在文件 acwing_test.cpp2722 行定义.

2722 {
2723 istringstream in("14\n10001001000010");
2724 auto out = ostringstream();
2725 main(in, out);
2726 const auto ans = out.str();
2727 ASSERT_EQ("2", ans);
2728 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().