Cain, who is hitting .344 since being moved into the leadoff spot on June 22, went 4 for 4 with a walk, a triple and a stolen base. He got into scoring position five times, but scored only twice.
"I'm just doing what I can to get on base, and if the guys happen not to drive me in, it happens sometimes," Cain said. "I'm just going to continue to do what I can to get on base."
The Rays opened the sixth with three consecutive hits, including a two-run single by Longoria off Jason Vargas (8-4), to take a 2-1 lead. Vargas, who allowed two runs and six hits in 5 2-3 innings, was coming off seven innings in the Royals' 4-0 win Wednesday against Minnesota.
Brandon Guyer's double, which preceded Longoria's single, was the most damaging hit, according to Vargas.
"I just got a changeup right in the middle of the zone, and that put them in a position to go ahead in the game with their best hitter coming up to the plate," Vargas said.
"You throw good ones and bad ones; that was a bad one. It was a lazy one, and it set them up to have an inning that changed the game for them."
Hellickson, coming back after arthroscopic surgery on his right elbow in January, gave up one run and six hits.
James Loney had an RBI double and Logan Forsythe hit a sacrifice fly as Tampa Bay took a 4-1 lead in the eighth. The Rays were aided when Royals shortstop Alcides Escobar fielded Longoria's grounder with no outs, but was beaten to second base by Guyer, who had led off with a bunt single.
"I think (Escobar) thought he was closer to the bag," explained Royals manager Ned Yost. "Flip it to Omar (Infante) and you get one. He was taking a shot at getting two, but he was a step father than he needed to be on that play."
Salvador Perez had three RBIs, including a two-run single off Jake McGee in the ninth that pulled the Royals within 4-3. Kansas City had 13 hits and got runners in scoring position in six innings. The Royals went 1 for 10 with runners in scoring position through eight innings.
McGee, the fourth Tampa Bay reliever, went the final 1 1-3 innings for his sixth save. The left-hander entered with two on and two outs in the eighth, and got a flyball from pinch-hitter Danny Valencia.
Brad Boxberger (2-1) replaced Hellickson in the fifth with runners on first and third with one out and gave up an RBI grounder to Perez that put Kansas City ahead 1-0.
NOTES: The Royals signed LHP Joe Saunders to a minor league deal and will assign him to Triple-A Omaha. ... Rays manager Joe Maddon said OF David DeJesus (broken left hand) should be back by early August, while OF Wil Myers (broken right wrist) could return in mid-August. ... Yost said OF Nori Aoki (strained left groin) could return just before or right after the All-Star break. ... Tampa Bay SS Yunel Escobar (shoulder) started a rehab assignment with Single-A Charlotte. ... Royals RHP Yordano Ventura (6-7) and Rays RHP Alex Cobb (4-6) are the scheduled starters for the series finale Wednesday night.