Caisa is going to have a party and he needs to buy the ingredients for a big chocolate cake. For that he is going to the biggest supermarket in town.
Unfortunately, he has just s dollars for sugar. But that's not a reason to be sad, because there are n types of sugar in the supermarket, maybe he able to buy one. But that's not all. The supermarket has very unusual exchange politics: instead of cents the sellers give sweets to a buyer as a change. Of course, the number of given sweets always doesn't exceed 99, because each seller maximizes the number of dollars in the change (100 cents can be replaced with a dollar).
Caisa wants to buy only one type of sugar, also he wants to maximize the number of sweets in the change. What is the maximum number of sweets he can get? Note, that Caisa doesn't want to minimize the cost of the sugar, he only wants to get maximum number of sweets as change.
The first line contains two space-separated integers n, s (1 ≤ n, s ≤ 100).
The i-th of the next n lines contains two integers xi, yi (1 ≤ xi ≤ 100; 0 ≤ yi < 100), where xi represents the number of dollars and yi the number of cents needed in order to buy the i-th type of sugar.
Print a single integer representing the maximum number of sweets he can buy, or -1 if he can't buy any type of sugar.
5 10 3 90 12 0 9 70 5 50 7 0
50
5 5 10 10 20 20 30 30 40 40 50 50
-1
In the first test sample Caisa can buy the fourth type of sugar, in such a case he will take 50 sweets as a change.
对于A题没有难度,直接其实就是将所有钱数划归成为分,即总钱数等于xi*1·00+yi,然后就是(s*100-总钱数)%100,输出最大的即可AC,没有难度。。。也就不多说了,具体代码如下:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
int a[105],b[105],sum[105];
int ma(int a,int b)
{
if(a>b)return a;
return b;
}
int main()
{
// freopen("in.txt","r",stdin);
int n,s;
while(cin>>n>>s)
{
for(int i=0;i<n;i++)
{
cin>>a[i]>>b[i];
sum[i]=a[i]*100+b[i];
}
int maxx=0,flag=0;
for(int i=0;i<n;i++)
{
if(100*s>=sum[i])
{
int ti=100*s-sum[i];
maxx=ma(maxx,ti%100);
flag=1;
}
}
if(flag==0)
cout<<-1<<endl;
else
cout<<maxx<<endl;
}
return 0;
}
Caisa solved the problem with the sugar and now he is on the way back to home.
Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height hi. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k + 1). When the player have made such a move, its energy increases by hk - hk + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time.
Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 105) representing the heights of the pylons.
Print a single number representing the minimum number of dollars paid by Caisa.
5
3 4 3 2 4
4
3
4 4 4
4
In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon.
#include<cstdio>
int a[100005];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
a[0]=0;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int en=0;
long long res=0;
for(int i=1;i<=n;i++)
{
if(a[i]-a[i-1]>en)
{
res+=a[i]-a[i-1]-en;
en=0;
}
else
en+=(a[i-1]-a[i]);
}
printf("%I64d\n",res);
}
return 0;
}
C题:
Gargari is jealous that his friend Caisa won the game from the previous problem. He wants to prove that he is a genius.
He has a n × n chessboard. Each cell of the chessboard has a number written on it. Gargari wants to place two bishops on the chessboard in such a way that there is no cell that is attacked by both of them. Consider a cell with number x written on it, if this cell is attacked by one of the bishops Gargari will get x dollars for it. Tell Gargari, how to place bishops on the chessboard to get maximum amount of money.
We assume a cell is attacked by a bishop, if the cell is located on the same diagonal with the bishop (the cell, where the bishop is, also considered attacked by it).
The first line contains a single integer n (2 ≤ n ≤ 2000). Each of the next n lines contains n integers aij (0 ≤ aij ≤ 109) — description of the chessboard.
On the first line print the maximal number of dollars Gargari will get. On the next line print four integers: x1, y1, x2, y2 (1 ≤ x1, y1, x2, y2 ≤ n), where xi is the number of the row where the i-th bishop should be placed, yi is the number of the column where the i-th bishop should be placed. Consider rows are numbered from 1 to n from top to bottom, and columns are numbered from 1 to n from left to right.
If there are several optimal solutions, you can print any of them.
4
1 1 1 1
2 1 1 0
1 1 1 0
1 0 0 1
12
2 2 3 2
#include<cstdio>
#include<cstring>
int gg[2005][2005];
long long row[4005],col[4005];
const int ax=2000;
int main()
{
// freopen("in.txt","r",stdin);
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
scanf("%d",&gg[i][j]);
}
int x1=1,y1=1;
memset(row,0,sizeof(row));
memset(col,0,sizeof(col));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
row[i+j]+=gg[i][j];
col[i-j+ax]+=gg[i][j];
}
}
long long maxx1=0,maxx2=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(row[i+j]+col[i-j+ax]-gg[i][j]>=maxx1)
{
x1=i;
y1=j;
maxx1=row[i+j]+col[i-j+ax]-gg[i][j];
}
}
}
int pa=(x1+y1)%2;
int x2=1,y2=2;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(((i+j)%2!=pa)&&row[i+j]+col[i-j+ax]-gg[i][j]>=maxx2)
{
x2=i;
y2=j;
maxx2=row[i+j]+col[i-j+ax]-gg[i][j];
}
}
}
printf("%I64d\n",maxx1+maxx2);
printf("%d %d %d %d\n",x1,y1,x2,y2);
return 0;
}
D题:
Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari?
You can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem
The first line contains two integers n and k (1 ≤ n ≤ 1000; 2 ≤ k ≤ 5). Each of the next k lines contains integers 1, 2, ..., n in some order — description of the current permutation.
Print the length of the longest common subsequence.
4 3
1 4 2 3
4 1 2 3
1 2 4 3
3
The answer for the first test sample is subsequence [1, 2, 3].
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
int a[6][1005],dp[1005];
bool flag[6][1005][1005];
int main()
{
// freopen("in.txt","r",stdin);
int n,k;
while(cin>>n>>k)
{
for(int i=0;i<k;i++)
{
for(int j=0;j<n;j++)
cin>>a[i][j];
}
for(int i=0;i<k;i++)
{
for(int j=0;j<n;j++)
{
for(int p=j+1;p<n;p++)
{
flag[i][a[i][j]][a[i][p]]=true;
}
}
}
for(int i=0;i<n;i++)
dp[i]=1;
int maxx=-1;
for(int i=n-1;i>=0;i--)
{
for(int j=i+1;j<n;j++)
{
bool f=false;
for(int p=0;p<k;p++)
{
if(flag[p][a[0][i]][a[0][j]]==false)
{
f=true;
break;
}
}
if(f==true)
continue;
if(dp[i]<=dp[j])
dp[i]=dp[j]+1;
}
if(dp[i]>maxx)
maxx=dp[i];
}
cout<<maxx<<endl;
}
return 0;
}