2016年9月13日 星期二

ZJ d645 輪下亡魂

http://zerojudge.tw/ShowProblem?problemid=d645

大概是我寫出的第二題DP吧XDD
第一題是d652 前天寫出來的

或許這在大家眼裡都是水題XDDD
不過我決定紀錄一下(#
這是一個沒有寫過普通背包就寫有無限有限背包問題的故事(#

總之開一個當容量為多少的時候最大飽足感的陣列
然後對於每種食物去跑一次每種容量再跑一圈每種數量(工弎洨
然後如果是無限的飼料就跑到容量用完就好(?

算了我還是直接上code好了=w=
表達能力真的有點((((



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<bits/stdc++.h>
using namespace std;

int n,c,v[1004],w[1004],t[1004],cs[1004];


int dp(){
    cs[0]=0;
    for(int i=1;i<=n;i++){
        for(int j=c;j>=w[i];j--){
            for(int k=0;j-w[i]*k>=0 && (k<=t[i] || t[i]==-1);k++){
                cs[j] = max(cs[j], cs[j-w[i]*k]+v[i]*k);
            }
        }
    }
    return cs[c];
}

int main(){
    cin>>n>>c;
    for(int i=1;i<=n;i++){
        cin>>v[i]>>w[i]>>t[i];
    }

    cout<<dp()<<endl;
}

發現出乎意料的短(?

TIOJ 1907 俄羅斯娃娃

http://tioj.infor.org/problems/1907

前幾天就解出來了 不過忘記丟了
雖然不是自己解的(((

總之把每個娃娃w跟h搞成一個pair然後存起來
然後對那個vector(或是要用array也可以啦)sort
但是當wi==wj的時候hj>hi要在前面 然後wi<wj在前面 所以就自己炸一個cmp出來
然後再對h找LIS就過了

原本用了奇怪的方法優化+二分搜又寫爛一次-w-
看起來二分搜是我(眼前)最大的敵人(###

喔然後code因為有神奇優化過 所以沒用黑魔法(?



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<vector>
#include<stdio.h>
#include<utility>
#include<algorithm>
using namespace std;
#define pii pair<int,int>
#define X first
#define Y second

int n,w,h,i,t,tt;
char c;
vector<pii> rs,B;

inline int rit(){
    t=0;
    do{
        c=getchar_unlocked();
    }while(c<'0'||c>'9');
    do{
        t=t*10+c-'0';
        c=getchar_unlocked();
    }while(c>='0'&&c<='9');
    return t;
};


bool cmp(const pii &a,const pii &b){
    if(a.X==b.X && a.Y>b.Y)return 1;
    return a.X<b.X;
}
bool cmpp(const pii &a,const pii &b){
    return a.Y<b.Y;
}

inline int LIS(){
    B.clear();
    B.push_back(rs[0]);
    for(i=1;i<n;i++){
        if(cmpp(B.back(),rs[i])){
            B.push_back(rs[i]);
        }
        else{
            *lower_bound(B.begin(),B.end(),rs[i],cmpp) = rs[i];
        }
    }
    return B.size();
}

int main(){
    tt=rit();
    while(tt--){
        n=rit();
        rs.clear();
        for(i=0;i<n;i++){
            w=rit();
            h=rit();
            rs.push_back({w,h});
        }
        sort(rs.begin(),rs.end(),cmp);
        printf("%d\n",LIS());
    }
}

.....我真的懶得改define跟include的顏色ˊ_>ˋ

2016年9月7日 星期三

TIOJ 1613 尋找蘿莉

http://tioj.infor.org/problems/1613

結果反而在pE先解出來之前先搞出pF了
因為要讓機率高的試的次數盡量少 所以其實是一顆霍夫曼
結果發現真的建樹的話會MLE
不過反正因為沒有要最後的編碼 只要機率*層數就好
所以就算就好(?



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<bits/stdc++.h>
using namespace std;
#define ll long long

inline int rit(){
    int t=0,k=1;
    char c;
    do{
        c=getchar();
        if(c=='-')k=-1;
    }while(c<'0'||c>'9');
    do{
        t=t*10+c-'0';
        c=getchar();
    }while(c>='0'&&c<='9');
    return t*k;
};

ll n,t,t1,t2;
ll ans;

priority_queue<ll,vector<ll>, greater<ll> > pq;

int main(){
    n=rit();
    for(int i=0;i<n;i++){
        t=rit();
        pq.push(t);
    }

    while(pq.size()>1){
        t1=pq.top();pq.pop();
        t2=pq.top();pq.pop();
        pq.push(t1+t2);
        ans+=t1+t2;
    }
    cout<<ans<<endl;
}

2016年9月6日 星期二

校內初賽

今天比了校內的資訊能力競賽初賽
考前還很緊張怕被刷下來
結果發現好像滿水的(x
包括亂入(?)的兩個人我是第六名
然後如果我知道std有binary_search的話就有第三名QwQ
出題者破台 超電(?

pA http://tioj.infor.org/problems/1544
太水了懶得說(?
一開始這題直接丟了個87分
後來想說剩下13分等等有空再弄就先去pB了

pB http://tioj.infor.org/problems/1547
還是很水(?
所以還是懶得說(?

pC http://tioj.infor.org/problems/1809
就是這題超討厭
因為二分寫錯就只有26分= =
然後爆搜好像都有37分..

也不難 把每個握手(讓a<=b)push到一個vector<pair<int,int>>裡面
然後sort 然後二分搜就可以過了

pD http://tioj.infor.org/problems/1546
這題比較開心(?
給你第一排跟第一列 說每個人一次可以填一格新的數字(必須=左上XOR左XOR上) 或是劃出一條p<=len<=q的遞增子序列(不能重複劃) 問誰會沒有操作可以做
基本上因為已經給你第一排跟第一列了 所以整個方格都是確定的
所以有幾個遞增子序列也是固定的 所以只要數一下然後%玩家就可以過了

不過這圖如果完整的開完整個圖的話會MLE
所以一開始就只拿91分而已
後來想到因為你只需要上一條跟這一條的第一個就可以完整的生出這一條
所以就開一個[2][m]的陣列然後交叉掃過去
數那一行有幾個遞增的時候再生就好了
然後就過了

這題其實卡最久的是我以為要嚴格遞增((

pE http://tioj.infor.org/problems/1907
好像是DAG的最長路徑之類的
然後就TLE了最後兩筆 只有60分
所以我也不知道怎麼解(?
大概知道有個地方可以優化一下 不過目前還沒有實作的靈感##

pF http://tioj.infor.org/problems/1613
完全沒想法(O

簡單來說就算是這麼長的文我還是只有一題可以提供code XDD
pD下收
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define ld long double
#define PB(x) push_back(x)
#define MP(x,y) make_pair(x,y)
#define vint vector<int>
#define pii pair<int,int>
#define rz(x) resize(x)
#define X first
#define Y second
// #define m (l+r)/2
// #define xm (x1+x2)/2
// #define ym (y1+y2)/2
#define DE cout<<"de"<<endl;

inline int rit(){
    int t=0,k=1;
    char c;
    do{
        c=getchar();
        if(c=='-')k=-1;
    }while(c<'0'||c>'9');
    do{
        t=t*10+c-'0';
        c=getchar();
    }while(c>='0'&&c<='9');
    return t*k;
};

int n,m,p,q;
int mp1[10003],mp2[10003],w[2][10003];
ll act;

int main(){
    n=rit(),m=rit(),p=rit(),q=rit();
    for(int i=0;i<m;i++){
        mp1[i]=rit();
    }
    for(int i=1;i<m;i++){
        mp2[i]=rit();
    }
    mp2[0]=mp1[0];
    act=(m-1)*(m-1);

    int i,j,k;
    for(i=0;i<m;i++){
            if(i){
                w[i&1][0]=mp2[i];
                for(int rr=1;rr<m;rr++){
                    w[i&1][rr]=w[(i+1)&1][rr-1]^w[(i+1)&1][rr]^w[i&1][rr-1];
                }
            }
            else{
                for(int rr=0;rr<m;rr++){
                    w[i&1][rr]=mp1[rr];
                }
            }
        for(j=0;j<m;j=k){
            for(k=j+1;k<m && w[i&1][k]>=w[i&1][k-1];k++){

            }
            int len=k-j;
            if(len>=p){
                if(len>=p && p==q){
                    act+=len-(p-1);
                }
                else if(len>=p && len<=q){
                    len-=(p-1);
                    act+=len*(len+1)/2;
                }
                else{
                    int mlen=len-(p-1);
                    int xlen=len-(q-1);
                    act+=(mlen+xlen)*(mlen-xlen+1)/2;
                }
            }
        }
    }
    
    memset(w,0,sizeof(w));
    for(i=0;i<m;i++){
            if(i){
                w[i&1][0]=mp1[i];
                for(int rr=1;rr<m;rr++){
                    w[i&1][rr]=w[(i+1)&1][rr-1]^w[(i+1)&1][rr]^w[i&1][rr-1];
                }
            }
            else{
                for(int rr=0;rr<m;rr++){
                    w[i&1][rr]=mp2[rr];
                }
            }
        for(j=0;j<m;j=k){
            for(k=j+1;k<m && w[i&1][k]>=w[i&1][k-1];k++){

            }
            int len=k-j;
            if(len>=p){
                if(len>=p && p==q){
                    act+=len-(p-1);
                }
                else if(len>=p && len<=q){
                    len-=(p-1);
                    act+=len*(len+1)/2;
                }
                else{
                    int mlen=len-(p-1);
                    int xlen=len-(q-1);
                    act+=(mlen+xlen)*(mlen-xlen+1)/2;
                }
            }
        }
    }

    printf("%d\n",act%n);
}

為什麼它可以把#define 跟#include 變成註解的顏色=w=
還我還要自己改 而且改完才發現一坨綠的好醜(#

2016年9月3日 星期六

TIOJ 1006 - 大數除法

因為各種心血來潮 所以就跑去寫了TIOJ 1006 : http://tioj.infor.org/problems/1006
然後因為想說可以當模板之類的用(?) 所以就試著寫的完整了一點
大概就先把加減乘等於大小於輸入輸出之類的然後再開工除法這樣
一樣是用直式除法的概念去寫(吧
不過重點就是不管怎樣就WA 6 QwQ
各種測資都測過了 不過因為找不到bug所以懶得找(# 於是就貼上來了(?

亂渣code下收


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include<bits/stdc++.h>
#define ll long long

using namespace std;

struct BIG;
BIG _10pow(int);

struct BIG{
    int n[555];
    int length;
    BIG(){
        memset(n,0,sizeof(n));
        length = 0;
    }
    BIG(int in){
        memset(n,0,sizeof(n));
        length = 0;
        int ptr=0;
        while(in){
            this->n[ptr++]=in%10;
            in/=10;
        }
        this->length=ptr;
    }
    BIG(long in){
        memset(n,0,sizeof(n));
        length = 0;
        int ptr=0;
        while(in){
            this->n[ptr++]=in%10;
            in/=10;
        }
        this->length=ptr;
    }
    BIG(ll in){
        memset(n,0,sizeof(n));
        length = 0;
        int ptr=0;
        while(in){
            this->n[ptr++]=in%10;
            in/=10;
        }
        this->length=ptr;
    }
    BIG(string s){
        memset(n,0,sizeof(n));
        length = 0;
        int i;
        for(i=0;i<(int)s.length();i++){
            if(s[i]>'9'||s[i]<'0'){
                printf("WTF are you input?\n");
                break;
            }
            this->n[i]=s[s.length()-1-i]-'0';
        }
        this->length=i;
    }

    void operator=(string const &s){
        stringstream ss;
        ss<<s;
        ss>>*this;
        return;
    }
    void operator=(BIG const &big){
        this->length=big.length;
        for(int i=0;i<this->length;i++){
            this->n[i]=big.n[i];
        }
        return;
    }
    // void operator=(string const &s){
    //     stringstream ss;
    //     ss<<s;
    //     ss>>*this;
    //     return;
    // }

    friend ostream &operator<<(ostream &ostm,BIG big){
        for(int i=big.length-1;i>=0;i--){
            ostm<<big.n[i];
        }
        // ostm<<" ("<<big.length<<")";
        if(big.length==0){
            ostm<<0;
        }
        return ostm;
    }
    friend istream &operator>>(istream &istm,BIG &big){
        memset(&big,0,sizeof(big));
        string s;
        istm>>s;
        int i;
        for(i=0;i<(int)s.length();i++){
            if(s[i]>'9'||s[i]<'0'){
                printf("WTF are you input?\n");
                break;
            }
            big.n[i]=s[s.length()-1-i]-'0';
        }
        big.length=i;
        // ostm<<" ("<<big.length<<")";
        return istm;
    }

    operator bool() const{
        return this->length;
    }

    bool operator<(BIG const &a){
        if(a.length!=this->length)return this->length<a.length;
        for(int i=a.length-1;i>=0;i--){
            if(a.n[i]!=this->n[i])return this->n[i]<a.n[i];
        }
        return false;
    }
    bool operator<=(BIG const &a){
        if(a.length!=this->length)return this->length<=a.length;
        for(int i=a.length-1;i>=0;i--){
            if(a.n[i]!=this->n[i])return this->n[i]<=a.n[i];
        }
        return true;
    }

    bool operator>(BIG const &a){
        return !(*this<=a);
    }

    friend BIG operator+(BIG const &a,int b){
        BIG t;
        int i=0;
        t.n[0]+=b;
        for(;;i++){
            t.n[i]+=a.n[i];
            if(!t.n[i])break;
            if(t.n[i]>9){
                t.n[i+1]+=t.n[i]/10;
                t.n[i]%=10;
            }
        }
        t.length=i;
        return t;
    }
    friend BIG operator+(int b,BIG const &a){
        BIG t;
        int i=0;
        t.n[0]+=b;
        for(;;i++){
            t.n[i]+=a.n[i];
            if(!t.n[i])break;
            if(t.n[i]>9){
                t.n[i+1]+=t.n[i]/10;
                t.n[i]%=10;
            }
        }
        t.length=i;
        return t;
    }
    friend BIG operator+(BIG const &a,BIG const &b){
        BIG t;
        int i=0;
        for(;;i++){
            t.n[i]+=(a.n[i]+b.n[i]);
            if(!t.n[i])break;
            if(t.n[i]>9){
                t.n[i+1]+=t.n[i]/10;
                t.n[i]%=10;
            }
        }
        t.length=i;
        return t;
    }
    BIG& operator++(){
        this->n[0]++;
        for(int i=0;i<this->length;i++){
            if(n[i]>9){
                n[i+1]+=n[i]/10;
                n[i]%=10;
            }
            else{
                break;
            }
        }
        return *this;
    }
    BIG& operator++(int){
        BIG tmp(*this);
        operator++();
        return tmp;
    }

    BIG operator-(int b){
        // cout<<"in BIG - int, int b="<<b<<endl;
        BIG t;
        int i=0;
        t.n[0]-=b;
        for(;;i++){
            t.n[i]+=this->n[i];
            if(t.n[i]==0)break;
            if(t.n[i]<0){
                t.n[i+1]+=((t.n[i]+1)/10-1);
                // cout<<t.n[i]/10<<endl;
                t.n[i]%=10;
                if(t.n[i])t.n[i]+=10;
            }
        }
        t.length=i;
        return t;
    }
    friend BIG operator-(BIG &a,BIG &b){
        // cout<<"in BIG - BIG, a,b="<<a<<" "<<b;
        BIG t=a;
        // cout<<t<<endl;
        if(b>a){
            printf("Wtf are you doing?\n");
            return 0;
        }
        for(int i=0;i<b.length;i++){
            t.n[i]-=b.n[i];
        }
        for(int i=0;i<t.length-1;i++){
            if(t.n[i]<0){
                t.n[i+1]+=((t.n[i]+1)/10-1);
                t.n[i]%=10;
                if(t.n[i])t.n[i]+=10;
            }
        }
        int i;
        for(i=t.length-1;i>=0;i--){
            if(t.n[i])break;
        }
        t.length=i+1;
        // cout<<a.length<<endl;
        // cout<<", t ="<<t<<endl;
        return t;
    }

    friend BIG operator*(BIG const &a,BIG const &b){
        BIG t;
        int mxl=a.length+b.length;
        for(int i=0;i<a.length;i++){
            for(int j=0;j<b.length;j++){
                t.n[i+j]+=a.n[i]*b.n[j];
            }
        }
        for(int i=0;i<mxl+1;i++){
            t.n[i+1]+=t.n[i]/10;
            t.n[i]%=10;
        }
        int i;
        for(i=mxl+1;i>=0;i--){
            if(t.n[i])break;
        }
        t.length=i+1;
        return t;
    }
    friend BIG operator*(BIG const &a,int const &bi){
        BIG b(bi);
        return a*b;
    }
    friend BIG operator*(BIG const &a,long const &bi){
        BIG b(bi);
        return a*b;
    }
    friend BIG operator*(BIG const &a,ll const &bi){
        BIG b(bi);
        return a*b;
    }
    friend BIG operator*(int const &bi,BIG const &a){
        BIG b(bi);
        return a*b;
    }
    friend BIG operator*(long const &bi,BIG const &a){
        BIG b(bi);
        return a*b;
    }
    friend BIG operator*(ll const &bi,BIG const &a){
        BIG b(bi);
        return a*b;
    }

    friend BIG operator/(BIG &a,BIG &b){
        BIG t;
        int mxl=a.length-b.length+1;
        // cout<<mxl<<endl;
        // int de=0;
        while(a>=b){
            int _l=a.length-b.length;
            nodid:
            if(a<b)break;
            int k=1;
            // cout<<k*b*_10pow(_l)<<endl;
            // cout<<k<<" "<<a<<" "<<b<<" "<<_l<<" "<<k*b*_10pow(_l)<<endl;
            while(k*b*_10pow(_l)<=a){
                k++;
                // cout<<k<<" "<<a<<" "<<b<<" "<<_l<<" "<<k*b*_10pow(_l)<<endl;
                // cout<<"not here"<<endl;
            }
            // cout<<a<<" "<<b<<" ";
            // cout<<"t=";for(int i=2;i>=0;i--)putchar(t.n[i]+'0');putchar('\n');
            // cout<<" k="<<k<<endl;
            // assert(de<5);
            // cout<<"_l="<<_l<<", k-1="<<k-1<<endl;
            t.n[_l]+=k-1;
            // cout<<"t=";for(int i=2;i>=0;i--)putchar(t.n[i]+'0');putchar('\n');
            // cout<<"-"<<((k-1)*b)*_10pow(_l)<<endl;
            // cout<<"a = "<<a<<", ((k-1)*b)*_10pow(_l) = "<<((k-1)*b)*_10pow(_l)<<" mmm "<<a-(((k-1)*b)*_10pow(_l))<<endl;
            // cout<<"old a = "<<a<<", ";
            // cout<<typeid(((k-1)*b)*_10pow(_l)).name()<<endl;
            // cout<<"making tt"<<endl;
            BIG tt = b*_10pow(_l)*(k-1);
            if(tt.length==0){
                _l--;
                goto nodid;
            }
            a=a-tt;
            // cout<<"tt returned, a="<<a<<endl;
            // cout<<"new a = "<<a<<endl;
            // a.length--;
            // de++;
        }
        // cout<<"t=";for(int i=2;i>=0;i--)putchar(t.n[i]+'0');putchar('\n');
        for(int i=0;i<mxl+2;i++){
            t.n[i+1]+=t.n[i]/10;
            t.n[i]%=10;
        }
        // cout<<"t=";for(int i=2;i>=0;i--)putchar(t.n[i]+'0');putchar('\n');
        int i;
        for(i=mxl+4;i>=0;i--){
            if(t.n[i])break;
        }
        // for(int i=2;i>=0;i--)putchar(t.n[i]+'0');putchar('\n');
        t.length=i+1;
        // for(int i=2;i>=0;i--)putchar(t.n[i]+'0');putchar('\n');
        return t;
    }
};

BIG _10pow(int l){
    BIG t;
    t.n[l]=1;
    t.length=l+1;
    return t;
}

BIG a,b;

int main(){
    cin>>a>>b;
    // if(as==bs){
    //     cout<<1<<endl;
    //     return 0;
    // }
    // BIG a(as);
    // BIG b(bs);
    // a=6546;b=123;
    // c.length+=5;
    // if(a.length>50||b.length>50)return 0;
    if(b==0)return 0;
    cout<<a/b<<endl;
}