1009C - Annoying Present - CodeForces Solution


greedy math *1700

Please click on ads to support us..

C++ Code:

// Code by Brijesh Verma(_bverma512)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vll vector<long long>
#define vin(n,v) for(int i = 0; i < n; i++) {ll a; cin >>a; v.push_back(a);}
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? 1 : 0);}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << "{"; __print(x.first); cerr << ","; __print(x.second); cerr << "}";}
template<typename T>
void __print(const T &x) {int f = 0; cerr << "{"; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
//vector<ll> p(1e7+7,1);
//void sieve(){
//    p[0]=p[1]=0;
//    for(ll i=2;i<=1e7+7;i++){
//       for(ll j=i*2;j<=1e7+7;j+=i){
//            // if(p[j]){p[j]=i;} // lowest prime
//           p[j]=0; 
//        }
//    }
//}
ll mod = 998244353;
ll inv(ll i) {if (i == 1) return 1; return (mod - ((mod / i) * inv(mod % i)) % mod) % mod;}
ll mod_mul(ll a, ll b) {a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod;}
ll pw(ll x,ll y){ll cnt = 1;x = x%mod;while(y > 0){if(y%2!=0){cnt = (cnt*x)%mod;}y /=2;x = ((x%mod)*(x%mod))%mod;}return cnt;}

void solve_tc(int tc){

    ll n,m;
    cin>>n>>m;

    ll sum =0;
    ll y = n/2;
    for(int i=0;i<m;i++){
        ll x,d;
        cin>>x>>d;

        sum += n*x;

        if(d>=0){
            sum += d*((n-1)*n)/2;
        }else{
            if(n%2){
                sum += d*((y+1)*y);
            }else sum += d*((y*(y-1))/2+(y*(y+1))/2);
        }

    }

    long double ans =sum/(1.0*n);

    cout<<fixed<<setprecision(15)<<ans<<endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int testcases=1;
    // cin>>testcases;
    for(int tt=1;tt<=testcases;tt++){
        solve_tc(testcases);
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number