I have a StationInfo class with properties

public uint EnergyConsumption { get; private set; } public uint CurrentEnergyAmount { get; private set; } public uint NumRobotsNearStation { get; private set; } public uint NumStationsAround { get; private set; } 

I create an array of type StationInfo .

I need to find in this array such a StationInfo object that satisfies this condition at the same time:

 EnergyConsumption -> min CurrentEnergyAmount -> max NumRobotsNearStation -> max NumStationsAround -> min 

An example with 2 arguments:

 {2,1} {3,4} {6,7} {1,10} {2, 15} 

need to find among the objects object with its properties {min, max}

There are no objects with such conditions, but there is an object close to the condition - {2, 15}

Is there any ready-made tool for sharps, and then there is no desire to waste time writing an algorithm.

  • Uh ... Specify the task. What if the minimum EnergyConsumption and maximum CurrentEnergyAmount fall on different elements? - VladD
  • Hmm, it's still not clear. And what is {2, 15} better than {1, 10} ? What is the exact criterion? - VladD
  • the difference between min and max is big - nullptr

3 answers 3

If I understand the condition correctly, you need to minimize the following value:

 EnergyConsumption - CurrentEnergyAmount - NumRobotsNearStation + NumStationsAround 

If so, it can be solved in an obvious way:

 static int Evaluate(StationInfo info) { return info.EnergyConsumption - info.CurrentEnergyAmount - info.NumRobotsNearStation + info.NumStationsAround; } var best = stations.OrderBy(Evaluate).FirstOrDefault(); 
  • Tested - works great! Exactly what is needed. True, I don’t understand how this thing works: return info.EnergyConsumption - info.CurrentEnergyAmount - info.NumRobotsNearStation + info.NumStationsAround; Please explain - nullptr
  • Well, this is such a value, which is smaller, the smaller the EnergyConsumption , and the smaller, the more CurrentEnergyAmount , etc. And if the EnergyConsumption much smaller, and the CurrentEnergyAmount not much more, then the result is still much smaller. From these considerations, it was chosen. - VladD
  • Is this purely logical you thought of, or did you use ready-made knowledge from mathematics? A year ago I came across this (there were two elements in the object, as in the example), so I rolled up lines like this 25 for this solution (first I sorted at least, then I looked for the maximum, then less for the maximum, and so on, in general so I do not remember). - nullptr
  • @nullptr: Well, this is more knowledge from mathematics than logic. For logical thinking today is too late :-) - VladD

You can use sorting, but I don’t dare to say about the speed of work:

 var result = (from station in stations orderby station.EnergyConsumption ascending, station.CurrentEnergyAmount descending, station.NumRobotsNearStation descending, station.NumStationsAround ascending select station).FirstOrDefault(); 
  • one
    If sorting is lazy, it will be quick, in theory. - VladD
  • No, this thing with the linq request. I have tested it on several important data - it gives the wrong results - nullptr
  • 2
    @nullptr: It all depends on the problem statement. Initially, you did not give all the necessary details. I think the answer @VladD is suitable. - Pavel Azanov
  • Pavel Azanov, thank you. Your linq also came in handy to me - nullptr

There is, and far from one. The best for this method would be, in my opinion, the LINQ construction FROM-> SELECT-> WHERE