Road To Zero CodeForces - 1342A
You are given two integers x and y. You can perform two types of operations:
Pay a dollars and increase or decrease any of these integers by 1. For example, if x=0 and y=7 there are four possible outcomes after this operation:
x=0, y=6;
x=0, y=8;
x=−1, y=7;
x=1, y=7.
Pay b dollars and increase or decrease both integers by 1. For example, if x=0 and y=7 there are two possible outcomes after this operation:
x=−1, y=6;
x=1, y=8.
Your goal is to make both given integers equal zero simultaneously, i.e. x=y=0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0.
Calculate the minimum amount of dollars you have to spend on it.
Input
The first line contains one integer t (1≤t≤100) — the number of testcases.
The first line of each test case contains two integers x and y (0≤x,y≤109).
The second line of each test case contains two integers a and b (1≤a,b≤109).
Output
For each test case print one integer — the minimum amount of dollars you have to spend.
Example
Input
2
1 3
391 555
0 0
9 4
Output
1337
0
Note
In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391+555+391=1337 dollars.
In the second test case both integers are equal to zero initially, so you dont’ have to spend money.
思路:如果2*a<=b的话,就用第一种方法;否则共同的那一部分用b,多于的那一部分用a。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll x,y,a,b;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
cin>>x>>y>>a>>b;
if(x==y&&x==0) cout<<0<<endl;
else
{
if(a*2<=b) cout<<(x+y)*a<<endl;
else cout<<min(x,y)*b+(max(x,y)-min(x,y))*a<<endl;
}
}
return 0;
}
Binary Period CodeForces - 1342B
思路:在2*t范围内,我们可以构造出101010…这样的串的,所以k如果不是1的话,那就是2.先检测一下原来的串是否只由一种元素构成,如果不是那就构造出10101010…这样的。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
string s;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
cin>>s;
int flag=1;
for(int i=1;i<s.length();i++)
{
if(s[i]!=s[i-1])
{
flag=0;
break;
}
}
if(flag) cout<<s<<endl;
else
{
for(int i=0;i<2*s.length();i++)
{
if(i%2==0) cout<<1;
else cout<<0;
}
cout<<endl;
}
}
return 0;
}
努力加油a啊,(o)/~