Zoj3519 bit1004 who is the smartest man

Source: Internet
Author: User

Question:

Cao wants to be the smartest person. The first line of data is the number of his opponents, and the second is his IP address.

Then list the IP addresses of all opponents. If Cao wins a person higher than his IP address, his IP address will be added to 2; otherwise, the IP address will be added to 1.

And if Cao wins, ask Cao to win the highest IP Address

Solution:

The greedy idea is very simple. Sort the IP addresses of each competitor, and find the smallest IP address among the people whose IP address is higher than him.

In the end, there is no higher than his IP address, and the rest of the people will be defeated one by one, A for loop will be done

# Include <iostream> # include <algorithm> # include <cstdio> using namespace STD; int A [550]; int N, IP; int counter; int main () {While (~ Scanf ("% d", & N, & IP) {counter = 0; For (INT I = 0; I <n; I ++) {scanf ("% d", & A [I]) ;}sort (A, A + n); For (INT I = 0; I <n; I ++) {if (a [I]> ip) {counter ++; IP + = 2 ;}} printf ("% d \ n", IP + N-counter );} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.