Dima and Guards
Nothing has changed since the last round. Dima and Inna still love each other and want to be together. They’ve made a deal with Seryozha and now they need to make a deal with the dorm guards…
There are four guardposts in Dima’s dorm. Each post contains two guards (in Russia they are usually elderly women). You can bribe a guard by a chocolate bar or a box of juice. For each guard you know the minimum price of the chocolate bar she can accept as a gift and the minimum price of the box of juice she can accept as a gift. If a chocolate bar for some guard costs less than the minimum chocolate bar price for this guard is, or if a box of juice for some guard costs less than the minimum box of juice price for this guard is, then the guard doesn’t accept such a gift.
In order to pass through a guardpost, one needs to bribe both guards.
The shop has an unlimited amount of juice and chocolate of any price starting with 1. Dima wants to choose some guardpost, buy one gift for each guard from the guardpost and spend exactly n rubles on it.
Help him choose a post through which he can safely sneak Inna or otherwise say that this is impossible. Mind you, Inna would be very sorry to hear that!
看着难,实际很水。
求每行前两个数最小值加后两数最小值的最小值。
#include<bits/stdc++.h>
using namespace std;
int main() {
int n,i,a[5],b[5],c[5],d[5],ret1,ret2;
cin>>n;
for(i=1; i<=4; i++)
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
for(i=1; i<=4; i++) {
ret1=min(a[i],b[i]);
ret2=min(c[i],d[i]);
if(ret1+ret2<=n) {
cout<<i<<" "<<ret1<<" "<<n-ret1;
return 0;
}
}
cout<<-1;
}
Dima and To-do List
You helped Dima to have a great weekend, but it’s time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he’s very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.
水,只要特判k(不能当成余0因为会影响相同状况顺序导致错误)
#include<bits/stdc++.h>
using namespace std;
int main() {
int n,i,sum[100010]= {0},k,a,minx=1e9,ret=-1;
cin>>n>>k;
for(i=1; i<=n; i++) {
scanf("%d",&a);
if(i%k!=0)
sum[i%k]+=a;
else
sum[k]+=a;
}
for(i=1; i<=k; i++) {
if(sum[i]<minx)minx=sum[i],ret=i;
}
//cout<<sum[1]<<" "<<sum[0]<<endl;
cout<<ret;
}
Dima and Salad
Dima, Inna and Seryozha have gathered in a room. That’s right, someone’s got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something.
Dima and Seryozha have n fruits in the fridge. Each fruit has two parameters: the taste and the number of calories. Inna decided to make a fruit salad, so she wants to take some fruits from the fridge for it. Inna follows a certain principle as she chooses the fruits: the total taste to the total calories ratio of the chosen fruits must equal k. In other words, , where aj is the taste of the j-th chosen fruit and bj is its calories.
Inna hasn’t chosen the fruits yet, she is thinking: what is the maximum taste of the chosen fruits if she strictly follows her principle? Help Inna solve this culinary problem — now the happiness of a young couple is in your hands!
Inna loves Dima very much so she wants to make the salad from at least one fruit.
比较难的dp题,意思是有几个食物,每个有美味值以及热量值,给出k,要选美味值和尽可能大并且尽可能多的食物但要保证食物的热量值和*k=美味值和,打印出这个最大美味值。
当时没做出来,后来看大佬思路其实是01背包变形,只是有负数,因此要多弄100个数组大小处理掉负数,之后就相当于v=0的背包装体积为a[i]-k*b[i]的物品。
#include<bits/stdc++.h>
using namespace std;
int main() {
int i,bb,j,n,k,a[110],dp[110][11000],m;
cin>>n>>k;m=100*n;
memset(dp,-10000,sizeof(dp));dp[0][m]=0;
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
{scanf("%d",&bb);
bb=a[i]-bb*k;
for(j=m*2;j>=0;j--)
dp[i][j]=max(dp[i-1][j],dp[i-1][j-bb]+a[i]);
}
if(dp[n][m]==0)cout<<-1;
else
cout<<dp[n][m];
}
Dima and Trap Graph
Dima and Inna love spending time together. The problem is, Seryozha isn’t too enthusiastic to leave his room for some reason. But Dima and Inna love each other so much that they decided to get criminal…
Dima constructed a trap graph. He shouted: “Hey Seryozha, have a look at my cool graph!” to get his roommate interested and kicked him into the first node.
A trap graph is an undirected graph consisting of n nodes and m edges. For edge number k, Dima denoted a range of integers from lk to rk (lk ≤ rk). In order to get out of the trap graph, Seryozha initially (before starting his movements) should pick some integer (let’s call it x), then Seryozha must go some way from the starting node with number 1 to the final node with number n. At that, Seryozha can go along edge k only if lk ≤ x ≤ rk.
Seryozha is a mathematician. He defined the loyalty of some path from the 1-st node to the n-th one as the number of integers x, such that if he initially chooses one of them, he passes the whole path. Help Seryozha find the path of maximum loyalty and return to his room as quickly as possible!
有n个点m条无向边。初始你需要选择一个整数x,走第i条边的限制为Li <= x <= Ri,假设1-n的一条路径上可以选择的整数x有ans,问你最大的ans。看大佬思路后照葫芦画瓢了个并查集(自己写还是不太会)
#include<bits/stdc++.h>
using namespace std;
#define N 3010
#define inf 1e9
int p[N];
int n,m,ans;
struct edge {
int u,v,l,r;
} e[N];
bool cmp(edge a,edge b) {
if(a.r==b.r) return a.l<b.l;
else return a.r>b.r;
}
int find(int n) {
if(p[n]==n) return n;
else return p[n]=find(p[n]);
}
int main() {
scanf("%d %d",&n,&m);
for(int i=0; i<m; i++) {
scanf("%d %d %d %d",&e[i].u,&e[i].v,&e[i].l,&e[i].r);
}
sort(e,e+m,cmp);
for(int i=0; i<m; i++) {
for(int j=1; j<=n; j++) {
p[j]=j;
}
for(int j=0; j<m; j++) {
if(e[j].l>e[i].l) continue;
if(e[j].r<e[i].l) continue;
p[find(e[j].u)]=find(e[j].v);
if(find(1)==find(n)) {
ans=max(ans,e[j].r-e[i].l+1);
break;
}
}
}
if(ans!=0) {
printf("%d",ans);
} else {
printf("Nice work, Dima!");
}
return 0;
}
Dima and Magic Guitar
Dima loves Inna very much. He decided to write a song for her. Dima has a magic guitar with n strings and m frets. Dima makes the guitar produce sounds like that: to play a note, he needs to hold one of the strings on one of the frets and then pull the string. When Dima pulls the i-th string holding it on the j-th fret the guitar produces a note, let’s denote it as aij. We know that Dima’s guitar can produce k distinct notes. It is possible that some notes can be produced in multiple ways. In other words, it is possible that aij = apq at (i, j) ≠ (p, q).
Dima has already written a song — a sequence of s notes. In order to play the song, you need to consecutively produce the notes from the song on the guitar. You can produce each note in any available way. Dima understood that there are many ways to play a song and he wants to play it so as to make the song look as complicated as possible (try to act like Cobein).
We’ll represent a way to play a song as a sequence of pairs (xi, yi) (1 ≤ i ≤ s), such that the xi-th string on the yi-th fret produces the i-th note from the song. The complexity of moving between pairs (x1, y1) and (x2, y2) equals + . The complexity of a way to play a song is the maximum of complexities of moving between adjacent pairs.
Help Dima determine the maximum complexity of the way to play his song! The guy’s gotta look cool!
寻找串S中的每个数字在给定的矩阵中的位置,使S中相邻的两个数的曼哈顿距离最大,并输出相邻的数的最大曼哈顿距离
直接写tle,因此处理绝对值
| x1 - x2 | - | y1 - y2 |=以下4种情况下的最大值
one:
x1-x2+y1-y2 —> (x1+y1) - (x2+y2)
two:
-(x1-x2) + (y1-y2) —> (x2-y2) - (x1-y1)
three:
(x1-x2) - (y1-y2) —> (x1-y1) - (x2-y2)
four:
-( x1-x2 ) - (y1-y2) —> (x2+y2) - (x1+y1)
绝对值肯定正,因此是里面最大的。
这里也贴tle和ac代码了`
tle
#include<bits/stdc++.h>
using namespace std;
int n,m,c[2010][2010];int aa[4000001],bb[4000001],cc[4000001],dd[4000001];
int solve(int a,int b)
{int i,j,m1=0,m2=0,ans=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{if(c[i][j]==a)
{aa[++m1]=i;cc[m1]=j;//cout<<m1<<" "<<i<<" "<<j<<" "<<1<<endl;
}
if(c[i][j]==b)
{bb[++m2]=i,dd[m2]=j;//cout<<m2<<" "<<i<<" "<<j<<" "<<2<<endl;
}
}
for(i=1;i<=m1;i++)
for(j=1;j<=m2;j++)
{//cout<<aa[i].x<<" "<<aa[i].y<<" "<<bb[j].x<<" "<<bb[j].y<<endl;
int u=abs(aa[i]-bb[j])+abs(cc[i]-dd[j]);
ans=max(ans,u);
}
return ans;
}
int main()
{ //freopen("in.txt","r",stdin);
int k,s,i,j,ans[10][10]={0},ret=-9999,p,q;
cin>>n>>m>>k>>s;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&c[i][j]);
for(i=1;i<=k;i++)
for(j=i;j<=k;j++)
{ans[i][j]=solve(i,j);
ans[j][i]=ans[i][j];
}
for(i=1;i<=s;i++)
{scanf("%d",&q);
if(i>=2)ret=max(ret,ans[q][p]);//cout<<p<<" "<<q<<" "<<ret<<endl;
p=q;
}
//cout<<ans[1][1];
cout<<ret;
}
ac
#include<bits/stdc++.h>
using namespace std;
int main()
{ //freopen("in.txt","r",stdin);
int n,m,k,s,i,j,ans[10][5]={0},ret=-9999,p,q,pp;bool b[10][10]={0};
cin>>n>>m>>k>>s;
memset(ans,999,sizeof(ans));
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{scanf("%d",&pp);
if(i+j>ans[pp][1])ans[pp][1]=i+j;
if(i-j>ans[pp][2])ans[pp][2]=i-j;
if((-i+j)>ans[pp][3])ans[pp][3]=-i+j;
if((-i-j)>ans[pp][4])ans[pp][4]=-i-j;
}
for(i=1;i<=s;i++)
{scanf("%d",&q);
if(i>=2&&b[p][q]!=1)
{for(j=1;j<=4;j++)
ret=max(ret,(ans[q][j]+ans[p][5-j]));
//cout<<p<<" "<<q<<" "<<ret<<endl;
b[p][q]=1;
b[q][p]=1;
}
p=q;
}
cout<<ret;
}