#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
struct gunman
{
int accuracy ;
bool alive ;
int position ;
};
std::ostream& operator<< ( std::ostream& stm, const gunman& g )
{
return stm << "{ " << "accuracy:" << g.accuracy << ", position:" << g.position
<< ", alive:" << std::boolalpha << g.alive << " }" ;
}
int main()
{
std::vector<gunman> gmen { {7,true,0}, {0,false,1}, {3,true,2}, {5,true,3},
{2,false,4}, {4,true,5}, {0,false,6}, {7,true,7} } ;
for( const auto& g : gmen ) std::cout << g << '\n' ;
std::cout << "-----------------\n" ;
const auto cmp1 = [] ( const gunman &lhs , const gunman &rhs )
{
if( lhs.alive && rhs.alive ) return lhs.accuracy < rhs.accuracy ;
else return lhs.alive ;
};
auto position1 = std::min_element( gmen.begin() , gmen.end() , cmp1 )->position ;
std::cout << "position of least accurate and alive: " << position1 << '\n' ;
const auto cmp2 = [position1,cmp1] ( const gunman &lhs , const gunman &rhs )
{ return cmp1(lhs,rhs) && ( lhs.position != position1 ) ; };
auto position2 = std::min_element( gmen.begin() , gmen.end() , cmp2 )->position ;
std::cout << "position of second-least accurate and alive: " << position2 << '\n' ;
std::cout << "-----------------\n" ;
std::vector< std::reference_wrapper<gunman> > tvec( gmen.begin() , gmen.end() ) ;
std::sort( tvec.begin(), tvec.end(), cmp1 ) ;
for( const auto& g : tvec ) std::cout << g << '\n' ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KCnN0cnVjdCBndW5tYW4KewogICBpbnQgYWNjdXJhY3kgOwogICBib29sIGFsaXZlIDsKICAgaW50IHBvc2l0aW9uIDsKfTsKCnN0ZDo6b3N0cmVhbSYgb3BlcmF0b3I8PCAoIHN0ZDo6b3N0cmVhbSYgc3RtLCBjb25zdCBndW5tYW4mIGcgKQp7CiAgICByZXR1cm4gc3RtIDw8ICJ7ICIgPDwgImFjY3VyYWN5O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ICwgY29uc3QgZ3VubWFuICZyaHMgKQogICAgICAgICAgICAgICAgICAgICAgIHsgcmV0dXJuIGNtcDEobGhzLHJocykgJiYgKCBsaHMucG9zaXRpb24gIT0gcG9zaXRpb24xICkgOyAgfTsKICAgIGF1dG8gcG9zaXRpb24yID0gc3RkOjptaW5fZWxlbWVudCggZ21lbi5iZWdpbigpICwgZ21lbi5lbmQoKSAsIGNtcDIgKS0+cG9zaXRpb24gOwogICAgc3RkOjpjb3V0IDw8ICJwb3NpdGlvbiBvZiBzZWNvbmQtbGVhc3QgYWNjdXJhdGUgYW5kIGFsaXZlOiAiIDw8ICBwb3NpdGlvbjIgPDwgJ1xuJyA7CgogICAgc3RkOjpjb3V0IDw8ICItLS0tLS0tLS0tLS0tLS0tLVxuIiA7CgogICAgc3RkOjp2ZWN0b3I8IHN0ZDo6cmVmZXJlbmNlX3dyYXBwZXI8Z3VubWFuPiA+IHR2ZWMoIGdtZW4uYmVnaW4oKSAsIGdtZW4uZW5kKCkgKSA7CiAgICBzdGQ6OnNvcnQoIHR2ZWMuYmVnaW4oKSwgdHZlYy5lbmQoKSwgY21wMSApIDsKICAgIGZvciggY29uc3QgYXV0byYgZyA6IHR2ZWMgKSBzdGQ6OmNvdXQgPDwgZyA8PCAnXG4nIDsKfQo=
{ accuracy:7, position:0, alive:true }
{ accuracy:0, position:1, alive:false }
{ accuracy:3, position:2, alive:true }
{ accuracy:5, position:3, alive:true }
{ accuracy:2, position:4, alive:false }
{ accuracy:4, position:5, alive:true }
{ accuracy:0, position:6, alive:false }
{ accuracy:7, position:7, alive:true }
-----------------
position of least accurate and alive: 2
position of second-least accurate and alive: 5
-----------------
{ accuracy:3, position:2, alive:true }
{ accuracy:4, position:5, alive:true }
{ accuracy:5, position:3, alive:true }
{ accuracy:7, position:0, alive:true }
{ accuracy:7, position:7, alive:true }
{ accuracy:0, position:1, alive:false }
{ accuracy:2, position:4, alive:false }
{ accuracy:0, position:6, alive:false }