c407(A,B)408(A,B)
Little Vasya went to the supermarket to get some groceries. He walked about the supermarket for a long time and got a basket full of products. Now he needs to choose the cashier to pay for the products.
There are n cashiers at the exit from the supermarket. At the moment the queue for the i-th cashier already has ki people. The j-th person standing in the queue to the i-th cashier has mi, j items in the basket. Vasya knows that:
- the cashier needs 5 seconds to scan one item;
- after the cashier scans each item of some customer, he needs 15 seconds to take the customer's money and give him the change.
Of course, Vasya wants to select a queue so that he can leave the supermarket as soon as possible. Help him write a program that displays the minimum number of seconds after which Vasya can get to one of the cashiers.
The first line contains integer n (1 ≤ n ≤ 100) — the number of cashes in the shop. The second line contains n space-separated integers: k1, k2, ..., kn (1 ≤ ki ≤ 100), where ki is the number of people in the queue to the i-th cashier.
The i-th of the next n lines contains ki space-separated integers: mi, 1, mi, 2, ..., mi, ki (1 ≤ mi, j ≤ 100) — the number of products the j-th person in the queue for the i-th cash has.
Print a single integer — the minimum number of seconds Vasya needs to get to the cashier.
1 1 1
20
4 1 4 3 2 100 1 2 2 3 1 9 1 7 8
100
In the second test sample, if Vasya goes to the first queue, he gets to the cashier in 100·5 + 15 = 515 seconds. But if he chooses the second queue, he will need 1·5 + 2·5 + 2·5 + 3·5 + 4·15 = 100 seconds. He will need 1·5 + 9·5 + 1·5 + 3·15 = 100 seconds for the third one and 7·5 + 8·5 + 2·15 = 105 seconds for the fourth one. Thus, Vasya gets to the cashier quicker if he chooses the second or the third queue.
#include <iostream> #include <stdio.h> #include <stdlib.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; typedef long long ll; int a[1010][1010]; int main() { int n; cin>>n; for(int i=0;i<n;i++) cin>>a[i][0]; for(int i=0;i<n;i++) for(int j=1;j<=a[i][0];j++) cin>>a[i][j]; int min0; for(int i=0;i<n;i++) { int s=15*a[i][0]; for(int j=1;j<=a[i][0];j++) s+=a[i][j]*5; if(i==0)min0=s; else if(min0>s)min0=s; //cout<<s<<endl; }cout<<min0<<endl; return 0; }
Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter.
The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland.
Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of m pieces of paper in the garland. Calculate what the maximum total area of the pieces of paper in the garland Vasya can get.
The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters ("a"..."z"). Each letter means that Vasya has a sheet of paper of the corresponding color.
The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.
Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.
aaabbac aabbccac
6
a z
-1
In the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color aand cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6.
In the second test sample Vasya cannot make a garland at all — he doesn't have a sheet of color z.
#include <iostream> #include <stdio.h> #include <stdlib.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; typedef long long ll; char a[1010],b[1010]; int x1[26]={0},x2[26]={0}; int main() { cin>>a>>b; int l1=strlen(a),l2=strlen(b); for(int i=0;i<l1;i++) x1[a[i]-'a']++; for(int i=0;i<l2;i++) x2[b[i]-'a']++; int flag=1,ans=0; for(int i=0;i<26;i++) { if(x2[i]!=0&&x1[i]==0) { flag=0;break; } ans+=min(x2[i],x1[i]); } if(!flag)cout<<-1<<endl; else cout<<ans<<endl; return 0; }
There is a right triangle with legs of length a and b. Your task is to determine whether it is possible to locate the triangle on the plane in such a way that none of its sides is parallel to the coordinate axes. All the vertices must have integer coordinates. If there exists such a location, you have to output the appropriate coordinates of vertices.
The first line contains two integers a, b (1 ≤ a, b ≤ 1000), separated by a single space.
In the first line print either "YES" or "NO" (without the quotes) depending on whether the required location exists. If it does, print in the next three lines three pairs of integers — the coordinates of the triangle vertices, one pair per line. The coordinates must be integers, not exceeding 109 in their absolute value.
1 1
NO
5 5
YES 2 1 5 5 -2 4
5 10
YES -10 4 -2 -2 1 2
#include <iostream> #include <stdio.h> #include <stdlib.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; typedef long long ll; int gcd(int a,int b) { return b? gcd(b,a%b):a; } int main() { int a,b; cin>>a>>b; int x=gcd(a,b); a/=x,b/=x; if(a>b)swap(a,b); if(x!=1) { int i,j,flag=0; for(i=1;i<x;i++) { for(j=i+1;j<x;j++) if(i*i+j*j==x*x) { flag=1;break; }if(flag)break; } if(!flag) cout<<"NO"<<endl; else { if(j*a!=i*b) cout<<"YES"<<endl<<0<<' '<<0<<endl<<-i*a<<' '<<j*a<<endl<<j*b<<' '<<i*b<<endl; else if(i*a!=j*b) cout<<"YES"<<endl<<0<<' '<<0<<endl<<-j*a<<' '<<i*a<<endl<<i*b<<' '<<j*b<<endl; else cout<<"NO"<<endl; } } else cout<<"NO"<<endl; return 0; }
比赛的时候硬是没dp出来。。。。状态转移方程。伤。。。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const long long int MOD=1e9+7; long long int dp[1100]; int n,p[1100]; int main() { cin>>n; for(int i=1;i<=n;i++) cin>>p[i]; for(int i=2;i<=n+1;i++) { dp[i]=(2*dp[i-1]-dp[p[i-1]]+2+MOD)%MOD; } cout<<dp[n+1]%MOD<<endl; return 0; }