problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
acwing::acwing1913类 参考

AcWing 1913. 公平摄影 更多...

#include <acwing.h>

静态 Public 成员函数

static int main (istream &cin, ostream &cout)
 

详细描述

AcWing 1913. 公平摄影

在文件 acwing.h662 行定义.

成员函数说明

◆ main()

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

在文件 acwing.cpp1806 行定义.

1806 {
1807 unsigned int n;
1808 cin >> n;
1809 auto cows = map<unsigned int, int>();
1810 auto indexes = vector<unsigned int>();
1811 auto sum = unordered_map<int, set<unsigned int>>();
1812 for(unsigned int i = 0; i < n; i++) {
1813 unsigned int x;
1814 char b;
1815 cin >> x >> b;
1816 indexes.push_back(x);
1817 if(b == 'G') {
1818 cows.insert(pair(x, 1));
1819 } else {
1820 cows.insert(pair(x, -1));
1821 }
1822 }
1823 sort(indexes.begin(), indexes.end());
1824
1825 unsigned int g_count = 0;
1826 unsigned int h_count = 0;
1827 unsigned int g_max = 0;
1828 unsigned int h_max = 0;
1829 int g_start = -1;
1830 int h_start = -1;
1831 int count = 0;
1832 for(auto &cow: cows) {
1833 if(cow.second == 1) {
1834 //G
1835 h_count = 0;
1836 h_start = -1;
1837 if(g_start == -1) {
1838 g_start = cow.first;
1839 } else {
1840 g_count = cow.first - g_start;
1841 }
1842 g_max = max(g_max, g_count);
1843 } else {
1844 //H
1845 g_count = 0;
1846 g_start = -1;
1847 if(h_start == -1) {
1848 h_start = cow.first;
1849 } else {
1850 h_count = cow.first - h_start;
1851 }
1852 h_max = max(h_max, h_count);
1853 }
1854 count += cow.second;
1855 cow.second = count;
1856 if(!sum.contains(cow.second)) {
1857 auto s = set<unsigned int>();
1858 s.insert(cow.first);
1859 sum.insert(pair(cow.second, s));
1860 } else {
1861 sum[cow.second].insert(cow.first);
1862 }
1863 }
1864 unsigned int maximum = 0;
1865 for(const auto &i: sum) {
1866 if(i.first == 0) {
1867 maximum = max(maximum, *i.second.rbegin() - indexes[0]);
1868 } else if(i.second.size() >= 2) {
1869 maximum = max(maximum, *i.second.rbegin() - *upper_bound(indexes.begin(), indexes.end(), *i.second.begin()));
1870 }
1871 }
1872 cout << max(maximum, max(g_max, h_max));
1873 return 0;
1874 }

被这些函数引用 acwing::TEST().


该类的文档由以下文件生成: