I don't really have this problem in Codeforces, where the rounds only last two hours, but I find that during longer contests (say, 4 hours), I tend to lose focus at the end. I start to think more about how I'm doing ("oh god, I'll never be able to solve this in time") than on solving. That means that I do almost nothing during the last hour or so.
Do you have any tips on how to work on this?
Here are a couple of tips: 1. Take a short 5-10 minute break during the middle of a long contest. You can take a stroll, drink some water, or eat something. 2. If you are stuck on solving a problem, try coding up a the best solution you have so far. Sometimes, coding a naive solution will give you ideas.
I'm not really sure how to stop thinking about performance, as sometimes I am worried as well. Best advice I can give you is to not worry so much and just enjoy solving the problems.
What you're describing sounds like ego depletion. If this is the case, you can try eating large amounts of sweets (or other food which contains easy-to-digest glucose) before/during the contest (see e.g. this). In my case, it helps a lot during longer contests, like Challenge-24.
I will definitely try eating large amounts of sweets ;)
You have no reason to feel guilty of eating too much now eh? :P
One of solutions for me is to listen music during contest to distract and get relaxed. Of course, in most cases it isn't a good idea for team contests:) And also you may not be a fan of music.
Possible way to build confidence is following trick — open standings, look at list of people/teams who solved given task, find most low-rated/low-skilled among them and say to yourself — those guys managed to solve it; of course, then I should be able to do it too, it should not be that hard.
And also it is a good idea to not think about your performance, like ed1d1a8d said:)