 # EVMHACK Editorial

https://www.codechef.com/START24C/problems/EVMHACK

Setter: Utkarsh Gupta + Daanish
Tester: Aryan Chaudhary
Editorialist: Rishabh Gupta

Cakewalk

None

# PROBLEM:

There are three cities and thus three EVMs. An insider told Chef that his party got A,B,C votes in these three cities according to the EVMs. Also, the total number of votes cast are P,Q,R respectively for the three cities.

Chef, being the party leader, can hack at most one EVM so that his party wins. On hacking a particular EVM all the votes cast in that EVM are counted in favor of Chef’s party.

A party must secure strictly more than half of the total number of votes cast in order to be considered the winner. Can Chef achieve his objective of winning by hacking at most one EVM?

# EXPLANATION:

Which EVM should he hack?

Chef would want to hack the EVM that would gain him the maximum new votes.

So his new vote count would increase to a+b+c+ max(p-a,q-b,r-c). Now we can answer yes or no depending on the new vote count.

# TIME COMPLEXITY:

O(1) for each test case.

# SOLUTION:

Setter's Solution
``````#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define rb pop_back
#define ti tuple<int, int, int>
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
using namespace std;

const string newln = "\n", space = " ";
const int maxt = 5e3, maxv = 100;

int main()
{
int t; cin >> t;
int a, b, c, p, q, r;
int cnt = 0;
while(t--){
cin >> a >> b >> c;

cin >> p >> q >> r;

bool ans = max({p + b + c, a + q + c, a + b + r}) > (p + q + r) / 2;
cout << (ans ? "YEs" : "No") << endl;
cnt += ans;
}
}
``````
Editorialist''s Solution
``````#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define dd double
#define endl "\n"
#define pb push_back
#define all(v) v.begin(),v.end()
#define mp make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll,ll>
#define fo(i,n) for(int i=0;i<n;i++)
#define fo1(i,n) for(int i=1;i<=n;i++)
ll mod=1000000007;
ll n,k,t,m,q,flag=0;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(a) -- no. of elements strictly less than a
// s.find_by_order(i) -- itertor to ith element (0 indexed)
ll min(ll a,ll b){if(a>b)return b;else return a;}
ll max(ll a,ll b){if(a>b)return a;else return b;}
ll gcd(ll a , ll b){ if(b > a) return gcd(b , a) ; if(b == 0) return a ; return gcd(b , a%b) ;}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifdef NOOBxCODER
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
#define NOOBxCODER 0
#endif
cin>>t;
//t=1;
while(t--){
ll a,b,c,p,q,r;
cin>>a>>b>>c>>p>>q>>r;
int s = a+b+c,tot= p+q+r;
int val= max(p-a,q-b);val =max(val , r-c);
if(s+val +s+val > p+q+r )cout<<"YES"<<endl;
else cout<<"NO"<<endl;

}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
return 0;
}

``````
1 Like

Can you please let me know were i made mistake in my code

#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t>0)
{
long long int a,b,c,p,q,r;
scanf("%lld %lld %lld %lld %lld %lld",&a,&b,&c,&p,&q,&r);
if((a+b+c)>((p+q+r)/2))
printf(“YES\n”);
else
{
int max=p-a;
if(max<(q-b))
max=q-b;
else if(max<(r-c))
max=r-c;
if((max+a+b+c)>((p+q+r)/2))
printf(“YES\n”);
else
printf(“NO\n”);
}
t–;
}
return 0;
}

1 Like

CAN ANYONE TELL ME MY MISTAKE ???WHERE I AM WRONG???

#include<bits/stdc++.h>
using namespace std;
int main(){
int T;
cin>>T;
while(T–){
double a,b,c,p,q,r;
cin>>a>>b>>c>>p>>q>>r;
double m = (p+q+r)/2;

vector<pair<int,int>>v;
v.push_back(make_pair(p,a));
v.push_back(make_pair(q,b));
v.push_back(make_pair(r,c));
sort(v.begin(),v.end());
int j = v.first+v.second+v.second;
// cout<<j<<endl;
// cout<<m<<endl;
if(j>m)
cout<<“YES”<<endl;
else
cout<<“NO”<<endl;
}
return 0;
}

1 Like

@tusharkumar123
when you have sorted vector it is sorted according to p,q,r so v.first will be maximum of p,q,r but it is not what we needed ,we needed maximum of (p-a,q-b,r-c)
i.e if s=a+b+c ;
maximum of (s+p-a,s+q-b,s+r-c)=maximum of (p+b+c,a+q+c,a+b+r);

1 Like

oh got it…thanks a lot

https://www.codechef.com/viewsolution/57665451
Please tell what happened wrong here.
I basically did the same just method but in a little different.

can anyone explain what mistake i have done , because its partial accepted

#include <bits/stdc++.h>

using namespace std;

int main()
{
int t;
cin >> t;
while (t–)
{
int a, b, c, p, q, r;
cin >> a >> b >> c >> p >> q >> r;
int x = max(p, max(q, r));
int y = min(a, min(b, c));

``````int z = (x + a + b + c) - y;

float k = (p + q + r) / 2;

if (z > k)
cout << "Yes" << endl;
else
cout << "No" << endl;
``````

}
return 0;
}

``````#include <bits/stdc++.h>

using namespace std;

int main()
{
int t;
cin >> t;
while (t--)
{
int a, b, c, p, q, r;
cin >> a >> b >> c >> p >> q >> r;
int x = max(p-a, max(q-b, r-c));
int y = min(a, min(b, c));

int z = (x + a + b + c);

int k = (p + q + r) / 2;

if (z > k)
cout << "Yes" << endl;
else
cout << "NO" << endl;
}
return 0;
}
``````