Coding
  • Welcome
  • Baisc Knowledge
    • Vim
    • C
      • Library
    • Java
      • Setup
      • Java Basic
  • Kattis
    • Easy
      • A Second Opinion
      • A Shortcut to What?
      • A Stack of Gold
      • ACM Contest Scoring
      • ASCII kassi
      • Aaah!
      • Add Two Numbers
      • Adding Trouble
      • Afjörmun
      • Airfare Grants
      • Above Average
      • Akcija
      • Alphabet Spam
      • Amerískur vinnustaður
      • Anti-Palindrome
      • Apaxiaaaaaaaaaaaans!
      • Arithmetic Functions
      • Arm Coordination
      • Arrangement
      • Attendance
      • Autori
      • Average Character
      • Avion
      • Baby Bites
      • Babylonian Numbers
      • ABC
      • Aldur
      • Backspace
      • Bannorð
      • Barcelona
      • Basketball One-on-One
      • Batter Up
      • Beavergnaw
      • Bela
      • BergMál
      • Bergur*
      • Akureyri*
      • Best Compromise
      • Best Relay Team*
      • Besta gjöfin
      • Betting
      • Bijele
      • Bilað Lyklaborð
      • Bitte ein Bit
      • Blandað Best
      • Blaðra
      • Blaðra2
      • Bluetooth*
      • Booking a Room
      • Bottle Opening
      • Bounding Robots
      • Breaking Branches*
      • Bracket Matching*
      • Broken Swords
      • Building Pyramids
      • Bus
      • Bus Assignment
      • CPR Number
      • Call for Problems
      • Canadians, eh?
      • Candy Store
      • Cetiri
      • Cetvrta
      • Champernowne Verification
      • Chanukah Challenge
      • Chardonnay
      • Chocolate Division*
      • Chugging
      • Cinema Crowds 2
      • Class Field Trip
      • ASCII Kassi 2
      • Coffee Cup Combo
      • Cold-puter Science
      • Composed Rhythms
      • Cookies
      • Cooking Water
      • Cornhusker
      • Cosmic Path Optimization
      • Count the Vowels
Powered by GitBook
On this page
  • Question
  • Solution
  • Idea
  • Code
Edit on GitHub
  1. Kattis
  2. Easy

Airfare Grants

PreviousAfjörmunNextAbove Average

Last updated 6 months ago

Question

Solution

Idea

Code

The behind idea of this problem is . Since the list is unsorted, we can only use linear search to find the maximum and minimum. After that, the logic is simple.

https://open.kattis.com/problems/airfaregrantsopen.kattis.com
#linear-search
https://github.com/mendax1234/Coding-Problems/blob/main/kattis/airfaregrants/airfaregrants.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

long read_long()
{
  long a;
  char buf[1024]; // use 1KiB just to be sure
  int success; // flag for successful conversion

  do
  {
    if (!fgets(buf, 1024, stdin))
    {
        // reading input failed:
        return 1;
    }

    // have some input, convert it to integer:
    char *endptr;

    errno = 0; // reset error number
    a = strtol(buf, &endptr, 10);
    if (errno == ERANGE)
    {
        printf("Sorry, this number is too small or too large.\n");
        success = 0;
    }
    else if (endptr == buf)
    {
        // no character was read
        success = 0;
    }
    else if (*endptr && *endptr != '\n')
    {
        // *endptr is neither end of string nor newline,
        // so we didn't convert the *whole* input
        success = 0;
    }
    else
    {
        success = 1;
    }
  } while (!success); // repeat until we got a valid number
  
  return a;
}

long find_min(long *list, long n)
{
  long min = list[0];
  for (long i = 1; i < n; i += 1)
  {
    if (list[i] < min)
    {
      min = list[i];
    }
  }
  return min;
}

long find_max(long *list, long n)
{
  long max = list[0];
  for (long i = 0; i < n; i += 1)
  {
    if (list[i] > max)
    {
      max = list[i];
    }
  }
  return max;
}

int main()
{
  long n;
  scanf("%ld\n", &n);
  long *list = calloc((size_t)n, sizeof(long));
  if (list == NULL)
  {
    return 1;
  }
  for (long i = 0; i < n; i += 1)
  {
    list[i] = read_long();
  }
  long min = find_min(list, n);
  long max = find_max(list, n) / 2;
  if (min <= max)
  {
    printf("%ld\n", (long)0);
  }
  else
  {
    printf("%ld\n", min - max);
  }
}